Page 1 of 1

Platformer game: statement error

Posted: Wed Nov 07, 2012 5:21 pm
by Cj1m
Me and some friends are trying to make a platformer game but we have stumbled across a statement error we cannot resolve. Here is the code:

Code: Select all

function love.load()
  imageCharacter = love.graphics.newImage("CharacterPrototype.png")
  charax = 200 
  charay = 200
  if charay = groundy then
  fallspeed = 0
  else fallspeed = 3
  end
  imageGround = love.graphics.newImage("DirtBlock.png")
  groundx = 650
  groundy = 650

  love.physics.setMeter(64) --the height of a meter our worlds will be 64px
  world = love.physics.newWorld(0, 9.81*64, true) --create a world for the bodies to exist in with horizontal gravity of 0 and vertical gravity of 9.81
  
  
  objects = {} -- table to hold all our physical objects
  
  --let's create the ground
  
    --initial graphics setup
  love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
  love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650 with no fullscreen, vsync on, and no antialiasing
end

function love.update(dt)
  world:update(dt) --this puts the world into motion
  
  
   if love.keyboard.isDown("d") then  
   x = x + 50
   end   

   if love.keyboard.isDown("s") then
   y = y + 50     
   end
   
   if love.keyboard.isDown("w") then
   y = y - 50
   end
   
   
   if love.keyboard.isDown("a") then
   x = x - 50
   end 
   
y = y + fallspeed 
 
   
 
  end


function love.draw()

  love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
  love.graphics.polygon(imageGround, groundx, groundy) -- draw a "filled in" polygon using the ground's coordinates
  
  
  
  love.graphics.setColor(255, 255, 255, 255)
  love.graphics.draw(imageCharacter, x, y)

  end
And here is the error:

Syntax error: main.lua:5: 'then' expected near '='

Hope you can help!

Re: Platformer game: statement error

Posted: Wed Nov 07, 2012 6:23 pm
by Robin
In Lua, comparing for equality works with == rather than =, so replace line 5 with:

Code: Select all

if charay == groundy then
Cheers. :)

Also, not related to the error, but you will want to move line 5-8 to love.update, because you'll want to do that check every frame, not just at the start of the game (when groundy is not yet initialised either, so it has the value nil).