Author Topic: custom_entity and sprite event  (Read 1853 times)

Yosha

  • Newbie
  • *
  • Posts: 37
    • View Profile
custom_entity and sprite event
« on: July 16, 2015, 08:45:01 am »
Hello, like many users here, I made a custom entity that follow the hero (it's the main gameplay of my project since many years I'm working on!) but I have some little troubles with it. It moves, throught stairs, hole or whatever, and I can change the sprite animation, but when I want to came back to normal, after the animation finished, I have an error.

Here is an example : P1 is the hero and P2 the follower

Code: [Select]
function P2:on_ground_below_changed(ground_below)
if ground_below == "hole" then
game:set_value("P2_state", "falling")
P2_Move:stop()
if P2:get_sprite():get_animation() ~= "falling" then
P2:get_sprite():set_animation("falling")
P1:freeze()
end
function P2:get_sprite():on_animation_finished("falling")
game:set_value("P2_state", "free")
P1:unfreeze()
end
end
end

Now there's the error :
Error: Failed to load script 'entities/P2': [string "entities/P2.lua"]:220: unexpected symbol near ':'
Error: In entities/P2: attempt to call a string value

The problem seems to be : function P2:get_sprite():on_animation_finished("falling")   

I have another problem with the jump (it works once and doesn't work after) but we'll see that later !
Thanks for the help!

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: custom_entity and sprite event
« Reply #1 on: July 16, 2015, 08:50:07 am »
It is a syntax error:
Code: (lua) [Select]
function P2:get_sprite():on_animation_finished("falling")should be
Code: (lua) [Select]
local p2_sprite = P2:get_sprite()
function p2_sprite:on_animation_finished("falling")

Yosha

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: custom_entity and sprite event
« Reply #2 on: July 16, 2015, 09:24:21 am »
OK thank you for the quickness! But I tried it and it doesn't work too.
I have another error :
Error: Failed to load script 'entities/P2': [string "entities/P2.lua"]:183: <name> or '...' expected near '"falling"'
Error: In entities/P2: attempt to call a string value

I found a trick but it's not what I want to do, and it would be fine while the animation is no longer that the timer...
Code: [Select]
sol.timer.start(1000, function()
function P2:get_sprite():on_animation_finished("falling")
game:set_value("P2_state", "free")
P1:unfreeze()
end)
« Last Edit: July 16, 2015, 09:27:33 am by Yosha »

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: custom_entity and sprite event
« Reply #3 on: July 16, 2015, 09:58:15 am »
Ooops, sorry, there is still an obvious syntax error. I think you meant:
Code: (lua) [Select]
local p2_sprite = P2:get_sprite()
function p2_sprite:on_animation_finished(animation)
    if animation == "falling" then
        ...
    end
end

Yosha

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: custom_entity and sprite event
« Reply #4 on: July 16, 2015, 12:13:52 pm »
Thank You ! It works now ! And above that, I found out why my Custom entity did only one jump before bugging... I created 2 movement : one for the normal moving (walking and stop), and one for the jump. And after a week of thinking, testing, and headache, I realized that only one was necessary.   :-\

However, I have another question about the diagonal jumper : I saw that the overlapping part is not only the blue bar viewed on the editor, but the green square when you select it(i think it's the bounding box) and when I use custom_entity:set_can_traverse([entity_type], traversable) like this example:
Code: [Select]
P2:set_can_traverse("jumper", function() game:set_value("P2_state", "jumping") end)
the state jumping begins when P2 overlaps the square and not only the jumper (blue bar) itself. So, I wondering if it was a kind of bug from solarus or if it was normal. Therefore I found a solution by changing the P2_state not with the jumper (too bad!) but when the hero state changed. Thanks for all and it the last time I disturb you today !

Christopho

  • Administrator
  • Hero Member
  • *****
  • Posts: 1177
    • View Profile
Re: custom_entity and sprite event
« Reply #5 on: July 16, 2015, 12:34:31 pm »
I'd say that this is a bug, you can report it on https://github.com/christopho/solarus/issues/new

Yosha

  • Newbie
  • *
  • Posts: 37
    • View Profile
Re: custom_entity and sprite event
« Reply #6 on: July 16, 2015, 12:56:19 pm »
Ok, it's done!