Create a waking animation through pictures and keypresses

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.
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

Well, if you don't mind :P It's messy and it's inconsistent but what I'm looking to do it assign all directions to be like the hero = walking_up assignment and then eventually have the single image pose when the button is released. but at the moment, the hero = walking_up assignment isn't working as expected...

Code: Select all

function love.load()
    
    require("AnAL")
    
    background = love.graphics.newImage("bg.png")
    
    local imgup  = love.graphics.newImage("/hero/heroanimeup.png")
    walking_up = newAnimation(imgup, 71, 100.5, 0.1, 3)
    local imgdown  = love.graphics.newImage("/hero/heroanimedown.png")
    walking_down = newAnimation(imgdown, 71, 100.5, 0.1, 3)
    local imgright  = love.graphics.newImage("/hero/heroanimeright.png")
    walking_right = newAnimation(imgright, 71, 100.5, 0.1, 3)
    local imgleft  = love.graphics.newImage("/hero/heroanimeleft.png")
    walking_left = newAnimation(imgleft, 71, 100.5, 0.1, 3)
    
    heroRightS  = love.graphics.newImage("/hero/5.png")
	heroLeftS = love.graphics.newImage("/hero/11.png")
    heroDownS  = love.graphics.newImage("/hero/fstand.png")
    heroUpS    = love.graphics.newImage("/hero/1.png")
      
    hero = heroDownS -- the player starts looking to the left

	
	x = 300
	y = 400
	speed = 100	
end

function love.update(dt)

    walking_up:update(dt)

    -- keyboard actions for our hero
	if love.keyboard.isDown("a") then
		x = x - (speed*dt)
		hero = heroLeftS
	elseif love.keyboard.isDown("d") then
		x = x + (speed*dt)
		hero = heroRightS
	end
	
	if love.keyboard.isDown("w") then
		y = y - (speed*dt)
		hero = walking_up
	elseif love.keyboard.isDown("s") then
		y = y + (speed*dt)
		hero = heroDownS
	end
	
end

function love.draw()
	
	love.graphics.draw(background)
	love.graphics.draw(hero, x, y)
	hero:draw(100, 100) 
	
end
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Create a waking animation through pictures and keypresse

Post by tentus »

The problem is that you're mixing an matching without the matching. You have both animations and still images, but your drawing code doesn't differentiate, it just tries to do both.

I would recommend making everything an animation. Yes, even the still images- a 1 frame animation isn't going to destroy your performance, but it WILL make your code way simpler.

Code: Select all

function love.draw()
   love.graphics.draw(background)
   if hero:type() == "Image" then
      love.graphics.draw(hero, x, y)
   else
      hero:draw(100, 100)
   end
end
becomes...

Code: Select all

function love.draw()
   love.graphics.draw(background)
   hero:draw(x, y)
end
Kurosuke needs beta testers
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

Thanks for the tip! I've taken it on board. At the moment though, I'm still running into an issue where when I press I direction it throws a "attempt to call method 'draw' (a nil value)" error regarding this line

Code: Select all

hero:draw(x, y)
. Any ideas? I'm sure I'm missing something very basic.

Edit: Nevermind, I got it! Thanks again!
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

I seem to be running into an issue with only one of the animations working. Here's the stuff:

Code: Select all

    function love.load()
       
        require("AnAL")
              
        local imgup  = love.graphics.newImage("/hero/heroanimeup.png")
        walking_up = newAnimation(imgup, 71, 101, 0.1, 3)
        local imgdown  = love.graphics.newImage("/hero/heroanimedown.png")
        walking_down = newAnimation(imgdown, 71, 101, 0.1, 3)
        local imgright  = love.graphics.newImage("/hero/heroanimeright.png")
        walking_right = newAnimation(imgright, 71, 101, 0.1, 3)
        local imgleft  = love.graphics.newImage("/hero/heroanimeleft.png")
        walking_left = newAnimation(imgleft, 71, 101, 0.1, 3)

         
        hero = walking_down

       
       x = 300
       y = 400
       speed = 100   
    end

    function love.update(dt)

        walking_up:update(dt)

        -- keyboard actions for our hero
       if love.keyboard.isDown("a") then
          x = x - (speed*dt)
          hero = walking_left
       elseif love.keyboard.isDown("d") then
          x = x + (speed*dt)
          hero = walking_right
       end
       
       if love.keyboard.isDown("w") then
          y = y - (speed*dt)
          hero = walking_up
       elseif love.keyboard.isDown("s") then
          y = y + (speed*dt)
          hero = walking_down
       end
       
    end

    function love.draw()
       
       hero:draw(x, y)
       
    end
It's weird, only the top

Code: Select all

local imgup  = love.graphics.newImage("/hero/heroanimeup.png") walking_up = newAnimation(imgup, 71, 101, 0.1, 3)
seems to work. If I swap out the .png it then, is the only one that works. The rest only "draw" the first frame.
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Create a waking animation through pictures and keypresse

Post by tentus »

Could you post the images in question please?
Kurosuke needs beta testers
Prof_Ants
Prole
Posts: 14
Joined: Mon Feb 20, 2012 9:04 am

Re: Create a waking animation through pictures and keypresse

Post by Prof_Ants »

All good. Thank you anyway, I made a really silly error.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Create a waking animation through pictures and keypresse

Post by bartbes »

In case anyone wonders how it has been solved (it is generally viewed as good manners to post a solution to your problem if you find it yourself), only the up animation got updated in love.update.
Post Reply

Who is online

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