Change sprite animation speed with a Behavior

For developers using the Construct 2 Javascript SDK

Post » Sun Jan 22, 2017 6:24 pm

Hi,
I'm making a behavior that's supposed to be attached to Sprite objects.
I want that behavior to set the animation speed of the sprite once that sprite is created.
And this is what I do in the behavior:
Code: Select all
   behinstProto.onCreate = function()
   {
      cr.plugins_.Sprite.prototype.acts.SetAnimSpeed.call(this.inst,0);
   };


Unfortunately that does nothing, because apparently the sprite object's instantiation/initialization comes after behinstProto.onCreate() and override my setting.
One solution is to put it in behinstProto.tick() but I don't want to add initialization stuff in the tick function.

One other hackish solution:
Code: Select all
   behinstProto.onCreate = function()
   {
      var self = this.inst;
      setTimeout(function(){
         cr.plugins_.Sprite.prototype.acts.SetAnimSpeed.call(self,0);
      },0);
   };

Do you have a better solution ?
Thanks.
B
21
S
10
G
3
Posts: 50
Reputation: 3,271

Post » Mon Jan 23, 2017 7:31 pm

Using a boolean to indicate the first tick and then doing it in the tick function is a cleaner solution to me.

Code: Select all
behinstProto.onCreate = function()
{
   this.firstTick = true;
};

behinstProto.tick = function()
{
   if (this.firstTick)
   {
      this.firstTick = false;
      cr.plugins_.Sprite.prototype.acts.SetAnimSpeed.call(this.inst,0);
   }
};
B
94
S
33
G
117
Posts: 5,392
Reputation: 75,345


Return to Javascript SDK

Who is online

Users browsing this forum: No registered users and 0 guests