Typing on a phone.
if self.attacking then return end
self.attacking = true
sprite.play_flipbook("#mysprite", "attack", function()
self.attacking = false
end)
Typing on a phone.
if self.attacking then return end
self.attacking = true
sprite.play_flipbook("#mysprite", "attack", function()
self.attacking = false
end)
it returns this following error:
Compilation failed: ‘)’ expected (to close ‘(’ at line 98) near ‘self’
You need to put a )
behind the last end
.
if self.attacking then return end
self.attacking = true
sprite.play_flipbook("#mysprite", "attack", function()
self.attacking = false
end)
thank you, no more error now, however it did not play attack animation. I put the code on the update. While on the input, i put the self.attacking = true.
if action.pressed then
-elseif action_id == hash(“attack”) then
self.attacking = true
end
Put all of the code in on_input
thank you so much, it works now. You are the best
I am wondering, why it is not working after i pressed other animation? for an example, after i play the other animation like walking, then if i press attack again it does not play? I am sorry for asking a lot.
It’s probably because you play another animation which will cause the callback for the first to not get called and self.attacking remains true
You can also check out my struggle with similar problem and a very helpful post here by @TheKing0x9
thank you so much. I will check it.
okay thank you, I will improve on it
hello, i still can not get it work. I am sorry. The attack animation did not played after walking animation is played.
Can you share your code here?
thank you for your response. When i check at the input, the attack id is received, it knows that the attack function are being launched. But the animation did not.
local ATTACK = hash("attack")
ATTACKS = {1, 2, 3, 4}
local function play_attack_anim(self, index)
if not (self.anim == ATTACKS[index]) then
msg.post("#sprite", "play_animation", {id = hash("attack"..ATTACKS[index])})
self.anim = ATTACKS[index]
print("play animation")
end
end
local function check_attacks(self)
if self.completed_attacks < self.scheduled_attacks then
play_attack_anim(self, self.completed_attacks % 3 + 1)
elseif self.completed_attacks == self.scheduled_attacks then
self.completed_attacks = 0
self.scheduled_attacks = 0
end
end
local function play_animation(self, animation)
if self.current_animation ~= animation then
self.current_animation = animation
sprite.play_flipbook("#sprite", animation)
print(animation)
end
end
function init(self)
--acquire input
msg.post(".", "acquire_input_focus")
self.scheduled_attacks = 0
self.completed_attacks = 0
self.anims = nil
self.direction = vmath.vector3(0, 0, 0)
end
function final(self)
end
function update(self, dt)
if self.completed_attacks == 0 and self.scheduled_attacks == 0 then
play_attack_anim(self, 4)
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("animation_done") then
self.completed_attacks = self.completed_attacks + 1
print("attack animation complete")
end
end
function on_input(self, action_id, action, animation)
if action_id == ATTACK and action.pressed then
check_attacks(self)
self.scheduled_attacks = self.scheduled_attacks + 1
print(self.scheduled_attacks, self.completed_attacks)
end
if action.pressed then
if action_id == hash("left") then
self.direction.x = -1
msg.post("#sprite", "play_animation", {id = hash("move-left")})
print("left animation complete")
elseif action_id == hash("right") then
self.direction.x = 1
msg.post("#sprite", "play_animation", {id = hash("move-right")})
print("right animation complete")
end
elseif action.released then
if action_id == hash("left") then
self.direction.x = 0
if vmath.length(self.direction) == 0 then msg.post("#sprite", "play_animation", {id = hash("move-left")}) end
elseif action_id == hash("right") then
self.direction.x = 0
if vmath.length(self.direction) == 0 then msg.post("#sprite", "play_animation", {id = hash("idle-right")}) end
end
end
end
function on_reload(self)
end
Add three " " " around your code, and it will display properly. Right now, all indentation and syntax highlighting got lost, which makes it very hard to read.
okay i use the preformatted text to display the code.
In this block, add self.attacking = true.
Add self.attacking = false here in this block
Replace this by
if action.pressed and not self.attacking then
Same for here.
This should do the trick. Just try and tell.
thank you so much, you really solved my problem. I really am appreciate your help. You are the best!
I suggest that you really analyse the final code and try to understand every detail of it. Why did you have to add the things @TheKing0x9 suggested? What was wrong before? Try to understand these things and you’ll have a little more knowledge about programming game logic the next time you get stuck.
okay I am trying my best thank you