Page 1 of 1

What's wrong with this?

Posted: Tue Oct 13, 2009 3:38 pm
by PjoKing
I'm using love for a few weeks, and I'm making some sort of Alien Invader.
function load()

gg = love.graphics.newImage("greenguy.png")
rg = love.graphics.newImage("redguy.png")

x = 200
y = 400

x2 = 200
y2 = 10

speed = 200

fl = false
fx = true

go = love.graphics.newImage("gameover.png")

end

function True()

if speed ~= 0 then
if love.keyboard.isDown(love.key_right) then
x = x + (speed * dt)
elseif love.keyboard.isDown(love.key_left) then
x = x - (speed * dt)
end

if 0 then
y2 = y2 + (speed * dt)
end
end

if x == x2 and y == y2 then
speed = speed - 200
end

if speed == 0 then
function draw()

love.graphics.draw(go, 200, 400)
end
end

end

function draw()

love.graphics.draw(gg, x, y)
love.graphics.draw(rg, x2, y2)

end


Okay, now, what's wrong with this?
before i used the speed 200 and 0 thing to see if the player is game over, everything just moved and everything.
But i wanted a game-over function and I made it, so that if the redguy and greenguy(player) are at the same place.
Now nothing is moving anymore, and nothing's happening.

Re: What's wrong with this?

Posted: Tue Oct 13, 2009 4:20 pm
by Robin
First: you use a function called True(), I think you mean update() there.
Second: you redefine the function draw() every time speed==0. That's... not very efficient. It would be better to just set a flag there, as in:

Code: Select all

if speed == 0 then
  gameover = true
end
[...]
function draw()
if gameover then
    love.graphics.draw(go, 200, 400)
else
    love.graphics.draw(gg, x, y)
    love.graphics.draw(rg, x2, y2)
end
Third:

Code: Select all

if 0 then
I'm not sure why that's there -- if you wanted to comment out the code: 0 evaluates as true.

Re: What's wrong with this?

Posted: Wed Oct 14, 2009 3:01 am
by Almost
Robin said most of it. I'm just going to elaborate and say:
if X will evaluate true for all values of X other than false and nil.

It's pretty convenient for testing if a variable has a value. 1 and 0 are both values, so they're both true.