Page 1 of 1
What is the Switches Statement?
Posted: Sat Dec 31, 2016 4:17 am
by GeorgeMcGinn
I see there are a couple of statements involving SWITCH.
What are the SWITCH statements used for?
The reference manual says how to create them, but not what they are for, and I am trying to figure this out for the programmer's guide.
Thanks,
Happy New Year
George
Re: What is the Switches Statement?
Posted: Sat Dec 31, 2016 6:26 am
by Mr. Kibernetik
Try to create one, and you will see how it looks.
Re: What is the Switches Statement?
Posted: Sat Dec 31, 2016 9:05 am
by Henko
You can let the user choose between binary conditions DURING RUNTIME, for instance:
In a drawing program : "snap to grid (yes/no)"
In a calculation program : "automatic recalculation after edit (yes/no)
In a game : "gravity (yes/no)"
And so on...
Re: What is the Switches Statement?
Posted: Sat Dec 31, 2016 11:07 am
by Dav
A SWITCH is an interactive object that works just like the light switch on a wall, on/off, or something like yes/no, etc. You can set it between two conditions and the user can choose how to toggle it during the program.
Here is a small program that uses them. It's a basic metronome and has two switches that the user can toggle for options....
- Dav
Code: Select all
'Basic metronome
'Flashes and ticks.
'for ipad or iphone
'you can adjust tempo, bmp, flash, tick
'default settings for metronome
tempo= 120
meter=3
flash=1
tick=1
'setup screen
text
set output font color 1,1,1
set output back color 0,128/255,128/255
set buttons font name "Verdana"
set output font size "76"
set buttons font size "28"
'make objects
slider "tempo" value tempo/240 at 3,100 size 300
switch "flash" state 1 at 50,70
switch "tick" state 1 at 170,70
'make buttons
button "tempo" text "Tempo: "&str$(tempo) at 20,150
button "bpm" text "BPM: "& str$(meter) at 20,220
button "+" text "+" at 190,220
button "-" text "-" at 150,220
'start tick at beat 1
metercount=1
do
if metercount=1 then
'flash on beat 1 if flash on
if flash = 1 then
'flash...
set output back color 1,1,0
pause .02
'back to normal
set output back color 0,128/255,128/255
end if
'tick high on beat 1 if tick on
if tick=1 then
notes set "115:c6"
notes play
end if
else
'back to normal color on other beats
if flash = 1 then
set output back color 0,128/255,128/255
end if
'lower tick on other beats
if tick=1 then
notes set "115:c4"
notes play
end if
end if
'display beat info
text clear
print "Beat:"&metercount
print "* ^";
'raise beat count here
metercount = metercount+1
if metercount > meter then metercount=1
'change tempo..
if slider_changed("tempo") then
newtempo=int(240*slider_value("tempo"))
'don't allow under 20
if newtempo > 20 then
button "tempo" set text "Tempo: "&str$(newtempo)
tempo=newtempo
end if
end if
'raise bpm...
if button_pressed("+") then
'only allow 12 bpm
if meter < 12 then
meter = meter + 1
button "bpm" set text "BPM: "& str$(meter)
end if
end if
'lower bpm
if button_pressed("-") then
'dont go below 1
if meter > 1 then
meter = meter - 1
button "bpm" set text "BPM: "& str$(meter)
end if
end if
'turn flash on/off
if switch_changed("flash") then
if switch_state("flash") = 1 then
flash= 1
else
flash= 0
end if
end if
'turn tick on/off
if switch_changed("tick") then
if switch_state("tick") = 1 then
tick = 1
else
tick = 0
end if
end if
'Tick delay routine below...
'we added a small pause to flash screen.
'So here we compensate for that..
if flash = 1 then
'only do after 1st flashing beat
'we added .02, so we subtract here
if metercount=1 then
pause (60 / tempo) -.02
else
pause 60 / tempo
end if
else
pause 60 / tempo
end if
until forever
Re: What is the Switches Statement?
Posted: Sun Jan 01, 2017 8:34 am
by GeorgeMcGinn
Hi,
I would have liked to try it, but the documentation is very sparse on details. There was no way I could program it, unless I know of a similar feature in another languages, like C/C++, which I do not.
Sometimes I think that the reference manual, just a regurgitation of the help menus, does not help whatsoever when a newer person, especially to SmartBASIC, doesn't even know half of the features.
For example, I know that Sprites are for at least automated graphics, and I got a part understanding of it, but it is not complete.
Now the code that Dav supplied does help me immensely. I'm a visual and practical use learner, so I do need to do to learn.
This is why I am writing the Programmer's Guide, because I will be giving more than a one line description on what a statement does, especially when there isn't an example file to help me.
Thanks Dav for the code. I can use it to dissect what the commands do and how they work.
George.
Mr. Kibernetik wrote:Try to create one, and you will see how it looks.
Re: What is the Switches Statement?
Posted: Sun Jan 01, 2017 8:37 am
by GeorgeMcGinn
Thanks Henko.
This helps and provides more info with the sample program, I should be fine.
Happy New Year everyone, George.
Henko wrote:You can let the user choose between binary conditions DURING RUNTIME, for instance:
In a drawing program : "snap to grid (yes/no)"
In a calculation program : "automatic recalculation after edit (yes/no)
In a game : "gravity (yes/no)"
And so on...
Re: What is the Switches Statement?
Posted: Sun Jan 01, 2017 10:26 am
by Mr. Kibernetik
GeorgeMcGinn wrote:I would have liked to try it, but the documentation is very sparse on details. There was no way I could program it, unless I know of a similar feature in another languages, like C/C++, which I do not.
According to documentation, it is:
SWITCH 0 STATE 0 AT 100,100