can anybody help me to understand this animation 2d code in lua?

189 views Asked by At

i am a beginner as you can see and i've been following a video tutorial on defold with lua about 2D animation since then i've been asking me some question about this code, why the local variable currentAnimation is equal to 0 a then to 1 on the code, then about self.currentAnimation, as i understand currentAnimation is a method because is an action which is called through self so the object can move right?

local currentAnimation = 0

function init(self)
    msg.post(".", "acquire_input_focus")
end

function final(self)
end

function update(self, dt)

end

function on_message(self, message_id, message, sender)
end

function on_input(self, action_id, action)

if action_id == hash("right") and action.pressed == true then
    if self.currentAnimation == 1 then
        msg.post("#sprite", "play_animation", {id = hash("runRight")})
        self.currentAnimation = 0
    else 
        msg.post("#sprite", "play_animation", {id = hash("idle")})
        self.currentAnimation = 1
    end
end

if action_id == hash("left") and action.pressed == true then
    if self.currentAnimation == 1 then
        msg.post("#sprite", "play_animation", {id = hash("runLeft")})
        self.currentAnimation = 0
    else
        msg.post("sprite", "play_animation", {id = hash("idle")})
        self.currentAnimation = 1
    end
end

end

What steps should i follow so i can change the code in a way so while i press the right arrow from the keyword the "hero" moves and when i stop pressing the "hero" stops moving because with this code it doesn't stop moving until i press again the same button, btw the second chunk of code did it myself following the first one.

Now i got another question, i want to make it jump when i pressed a designated button:

if action_id == hash("jump") then
    if action.pressed then
        msg.post("#sprite", "play_animation", {id = hash("heroJump")})
        self.currentAnimation = 0
    else
        msg.post("#sprite", "play_animation", {id = hash("idle")})
    end

end

With this code it doesn't jump, i have tried other code but act like a loop making the jumping animation over a over, i just want it to jump every time i pressed the designated button.

1

There are 1 answers

4
Dimitry On BEST ANSWER

The currentAnimation is surely not a method, it is a field of a table that happens to be named self.

It is anyone's guess what purpose is achieved with the line local currentAnimation = 0, it is never used afterwards.

Apparently, it seems that the code you've provided is used to describe behavior of an object (in truth, lua table). According to manual in defold framework you implement behaviors using message passing between different objects and by subscribing listeners to event relevant to your object. The init, final,update, on_message and, importantly, on_input are all event handlers you define for particular events. The game engine then calls those whenever it decides to do so.

While handling event of pressed button, your object uses the line

msg.post("#sprite", "play_animation", {id = hash("runRight")})

to send message to engine indicating that it should draw something and probably perform some behaviors defined elsewhere.

The above code implements character as a simple finite state automata. currentAnimation is a variable indicating current state, 1 is for standing still, 0 is for running. The code inside if operator handles the transition between states. The way you've currently done it, it'll take two keypresses to change direction of running.

Your on_input event handler receives action table, which describes the event, it then filters events handling only the right button pressed down if action_id == hash("right") and action.pressed == true then (and your addition checks for left button). According to documentation, you can also check for button being released by checking field action.released. If you want the character to stop, you should add corresponding branch in the event handler. They have a bunch of examples there. You can combine those to implement behavior you want.