Page 1 of 1
Sprites, Objects the works?
Posted: Thu Feb 04, 2010 7:50 am
by dragonsnare0
Hey I'm just starting off in love working my way around lua
I'm very familiar when it comes to design scripting and creating games in other engines without a problem. I'm trying to figure out how to create a sprite/object to use as my player. a point the right direction would help me out a lot.
Re: Sprites, Objects the works?
Posted: Thu Feb 04, 2010 8:39 am
by Taehl
The simplest way would be to make a table (think of it as an object, in this case), let's call it "player", which stores the player's position, graphic, health, and whatnot.
Code: Select all
player.x = 100
player.y = 200
player.image = love.graphics.newImage("player.png")
player.health = 100
And then draw the player in your love.draw() function:
Code: Select all
function love.draw()
love.graphics.draw( player.image, player.x, player.y, 0, 1, 1, 0, 0) )
end
And there you go - a player with a position, a graphic, and a health value. Add some code to love.update(dt) to make it move around, and you're off to a fine start.
Re: Sprites, Objects the works?
Posted: Thu Feb 04, 2010 8:55 am
by dragonsnare0
you are totally awesome that is exactly what I was looking for.
Re: Sprites, Objects the works?
Posted: Thu Feb 04, 2010 10:30 am
by dragonsnare0
ok cool now I got a movable little smiley face. how about animated sprites and please don't tell me gif images. I'm saying like either using 1 image like a tileset of images for the character with all their animations in it. if it's supported. or how to use multiple external images to create my animation.
I have theory for how it would work with multiple images.
something like
Code: Select all
EndFrame=10
Frame =0
Player.image = love.graphics.newImage("Face"+Frame+".png")
function love.update(dt)
Frame = Frame+1
if Frame > EndFrame Then
Frame =0
end
}
function love.draw()
love.graphics.draw( Player.image, Player.x, Player.y, 3.14, 1, 1, 0, 0)
end
and then have each frame of the animation like Face0.png, Face1.png and so on.
would that work or is there a better method?
don't mind my syntax I'm still learning Lua.
Re: Sprites, Objects the works?
Posted: Thu Feb 04, 2010 2:41 pm
by Robin
Lots of syntax errors indeed.
If I would write the exact thing you were trying to do, I would to it this way:
Code: Select all
function love.load()
EndFrame=10
Frame = 1
Images = {}
for i=1,EndFrame do
Images[i] = love.graphics.newImage("Face"..i..".png") -- + doesn't work on strings
end
Player = {x = 300, y = 200}
end
function love.update(dt)
Frame = Frame % EndFrame + 1
end
function love.draw()
love.graphics.draw(Images[Frame], Player.x, Player.y, 3.14, 1, 1, 0, 0)
end
Disclaimer: completely untested, blablabla
Re: Sprites, Objects the works?
Posted: Thu Feb 04, 2010 10:57 pm
by dragonsnare0
oh ok so I did sort of have an idea how it would work. yes putting it in a array would make more since.
so adding variables to strings is kinda like php syntax.