What's wrong with this?

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
PjoKing
Prole
Posts: 1
Joined: Tue Oct 13, 2009 3:31 pm

What's wrong with this?

Post 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.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: What's wrong with this?

Post 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.
User avatar
Almost
Prole
Posts: 42
Joined: Fri Aug 21, 2009 8:04 pm
Location: Canada
Contact:

Re: What's wrong with this?

Post 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.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 102 guests