Noob - Timer Question

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

Moderators: Shine, Insert_witty_name

Post Reply
PsyOps
Posts: 5
Joined: Tue Jan 23, 2007 12:50 pm

Noob - Timer Question

Post by PsyOps »

Been teaching myself how to code in Java, then i got sidetracked with Lua.
This is some code i wrote but i cant get it to do what i want.

this is a noob slot machine in the making. This code wont be used like this, its just for me to get an understanding of how lua works. Later i will implement the code in as its needed. Right now I am trying to make square activate the first slot. But i want it to pause randomly between 1-5 seconds. I cant get the timer to work, I'm sure its cause im an idiot and am not understanding something.

Code: Select all

red = Color.new(255,0,0)
green = Color.new(0, 255, 0)
blue = Color.new(0, 0, 255)
white = Color.new(255, 255, 255)

dofile("animLib.lua")

test = SS.new("reel2", "png", 96, 96, "images/")
slot1 = 0
slot2 = 0
slot3 = 0

while true do --begins main body
pad = Controls.read()
if pad:start() then break end

if slot1 == 0 then test:blit(72, 88, 25)  end
if slot2 == 0 then test:blit(192, 88, 25) end
if slot3 == 0 then test:blit(312, 88, 25) end

screen:print(0, 0, "Slot1 = "..slot1.."   Slot2 = "..slot2.."   Slot3 = "..slot3, blue)


if pad:square() then
pullTimer()
end -- end pad:square()

if pad:circle() then
   if (slot1 > 0) and (slot2 > 0) and (slot3 > 0) then
   slot1 = 0
   slot2 = 0
   slot3 = 0
   end
end -- end pad:circle()

function pullTimer()
timer = Timer.new()
x = math.floor(timer:time()/1000)
screen:print(10,10,x, blue)
t = math.random(5)
   if t == x  then
   activatePull()
  timer:reset(0)
  end
end -- end pullTimer()

function activatePull()
   if slot1 == 0 then
   slot1 = pull(0)
   else if slot2 == 0 then
   slot2 = pull(0)
   else if slot3 == 0 then
   slot3 = pull(0)
   end
   end
   end
end --end activatePull()

function pull(p)
p = math.random(127)
return p
end -- end pull()

screen.flip()
screen.waitVblankStart()
end --end main body
I am very new to coding. Being able to make games for my sons psp has peaked my interest.
any help would be appreciated.
Snoochie Boochies
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

Code: Select all

-- Init it up here somewhere
self.Timer = os.clock(),

-- more code WHiletrue and such
	local CHECK_TIME = os.clock() - self.Timer
	if (CHECK_TIME >= 1) then
		self.Timer = os.clock() -- resets the clock
		-- Do stuff here!
	end

PsyOps
Posts: 5
Joined: Tue Jan 23, 2007 12:50 pm

Post by PsyOps »

thats exactly what i did, and it works. thnx
ive run into a small snag though. I think it recycles back to 0 after a certain amount of time. so when i try to subtract from local time, it already recycled to 0.
or something like that.
i just need to figure out when it goes back to 0. I think its 5000 seconds or so.
Snoochie Boochies
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

self.Timer = os.clock() -- resets the clock

If you dont want to reset the clock then dont call that function. Everything should work fine after that
Post Reply