newb question

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
crazzykid
Prole
Posts: 2
Joined: Fri May 06, 2011 3:56 pm

newb question

Post by crazzykid »

Hi All,

Just started playing around with love to see how it all works and im quiet impressed.
Though looking through the wiki and forum I cant seem to find my answer.

Im trying to animate the character when he walks. I have 8 images per direction .
I looked at AnAL but i dont think it will do what i want.

Code: Select all

function love.load()
   player = love.graphics.newImage("images/soldier/walking s0000.bmp")
   player_x = 100
   player_y = 50
   player_speed = 100
   i = 0
   love.graphics.setBackgroundColor(110,80,52)
   lastkey = "nothing"
   keynow = "nothing"
end

function love.keypressed(key)
   keynow = key
end

function love.keyreleased(key)
   lastkey = key
end

function love.update(dt)
   key_Right = love.keyboard.isDown("right")
   key_Left = love.keyboard.isDown("left")
   key_Down = love.keyboard.isDown("down")
   key_Up = love.keyboard.isDown("up")

   if key_Right then
      player = love.graphics.newImage("images/soldier/walking e0000.bmp")
      player_x = player_x + (player_speed * dt)
   elseif key_Left then
      player = love.graphics.newImage("images/soldier/walking w0000.bmp")
      player_x = player_x - (player_speed * dt)
   elseif key_Up then
      player = love.graphics.newImage("images/soldier/walking n0000.bmp")
      player_y = player_y - (player_speed * dt)
   elseif key_Down then
      player = love.graphics.newImage("images/soldier/walking s0000.bmp")
      player_y = player_y + (player_speed * dt)
   end
    if love.keyboard.isDown( "escape" ) then
        love.event.push( "q" )
    end
end

function love.draw()
   love.graphics.draw(player, player_x, player_y)
   love.graphics.print("FPS: "..love.timer.getFPS(), 10, 20)
   love.graphics.print(i, 10,40)
   love.graphics.print("key Now: " .. keynow, 10, 60)
   love.graphics.print("Last key: " .. lastkey, 10, 80)
end
As you can see the character image will change depending on up,down,left,right but i have 8 images to make up per direction which will animate the character walking. Im not sure how to go about this if you understand what I mean.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: newb question

Post by Ensayia »

THe first problem I see is that you are creating a new image every time you move. use love.graphics.newImage() once to create the image, then use it the correct way. Here's a modified version of your code (changes in bold):

NOTE: You will want to use some other image format that BMP. Bitmaps are unompressed and take up a LOT of space. I suggest PNG.

NOTE: Learn about Lua tables. They will help you manage your variables and are one of the most powerful aspects of Lua.

function love.load()
player_left = love.graphics.newImage("images/soldier/leftimg.bmp")
player_right = love.graphics.newImage("images/soldier/rightimg.bmp")
player_up = love.graphics.newImage("images/soldier/upimg.bmp")
player_down = love.graphics.newImage("images/soldier/downimg.bmp")
player_x = 100
player_y = 50
player_speed = 100
player_image = player_right -- choosing one for default
i = 0
love.graphics.setBackgroundColor(110,80,52)
lastkey = "nothing"
keynow = "nothing"
end

function love.keypressed(key)
keynow = key
end

function love.keyreleased(key)
lastkey = key
end

function love.update(dt)
key_Right = love.keyboard.isDown("right")
key_Left = love.keyboard.isDown("left")
key_Down = love.keyboard.isDown("down")
key_Up = love.keyboard.isDown("up")

if key_Right then
player = player_right
player_x = player_x + (player_speed * dt)
elseif key_Left then
player = player_left
player_x = player_x - (player_speed * dt)
elseif key_Up then
player = player_up
player_y = player_y - (player_speed * dt)
elseif key_Down then
player = player_down
player_y = player_y + (player_speed * dt)
end
if love.keyboard.isDown( "escape" ) then
love.event.push( "q" )
end
end

function love.draw()
love.graphics.draw(player, player_x, player_y)
love.graphics.print("FPS: "..love.timer.getFPS(), 10, 20)
love.graphics.print(i, 10,40)
love.graphics.print("key Now: " .. keynow, 10, 60)
love.graphics.print("Last key: " .. lastkey, 10, 80)
end
User avatar
Lafolie
Inner party member
Posts: 809
Joined: Tue Apr 05, 2011 2:59 pm
Location: SR388
Contact:

Re: newb question

Post by Lafolie »

Ensayia: try the

Code: Select all

 tag :P

Here are a few pages that might help you:

[url]http://lua-users.org/wiki/TablesTutorial[/url]
[url]http://www.lua.org/pil/2.5.html[/url]
[url]http://www.kb.iu.edu/data/aekl.html[/url] - PNG files are great because they are so versatile. You can have a full-color PNG or an indexed, smaller file. They support multiple levels of alpha blending (transparency) and are -visually- uncompressed but carry a much smaller footprint than a BMP. I always used PNG files for graphics. Or maybe TGA, but they're not too dissimilar. TGA's can be trickier to work with at times.
Do you recognise when the world won't stop for you? Or when the days don't care what you've got to do? When the weight's too tough to lift up, what do you? Don't let them choose for you, that's on you.
crazzykid
Prole
Posts: 2
Joined: Fri May 06, 2011 3:56 pm

Re: newb question

Post by crazzykid »

sweet , ill look into lua tables now =)...

cheers for the help on correct way with drawing the image.
User avatar
Ensayia
Party member
Posts: 399
Joined: Sat Jun 12, 2010 7:57 pm

Re: newb question

Post by Ensayia »

Sorry about the code tag, it got screwed up somehow.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 1 guest