Page 1 of 1

<Newbie> Error when using IF statement.

Posted: Tue Jan 31, 2017 1:19 am
by Mapyo
I cannot figure this out... Am I blind?



Code:

Code: Select all

function love.load()
	pj = 0
	px = 30 -- Start Pos
	py = 60 -- Start Pos
	phygrav = 0.50  -- Gravity Pull
	phyground = 61  -- Ground Location
	pgraphic = love.graphics.newImage("p_graphic.bmp")

end

function love.update(dt)
	if love.keyboard.isDown("a") then
	px = px - 0.70
	elseif love.keyboard.isDown("d") then
	px = px + 0.70
	elseif love.keyboard.isDown("w") then
	print "jump attempt"
		if pj = 0 then   <------------ Problem Starts Here
			py = py - 6
			pj = 1
		end
	end
	
	if py < 60 then
		py = py - phygrav
	elseif py >= 60 then
		pj = 0
	end
	
	
end

function love.draw()
love.graphics.draw(pgraphic, px, py)
end




Error: Syntax error: main.lua:20: 'then' expected near '='

stack traceback:
[C]: at 0x7ff82aed3af0
[C]: in function 'require'
[string "boot.lua"]:429: in function <[string "boot.lua"]:275>
[C]: in function 'xpcall'

Re: <Newbie> Error when using IF statement.

Posted: Tue Jan 31, 2017 4:19 am
by Positive07
= is meant for assignment, use == for comparison

Code: Select all

function love.load()
   pj = 0
   px = 30 -- Start Pos
   py = 60 -- Start Pos
   phygrav = 0.50  -- Gravity Pull
   phyground = 61  -- Ground Location
   pgraphic = love.graphics.newImage("p_graphic.bmp")

end

function love.update(dt)
   if love.keyboard.isDown("a") then
   px = px - 0.70
   elseif love.keyboard.isDown("d") then
   px = px + 0.70
   elseif love.keyboard.isDown("w") then
   print "jump attempt"
      if pj == 0 then   <------------ Solution Here
         py = py - 6
         pj = 1
      end
   end
   
   if py < 60 then
      py = py - phygrav
   elseif py >= 60 then
      pj = 0
   end
   
   
end

function love.draw()
love.graphics.draw(pgraphic, px, py)
end

Re: <Newbie> Error when using IF statement.

Posted: Tue Jan 31, 2017 4:37 am
by Mapyo
Thank's so much!