Lua Newbie Question

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
fade
Posts: 2
Joined: Sat Oct 01, 2005 4:37 am

Lua Newbie Question

Post by fade »

I'm pretty new to lua and i've enjoyed messing around with it so far, but i need some seasoned help..

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&#58;down&#40;&#41; then
   y = y + 5
   if y > 230 then
      y = 230
   end
end


if pad&#58;left&#40;&#41; then
   x = x - 5
   if x < -4 then
      x = -4
   end
   
end


if pad&#58;right&#40;&#41; then
   x = x + 5
   if x > 440 then
      x = 440
   end
   
end
-- END OF CONTROLS



-- FIRE
	
 if pad&#58;cross&#40;&#41; then

fire&#40;&#41;;

end





if pad&#58;start&#40;&#41; then  -- press start to restart
      break
   end


end
LuMo
Posts: 410
Joined: Sun Aug 21, 2005 2:45 am
Location: Austria
Contact:

Post by LuMo »

add a variable where you count the animation frames (from 0 to 5) when its 5 set it back to zero
further you may add a boolean variable which tells you if you pressed the button (if pressed another variable will be counted up)
note that psp uses 60fps (so if the variable reaches 60, one second passed by....)

the rest is straight forward

greets
lumo
"Good artists copy, great artists steal."
Pablo Picasso
go2lumo.com
fade
Posts: 2
Joined: Sat Oct 01, 2005 4:37 am

Post by fade »

Thanks a lot
Post Reply