I want to have a sound occur when the mouse-controlled sprite touches the brick wall sprite. I've coded in the sound trigger, but it's an "if-then" loop, which means as long as the two sprites are touching, the sound is constantly triggered (it's a single explosion sound (sample taken from 1945's /data folder, for testing purposes) but it sounds like a machine gun, actually, because the condition if-then" is always true).
My assumption is that I have to make the stationary sprite disappear, the instant the mouse-controlled sprite touches it, in order to have the sound trigger only once, correct?
How do I make the sprite vanish, when touched?
Here's my code:
screen (800,600,0,"Collision Test")
rec1={}
rec2={}
rec3={}
rec4={}
rec5={}
rec1.image = loadimage ("data/wall.bmp")
rec2.image = loadimage ("data/sprite.bmp")
rec3.image = loadimage ("data/grass.bmp")
rec4.image = loadimage ("data/sprite.bmp")
rec5.image = loadimage ("data/sprite.bmp")
putimage(300,400,rec1.image)
backcolor (255,255,0)
cls()
redraw()
rec1.x=500
rec1.y=400
putimage(300,400,rec4.image)
backcolor (255,255,0)
cls()
redraw()
rec4.x=550
rec4.y=565
putimage(300,400,rec5.image)
backcolor (255,255,0)
cls()
redraw()
rec5.x=585
rec5.y=565
putimage(300,400,rec3.image)
backcolor (255,255,0)
cls()
redraw()
rec3.x=375
rec3.y=300
backcolor (255,0,0)
mousehide()
grabmouse()
repeat
cls()
key=getkey()
rec2.x = mousex()
rec2.y = mousey()
colour(0, 200, 255)
fillbox(10, 580, 200, 590)
colour(139, 80, 14)
fillbox(150, 110, 650, 500)
putimage (rec1.x,rec1.y,rec1.image)
alphachannel (255)
putimage (rec2.x,rec2.y,rec2.image)
alphachannel (255)
putimage (rec3.x,rec3.y,rec3.image)
alphachannel (255)
putimage (rec4.x,rec4.y,rec4.image)
alphachannel (255)
putimage (rec5.x,rec5.y,rec5.image)
alphachannel (255)
colour(0, 0, 0)
drawtext (350,580,"SCORE: 56000")
redraw()
if imagecoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y) then
-- drawtext (0,0,"Imagecoll detected")
music2 =loadsound ("data/Explosion7.wav")
playsound (music2, 25, 1, 0)
pausemusic(music2)
end
if boxcoll (rec2.x,rec2.y,imagewidth (rec2.image), imageheight (rec2.image), rec1.x, rec1.y, imagewidth (rec1.image), imageheight (rec1.image)) then
drawtext (0,10,"Boxcoll detected")
end
if pixelcoll (rec2.image, rec2.x, rec2.y, rec1.image, rec1.x, rec1.y, 2) then
drawtext (0,20,"Pixelcoll detected")
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,30,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,40,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),-1,0,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,50,side)
end
side = sidecoll (rec2.x,rec2.y,imagewidth (rec2.image),imageheight (rec2.image),0,-1,rec1.x,rec1.y,imagewidth (rec1.image),imageheight (rec1.image))
if side ~= "none" then
drawtext (0,60,side)
end
redraw()
sync()
until key==27
closewindow()