如何建立在爱情的2D行走动画 [英] How to create a walking animation in LOVE 2D
问题描述
所以我想我已经创建取决于我pressed /时的关键是如何来改变人物的形象pressing?
我最终将有一个行走动画发生的历史时,D(或WASD键的)是pressed但他仍然站立的时候,D键刚刚pressed等。所有图像已经创建成功了。
我试过这个,但它没有工作:
函数love.load() 如果love.keyboard.isDown(一),那么
英雄= love.graphics.newImage(/英雄/ 11.png)
ELSEIF love.keyboard.isDown(D),那么
英雄= love.graphics.newImage(/英雄/ 5.png)
ELSEIF love.keyboard.isDown(S),那么
英雄= love.graphics.newImage(/英雄/ fstand.png)
ELSEIF love.keyboard.isDown(W),那么
英雄= love.graphics.newImage(/英雄/ 1.png)
结束功能love.draw() love.graphics.draw(背景)
love.graphics.draw(英雄,X,Y)结束
您必须了解爱情作品。它(非常基本)做到这一点:
love.load() - 调用love.load只有一次,一开始
而真正做 - 循环重复下面的永远(直到游戏结束)
love.update(DT) - 调用love.update()
love.draw() - 调用love.draw()
结束
此模式是如此频繁的循环本身有一个名字 - 这就是所谓的游戏循环 。 p>
您code简化版,工作,因为你正在使用 love.load()
,如果它是游戏圈的一部分,但事实并非如此。这就是所谓的一开始,首先毫秒时左右你的程序,也再没有。
您想要使用 love.load
做负载的图片,和 love.update
来改变他们:
函数love.load()
heroLeft = love.graphics.newImage(/英雄/ 11.png)
heroRight = love.graphics.newImage(/英雄/ 5.png)
heroDown = love.graphics.newImage(/英雄/ fstand.png)
heroUp = love.graphics.newImage(/英雄/ 1.png) 英雄= heroLeft - 玩家开始寻找左
结束功能love.update(DT)
如果love.keyboard.isDown(一),那么
英雄= heroLeft
ELSEIF love.keyboard.isDown(D),那么
英雄= heroRight
ELSEIF love.keyboard.isDown(S),那么
英雄= heroDown
ELSEIF love.keyboard.isDown(W),那么
英雄= heroUp
结束
结束功能love.draw()
love.graphics.draw(背景)
love.graphics.draw(英雄,X,Y)
结束
上面的code具有一定的重复性,可以使用表格被分解了,但我已经离开它简单的目的。
您还会注意到我已经包括在 love.update
功能 DT
参数。这是很重要的,因为你会需要它来确保动画的工作方式相同中的所有计算机(以飞快的速度 love.update
被称为取决于每台计算机上,而 DT
允许你来处理那些)
不过,如果你想要做的动画,你可能会想要使用此动画库或的我自己
。So I was wondering how to change an image of character I've created depending on the key I've pressed/am pressing?
My ultimate going to to have a walking animation occuring when "d" (or any of the wasd keys) is pressed but then he stands still when the "d" key has just been pressed etc. All images have been created already.
I've tried this but it didn't work out:
function love.load()
if love.keyboard.isDown("a") then
hero = love.graphics.newImage("/hero/11.png")
elseif love.keyboard.isDown("d") then
hero = love.graphics.newImage("/hero/5.png")
elseif love.keyboard.isDown("s") then
hero = love.graphics.newImage("/hero/fstand.png")
elseif love.keyboard.isDown("w") then
hero = love.graphics.newImage("/hero/1.png")
end
function love.draw()
love.graphics.draw(background)
love.graphics.draw(hero, x, y)
end
You must understand how LÖVE works. It (very basically) does this:
love.load() -- invoke love.load just once, at the beginning
while true do -- loop that repeats the following "forever" (until game ends)
love.update(dt) -- call love.update()
love.draw() -- call love.draw()
end
This schema is so frequent that the loop itself has a name - it's called The Game Loop.
Your code does't work because you are using love.load()
as if it was part of the game loop, but it isn't. It's called at the beginning, during the first millisecond or so of your program, and never again.
You want to use love.load
do load the images, and love.update
to change them:
function love.load()
heroLeft = love.graphics.newImage("/hero/11.png")
heroRight = love.graphics.newImage("/hero/5.png")
heroDown = love.graphics.newImage("/hero/fstand.png")
heroUp = love.graphics.newImage("/hero/1.png")
hero = heroLeft -- the player starts looking to the left
end
function love.update(dt)
if love.keyboard.isDown("a") then
hero = heroLeft
elseif love.keyboard.isDown("d") then
hero = heroRight
elseif love.keyboard.isDown("s") then
hero = heroDown
elseif love.keyboard.isDown("w") then
hero = heroUp
end
end
function love.draw()
love.graphics.draw(background)
love.graphics.draw(hero, x, y)
end
The code above has certain repetitiveness that can be factored out using tables, but I've left it simple on purpose.
You will also notice that I have included the dt
parameter in the love.update
function. This is important, since you will need it to make sure that animations work the same in all computers (the speed at which love.update
is called depends on each computer, and dt
allows you to cope with that)
Nevertheless, if you want to do animations, you will probably want to use this Animation Lib or my own.
这篇关于如何建立在爱情的2D行走动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!