if then else
Blitz3D Forums/Blitz3D Programming/if then else
| ||
I have music in my game and I wanted the user to be able to toggle it off and on so I wrote this:If KeyHit (50)=True And musicflag = 0 Then musicflag = .5 Else musicflag = 0Now the music is playing on a channel and musicflag is the volume represented here: ChannelVolume play_annoy,musicflagplay_annoy is the music channel. The music loops because in the front of the program I loaded it as: Global annoy=LoadSound("media/heroloop1.ogg") LoopSound annoy play_annoy=PlaySound(annoy)I can't make this work... it never comes on except for a brief splash as the program starts. What am I doing wrong? |
| ||
Firstly - if musicflag is an integer not a float then it will always be set to zero (as it will round down from 0.5 to 0). A better way would be to do the following: At the start of the game set Musicflag to 0. then do this: If KeyHit(50)=true then MusicFlag=1-MusicFlag which will alternate it between 0 and 1 each time it is pressed. then do this If MusicFlag=1 then channelvolume PLay_annoy,0.5 else channelvolume Play_annoy,0 |
| ||
If KeyHit(50) >= 1 then MusicFlag=1-MusicFlag KeyHit() doesn't always return just true (1)- it returns the number of times the key has been pressed since the last call to it. Just a little thing you might want to keep in mind just in case. Rarely if ever will it return above 1 for me. |
| ||
I thought (anything except 0) == True. Anyway, I reckon you're using an integer. |
| ||
it does :) |
| ||
You may want to add the following lines to that or it will keep toggling on and off as you hold the key down. .K50 If KeyDown(50) Goto K50 ;loop until key released |
| ||
I wouldn't do as AbbaRue suggested unless you want your game to pause while you hold down the key. |
| ||
lol@paused program. Do this, MusicFlag=(keyhit(1)>0) The ifless optimization |
| ||
That will reset the music flag to zero except for a split second when you hit key (1). Noels solutions will work. |
| ||
So it will. (Egg on face) |
| ||
Noel and Matty... excellent suggestion! All of you thank you for that help... :) -RZ |