Computer Science Canada

Button Help Pleasee

Author:  jettyboi [ Thu Jun 22, 2017 7:11 pm ]
Post subject:  Button Help Pleasee

I made a button that will play a song and a gif when clicked (GUI.Button if it matters)
So far, if I click it, it will play the song and the gif animation, but I want it to stop when I click the button a second time.
Is there any way to do this?
This is the code for one button called "Spooked"


var s1 : int := Pic.FileNew ("skele (1).gif")
var s2 : int := Pic.FileNew ("skele (2).gif")
var s3 : int := Pic.FileNew ("skele (3).gif")
var s4 : int := Pic.FileNew ("skele (4).gif")
var s5 : int := Pic.FileNew ("skele (5).gif")
var s6 : int := Pic.FileNew ("skele (6).gif")
var s7 : int := Pic.FileNew ("skele (7).gif")
var s8 : int := Pic.FileNew ("skele (8).gif")
var s9 : int := Pic.FileNew ("skele (9).gif")
var s10 : int := Pic.FileNew ("skele (10).gif")

process spkm
loop
Music.PlayFile ("Spooked.mp3")
end loop
end spkm
process spooked
loop
Pic.Draw (s1, 270, 20, picMerge)
delay (50)
Pic.Draw (s2, 270, 20, picMerge)
delay (50)
Pic.Draw (s3, 270, 20, picMerge)
delay (50)
Pic.Draw (s4, 270, 20, picMerge)
delay (50)
Pic.Draw (s5, 270, 20, picMerge)
delay (50)
Pic.Draw (s6, 270, 20, picMerge)
delay (50)
Pic.Draw (s7, 270, 20, picMerge)
delay (50)
Pic.Draw (s8, 270, 20, picMerge)
delay (50)
Pic.Draw (s9, 270, 20, picMerge)
delay (50)
Pic.Draw (s10, 270, 20, picMerge)
delay (50)
end loop
end spooked
procedure spooked0
fork spkm
fork spooked
end spooked0

var sp : int := GUI.CreateButton (25, 15, 100, "Spooked", spooked0)

loop
exit when GUI.ProcessEvent
end loop

Thanks! Very Happy

Author:  Insectoid [ Fri Jun 23, 2017 11:25 am ]
Post subject:  RE:Button Help Pleasee

Step one is to rewrite your process as a procedure. Just trust me on that.

From here you have two options. You can use a global variable to store whether the music is running or not. When the procedure is called, it checks this variable. If false, play music. If true, stop music.

The second option is to use to nearly identical buttons. One starts the music, disables itself and enables the second button. The other stops the music, disables itself, and re-enables the first button.

Either will work. Pick whichever sounds more fun.


: