Jump to content

Animation working when he want


brainbass
 Share

Recommended Posts

Hi,

I use phaser 2.6.1.

I have created an animation. This work fine but sometimes this not starting. I dont know why.

 

In create() function i add animations for my spritesheet:

this.car = game.add.sprite(game.world.centerX,500, 'car');
this.car.animations.add('mover',[0,1,2,3],1000, true);
this.car.fixedToCamera = true;

 

When overlap i start the function for animate this

game.physics.arcade.overlap(this.car, this.borders, this.canMove, null, this);

 

This is my function

},canMove: function(car, objet){
	if(crash == 0){
		crash = 1;
		_this.car.animations.play("mover");
                console.log("fire")
		setTimeout(function(){
			_this.car.animations.stop();
			_this.car.frame = 0;
			crash = 0;
		},1000)
	}
}

 

When the car overlap a border, this function is start.

The console.log() show but the animation suddenly not start. I can restart a lot of time my page, suddenly the animation not starting but the console.log() is show.

 

Thanks for your help

 

Link to comment
Share on other sites

Hello!

Can you check the contents of "_this.car" using "console.log(_this.car)", and see if it's a valid object?

And, could you also post the code part where you initialize the "_this" variable? There may be some issue where it doesn't correctly point to the "this" object, so your code may not find the "car" object with the animations set up.

Link to comment
Share on other sites

Thanks all. I have found my issue.

 

The issue is because i have first initialized my car with :

this.car = game.add.sprite(game.world.centerX,500, 'car');
this.car.animations.add('mover',[0,1,2,3],1000, true);
this.car.fixedToCamera = true;
_this = this

After in my function i call directly my car with _this

I used _this because inside setTimeout(), this property is no equal of this of document

I dont know why but if i initialize first with this and after i call with _this this is not working fine. Also for resolve my issue i have starting with _this directly.

_this = this;
_this.car = game.add.sprite(game.world.centerX,500, 'car');
_this.car.animations.add('mover',[0,1,2,3],1000, true);
_this.car.fixedToCamera = true;
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...