Page 1 of 1

Quad problem

Posted: Mon Aug 08, 2016 11:42 am
by silver108
Hello all,
I was working on a platformer game with love2d,
and as a learning experience I decided to write my own
tools for it : (level editor + animation system) .
I use quads to only display a certain part of a sprite sheet.
But my code isn't working as expected

Code: Select all

function love.load()
  p = {}
  p.x = 50;
  p.y = 50;
  p.sheet = love.graphics.newImage("a.png")
  p.speed = 50;
  
  p.idle = love.graphics.newQuad(0,0,30,45,p.sheet:getDimensions())
  p.run = love.graphics.newQuad(0,0,30,38,p.sheet:getDimensions())
  p.curanim = p.idle
end

function love.update(dt)
if love.keyboard.isDown("right") then
      p.x = p.x + (p.speed * dt)
	  p.curanim = p.run
   end
   if love.keyboard.isDown("left") then
      p.x = p.x - (p.speed * dt)
   end
 
   if love.keyboard.isDown("down") then
      p.y =p.y + (p.speed * dt)
   end
   if love.keyboard.isDown("up") then
     p.y = p.y - (p.speed * dt)
   end

end

function love.draw()
   love.graphics.draw(p.sheet, p.curanim, p.x, p.y)
end


curanim never gets set to p.run in the update function.
Also p.sheet is my spritesheet

Re: Quad problem

Posted: Mon Aug 08, 2016 2:41 pm
by MadByte

Code: Select all

  p.idle = love.graphics.newQuad(0,0,30,45,p.sheet:getDimensions())
  p.run = love.graphics.newQuad(0,0,30,38,p.sheet:getDimensions())
Here is something wrong. Both quads point to the same image in your sheet, the second one is just 7px shorter then the first one.

Re: Quad problem

Posted: Mon Aug 08, 2016 3:11 pm
by zorg
Other than what MadByte said, you only ever change the animation in love.update when the player presses the right key -the first time-, and you don't ever change it back.

Also, that's not how you detect input.

Löve gives you the [wiki]love.keypressed[/wiki] and [wiki]love.keyreleased[/wiki] callbacks, use those to set state, like pressed[key] = true in the former, and pressed[key] = false in the latter; then you can check for the state in love.update.

In my opinion, the only reason you'd ever need to check directly if a key is down in the update callback is if you'd be doing fancy 4-state key detection (with extra states: depressed, pressed, held, released) but that's unnecessary, most of the time.

Re: Quad problem

Posted: Tue Aug 09, 2016 11:24 am
by silver108
Oh thanks mad byte, I was using a pixel measurer application and i didn't notice that
Also zone I was just testing the animations so I quickly just put something up to
test it, but thanks for the links.
Writing this from a tablet so excuse
the grammar