Page 2 of 2

Re: Create a waking animation through pictures and keypresse

Posted: Mon Feb 20, 2012 2:06 pm
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

Re: Create a waking animation through pictures and keypresse

Posted: Mon Feb 20, 2012 2:43 pm
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

Re: Create a waking animation through pictures and keypresse

Posted: Mon Feb 20, 2012 9:30 pm
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!

Re: Create a waking animation through pictures and keypresse

Posted: Tue Feb 21, 2012 12:21 am
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.

Re: Create a waking animation through pictures and keypresse

Posted: Tue Feb 21, 2012 7:35 am
by tentus
Could you post the images in question please?

Re: Create a waking animation through pictures and keypresse

Posted: Tue Feb 21, 2012 10:01 am
by Prof_Ants
All good. Thank you anyway, I made a really silly error.

Re: Create a waking animation through pictures and keypresse

Posted: Tue Feb 21, 2012 6:23 pm
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.