PDA

View Full Version : lua help



jools
July 17th, 2007, 19:56
hey

Im workin on a lua game but for some reson when I put some of my code into a function it crashes

Heres the code im using:
playerx = 64
playery = 210
hspeed = 0

player = Image.load("sprites/player.png")
stageonebac = Image.load("backgrounds/stageone.png")

function movePlayer()
pad = Controls.read()
if pad:start() then
break
end

if pad:left() and hspeed >=-4 then
hspeed = hspeed - 0.3
end

if pad:right() and hspeed <=4 then
hspeed = hspeed + 0.3
end

playerx = playerx + hspeed

if hspeed >= 0 then hspeed = hspeed -0.1
end
if hspeed <= 0 then hspeed = hspeed +0.1
end pad = Controls.read()
if pad:start() then
break
end

if pad:left() and hspeed >=-4 then
hspeed = hspeed - 0.3
end

if pad:right() and hspeed <=4 then
hspeed = hspeed + 0.3
end

playerx = playerx + hspeed

if hspeed >= 0 then hspeed = hspeed -0.1
end
if hspeed <= 0 then hspeed = hspeed +0.1
end
end


while true do
movePlayer()
screen:blit(0,0, stageonebac)
screen:blit(playerx,playery, player)

screen.waitVblankStart()
screen.flip()
end

When I just have this though it works fine:
playerx = 64
playery = 210
hspeed = 0

player = Image.load("sprites/player.png")
stageonebac = Image.load("backgrounds/stageone.png")





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

if pad:left() and hspeed >=-4 then
hspeed = hspeed - 0.3
end

if pad:right() and hspeed <=4 then
hspeed = hspeed + 0.3
end

playerx = playerx + hspeed

if hspeed >= 0 then hspeed = hspeed -0.1
end
if hspeed <= 0 then hspeed = hspeed +0.1
end pad = Controls.read()
if pad:start() then
break
end

if pad:left() and hspeed >=-4 then
hspeed = hspeed - 0.3
end

if pad:right() and hspeed <=4 then
hspeed = hspeed + 0.3
end

playerx = playerx + hspeed

if hspeed >= 0 then hspeed = hspeed -0.1
end
if hspeed <= 0 then hspeed = hspeed +0.1
end
screen:blit(0,0, stageonebac)
screen:blit(playerx,playery, player)

screen.waitVblankStart()
screen.flip()
end

can someone please help :)

-Xandu-
July 17th, 2007, 21:56
Try this, don't break the loop before it even starts:


playerx = 64
playery = 210
hspeed = 0

player = Image.load("sprites/player.png")
stageonebac = Image.load("backgrounds/stageone.png")

function movePlayer()
pad = Controls.read()

if pad:left() and hspeed >=-4 then
hspeed = hspeed - 0.3
end

if pad:right() and hspeed <=4 then
hspeed = hspeed + 0.3
end

playerx = playerx + hspeed

if hspeed >= 0 then hspeed = hspeed -0.1
end
if hspeed <= 0 then hspeed = hspeed +0.1
end

if pad:left() and hspeed >=-4 then
hspeed = hspeed - 0.3
end

if pad:right() and hspeed <=4 then
hspeed = hspeed + 0.3
end

playerx = playerx + hspeed

if hspeed >= 0 then hspeed = hspeed -0.1
end
if hspeed <= 0 then hspeed = hspeed +0.1
end
end


while true do
movePlayer()
screen:blit(0,0, stageonebac)
screen:blit(playerx,playery, player)

if pad:start() then
break
end


screen.waitVblankStart()
screen.flip()
end

jools
July 17th, 2007, 22:51
thanks I forgot about that