Quad problem

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
silver108
Prole
Posts: 4
Joined: Mon Aug 08, 2016 11:35 am

Quad problem

Post 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
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Quad problem

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Quad problem

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
silver108
Prole
Posts: 4
Joined: Mon Aug 08, 2016 11:35 am

Re: Quad problem

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

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests