LOVE中使用Lua进行一键动画

最近我熟悉了LOVE 2D中的Lua,并观看了一些教程,现在正在尝试制作一个简单的游戏。游戏将有一个能够奔跑的角色,并且按下空格键可以进行攻击(用他的剑打击)。按下空格键时,应该遍历所有攻击动画的帧/精灵。但是角色并未经历所有帧,并且它们之间的间隔似乎非常快,即使我将它保持在最小值。

这是我用来处理按键的代码。

self.animations = {
        ['idle'] = Animation{
            frames = {
                self.frames[1]
            },
            interval = 1
        },
        ['attack'] = Animation{
            frames = {
                self.frames[1], self.frames[2], self.frames[3], self.frames[4], self.frames[5], self.frames[6], self.frames[3]
            },
            interval = 0.25
        }
    }

    self.animation = self.animations['idle']
    self.currentFrame = self.animation:getCurrentFrame()

    self.behaviors = {
        ['idle'] = function(dt)

            if love.keyboard.wasPressed('space') then
                self.dy = 0
                self.state = 'attack'
                self.animation = self.animations['attack']
            elseif love.keyboard.wasPressed('up') then
                self.dy = -MOVE_DIST
            elseif love.keyboard.wasPressed('down') then
                self.dy = MOVE_DIST
            else
                self.dy = 0
            end
        end,
        ['attack'] = function(dt)
            self.animation = self.animations['attack']
            if love.keyboard.wasPressed('up') then
                self.dy = -MOVE_DIST
            elseif love.keyboard.wasPressed('down') then
                self.dy = MOVE_DIST
            else
                self.dy = 0
                self.state = 'idle'
                self.animation = self.animations['idle']
            end
        end

这是我的animation类,负责转换效果

Animation = Class{}

function Animation:init(params)
    --self.texture = params.texture
    self.frames = params.frames
    self.interval = params.interval or 0.05
    self.timer = 0
    self.currentFrame = 1

end

function Animation:getCurrentFrame()
    return self.frames[self.currentFrame]
end

function Animation:restart()
    self.timer = 0
    self.currentFrame = 1
end

function Animation:update(dt)
    self.timer = self.timer + dt

    if #self.frames == 1 then
        return self.currentFrame

    else
        while self.timer > self.interval do
            self.timer = self.timer - self.interval

            self.currentFrame = (self.currentFrame + 1) % (#self.frames + 1)

            if self.currentFrame == 0 then
                self.currentFrame = 1
            end
        end
    end
end

请帮忙。我希望我已经正确地提出了问题。先谢谢了。

点赞
用户8267449
用户8267449

不知道如何在按键代码中处理状态,但这也可能是问题的一部分。不管怎样,你的Animation:update 对我来说看起来很奇怪,我真的认为这是问题的核心。有一个简单的功能可供使用,应该可以完美地工作(如果有关于它的问题,请随时问)。

如果您不太熟悉调试器,可以使用[love.graphics.print](https://love2d.org/wiki/love.graphics.print)将一些值打印到屏幕上(例如该值中的self.currentFrame或self.timer), 以实时查看这些值的情况。

function Animation:update(dt)
    self.timer = self.timer + dt/self.interval

    if self.timer >= #self.frames then
        self.timer = 0
    end

    self.currentFrame = Math.floor(self.timer)+1
end

另请注意,“Math.floor(self.timer)+1”不能替换为“Math.ceil(self.timer)”,因为如果self.timer == 0,它将返回0(而lua的数组从1开始)。

编辑: 它可能来自这里:


['attack'] = function(dt)
    self.animation = self.animations['attack']
    if love.keyboard.wasPressed('up') then
        self.dy = -MOVE_DIST
    elseif love.keyboard.wasPressed('down') then
        self.dy = MOVE_DIST
    else
        self.dy = 0
        self.state = 'idle'
        self.animation = self.animations['idle']
    end
end

我不知道你如何处理输入,因为“love.keyboard.wasPressed”不是love函数,但是如果上一个帧没有按下“up”和“down”,状态将返回到“idle”,动画将返回到animations['idle']。

没有更多的代码,我为您做不了更多。

2020-06-09 23:34:39