sprite:set_animation bug ?

Started by Realguigui, January 17, 2017, 05:45:42 PM

Previous topic - Next topic
Hello. Sorry for my english but I'm french.

At the almost end of the code below, I use the function "sprite:set_animation" in the line : entity:get_sprite():set_animation("entree",traversage()). In the documentation, I'm reading that the second parameter may be a "function that will be called when the animation finishes". Sadly, the animation and the function start at the same millisecond (I put 2 print os.clock() to verify that). My animation have 9 steps with 100 ms of delay for each.
Well, bug or I forgot something ?

Code of the custom entitie:
-- Lua script of custom entity mur_illusion.
-- This script is executed every time a custom entity with this model is created.

local entity = ...
local game = entity:get_game()
local map = entity:get_map()
-- On recupere le hero dans une variable locale
local hero = map:get_hero()

-- variable qui contient l'autre custom entity qui correspond à l'autre coté du mur illusion
-- on ne l'affecte pas car on ne sait pas s'il est déjà créé au moment de la création de cette entité
local autre_mur = nil

local traversage_en_cours = false

--****************************************************************************************************
-- EVENEMENT : A LA CREATION DE L'ENTITE MUR ILLUSION
--****************************************************************************************************
function entity:on_created()
  -- RECUPERATION DE L'AUTRE MUR
  -- On recupere le préfixe de notre entité (partie à gauche du dernier "_")
  -- On cherche la position du "_" suivi d'un chiffre
  local index = string.find(entity:get_name(),"_%d")
  -- On recupere les lettres du nom de notre variable de la 1ere position jusqu'a la valeur de l'index pour obtenir notre prefixe
  local prefixe = string.sub(entity:get_name(),1,index)
  -- Si le mur opposé existe (s'il y a 2 entités sur la carte qui ont le meme préfixe)
  if map:get_entities_count(prefixe) == 2 then
    -- On recupere le mur de sortie opposé grace au nommage formalisé
    -- pour chaque entité trouvé sur la map qui ont le meme préfixe
    for found_entity in map:get_entities(prefixe) do
      -- Si cette entité n'est pas l'entité actuelle alors
      if found_entity ~= entity then
        -- On a trouvé notre autre entité et on l'affecte à notre var
        autre_mur = found_entity
       
        -- On attend un instant pendant que le code continue de s'executer que les entitées et leur fonctions soient initialisées pour ...
        sol.timer.start(100, function()
          -- ... transmettre notre entité à l'autre mur au cas ou il ne nous ai pas récupéré au moment de sa creation car créé en 1er
          autre_mur:set_mur_oppose(entity)
          return false
        end)

      end     
    end
  end


  -- On initialise certains parametres que l'on a pas pu initialisés avec l'editeur
  -- On rend l'entité intraversable
  entity:set_traversable_by(false)

  -- On parametre un test de collision de type "facing" avec lancement de la fonction hero mur interaction
  entity:add_collision_test("facing",hero_mur_interaction)

end

--****************************************************************************************************
-- FONCTION EXTERNE QUI PERMET DE PARAMETRER LE MUR OPPOSE
--****************************************************************************************************
function entity:set_mur_oppose(other_entity)
  autre_mur = other_entity
end


--****************************************************************************************************
-- FONCTION LANCEE LORSQUE LE HERO ENTRE EN CONTACT "DE FACE" AVEC LE MUR ILLUSION (TEST DE COLLISION)
--****************************************************************************************************
function hero_mur_interaction()
  -- Si le hero pousse le mur
  if hero:get_animation()=="pushing" and traversage_en_cours == false then

    -- On signale qu'il y a traversage en cours pour eviter que le processus se relance immédiatement
    traversage_en_cours = true

    -- On freeze le hero
    hero:freeze()

    -- On rend invisible le hero
    hero:set_visible(false)

    print ("avant animation: " .. os.clock())

    -- On lance l'animation d'entrée dans le mur
    entity:get_sprite():set_animation("entree",traversage())

  end
   
end



function traversage()

  print ("fonction traversage: " .. os.clock())
 
  -- On remets l'animation "normal" sur le mur illusion
  entity:get_sprite():set_animation("normal")

  -- On lance l'animation "sortie"
   
  -- On recupere les coordonnées qui font face au mur opposé
  local x,y,layer = autre_mur:get_facing_position()
 
  -- Selon la  direction du mur ont ajoute quelques pixels dans la bonne direction pour ne pas se retrouver coincé dans le mur
  if autre_mur:get_direction() == 0 then
      -- On déplace le hero au dela du mur opposé
      hero:set_position(x+10,y,layer)
  end

  if autre_mur:get_direction() == 1 then
      -- On déplace le hero au dela du mur opposé
      hero:set_position(x,y-10,layer)
  end

  if autre_mur:get_direction() == 2 then
      -- On déplace le hero au dela du mur opposé
      hero:set_position(x-10,y,layer)
  end

  if autre_mur:get_direction() == 3 then
      -- On déplace le hero au dela du mur opposé
      hero:set_position(x,y+30,layer)
  end

  -- On defreeze le hero
    hero:unfreeze()

  -- On rend visible le hero
    hero:set_visible(true)

  -- On signale que le mur est de nouveau traversable
  traversage_en_cours = false

end



Debug Console:(I tried 2 times to go throught the wall)
Info: Solarus 1.5.1
Info: Opening quest 'C:/Users/atic/Google Drive/old school'
Info: Sound volume: 100
Info: Music volume: 100
Info: Joypad support enabled: true
Info: 2D acceleration: no
Info: Turbo mode: no
Warning: Cannot use quest size 320x240: this quest only supports 256x224 to 256x240. Using 256x224 instead.
Info: Video mode: normal
Info: LuaJIT: yes (LuaJIT 2.0.3)
This is a sample quest for Solarus.
Info: Language: fr
Info: Lua console: yes
Info: Simulation started
avant animation: 14.907
fonction traversage: 14.907
avant animation: 19.33
fonction traversage: 19.33
Info: Simulation finished


Thanks for your help.

With
Code (lua) Select
entity:get_sprite():set_animation("entree",traversage())
you are calling the function "traversage", instead of passing a value of type function.

So you should just do
Code (lua) Select
entity:get_sprite():set_animation("entree",traversage)
like you correctly did above with
Code (lua) Select
entity:add_collision_test("facing",hero_mur_interaction)

Thank you. it's work perfectly.
Sorry for this message due to inattention problem. Maybe you could move it in the development section of this forum

No problem, and welcome on the forum!