[LUVA充满爱的一键式动画

问题描述 投票:0回答:1

我最近在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 = 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

请帮助。我希望我正确地问了这个问题。预先感谢。

animation lua love2d
1个回答
0
投票

真的不知道您如何处理按键代码中的状态,但这也可能是问题的一部分。无论如何,您的Animation:update对我来说看起来很奇怪,我真的认为这是问题的中心。您可以使用一个简单的函数来代替它,它应该可以完美运行(如果您有任何疑问,可以随时询问)。如果您对调试器不太满意,可以使用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(并且lua的数组从1开始,它将返回0) )

© www.soinside.com 2019 - 2024. All rights reserved.