My question...I've written a simple test script based off the smiley animation tutorial. You can move the smiley about the screen in the script and play a sound when the cross button is pressed. This is easy enough, but the problem is that i've noticed that if you keep your finger on the button it plays the sound over and over again continously, really quickly, which sounds horrible. Whereas i only want it to play the sound once, even if the button is held down. And also after letting the button back up, work again only once with each button press. I'd also like to do this with some frames of animation, have lua display maybe 4 or 5 frames of animation for a sprite upon pressing a button, but NOT repeat them if the button is held down. Does lua have a timer maybe to let it play for perhaps half a second and then stop? Is a timer even the best method to do this?
My script is below, any help is much appreciated. :)
Code: Select all
-- Activate USB Mode
System.usbDiskModeActivate()
-- load sounds
smileyfiresound = Sound.load("DATA/SOUNDS/blam.wav")
-- Load images
smiley = Image.load("DATA/IMAGES/smiley.png")
-- Load Backgrounds
background = Image.load("DATA/BACKGROUNDS/background.png")
-- Set Variables
fire_pressed = false
smiley_x_pos = 200
smiley_y_pos = 100
-- Set Functions
function fire()
smileyfiresound:play()
end
-- Set starting X/Y positions
x = smiley_x_pos
y = smiley_y_pos
while true do
screen:blit(0, 0, background, 0, 0, background:width(), background:height(), false)
screen:blit(x, y, smiley)
screen.waitVblankStart()
screen.flip()
-- CONTROLS
pad = Controls.read()
if pad:up() then
y = y - 5
if y < -4 then
y = -4
end
end
if pad:down() then
y = y + 5
if y > 230 then
y = 230
end
end
if pad:left() then
x = x - 5
if x < -4 then
x = -4
end
end
if pad:right() then
x = x + 5
if x > 440 then
x = 440
end
end
-- END OF CONTROLS
-- FIRE
if pad:cross() then
fire();
end
if pad:start() then -- press start to restart
break
end
end