Jump to content

Animation is not work


san40511
 Share

Recommended Posts

AT.Actors = AT.Actors || {};AT.Actors.Player = function (game,x,y) {    Phaser.Sprite.call(this, game, x,y,'player');    this.game = game;    this.RUNNING_SPEED = 200;    this.JUMPING_SPEED = 500;    this.customParams = {};    this.cursors = this.game.input.keyboard.createCursorKeys();    this.createPlayer();};AT.Actors.Player.prototype = Object.create(Phaser.Sprite.prototype);AT.Actors.Player.prototype.constructor = AT.Actors.Player;AT.Actors.Player.prototype.createPlayer = function () {    this.anchor.setTo(.5,.5);    //create player    this.game.physics.arcade.enable(this);    this.customParams = {};    this.body.collideWorldBounds = true;    this.body.setSize(10,28,0,0);    this.game.camera.follow(this);    this.initAnimations();};AT.Actors.Player.prototype.initAnimations = function () {    this.animations.add('walking', [        "p2_walk01.png","p2_walk02.png","p2_walk02.png",        "p2_walk04.png","p2_walk05.png","p2_walk06.png",        "p2_walk07.png","p2_walk08.png","p2_walk09.png",        "p2_walk10.png","p2_walk11.png"    ], 10, true);        this.animations.add('jump', [        "p2_jump.png"    ], 10, false);};

initAnimations throw the error - Uncaught TypeError: Cannot read property 'add' of undefined

 

How it can be I don't know but console.log(this) shows the object has field animation and method add in prototype.

Please guys help me!!!

Link to comment
Share on other sites

I would personally not set Actors.Player.prototype to equal Object.create(Phaser.Sprite.prototype).  You should favor Composition over Inheritance for a number of reasons, but definitely in JavaScript where those mechanisms don't have the same meaning as non-dynamic languages.  Just a preference.

 

Instead I would set a field in Player called this.sprite, and pull in your player sprite.  In initAnimations, you can reference this.sprite.animations instead of trying to do this.animations.  Your player object is not just a sprite, and it should not be inheriting any of those properties.  It is composed of a sprite.

Link to comment
Share on other sites

Also here's another problem (I'm not sure about whether this is actually an issue or not since JavaScript can be kind of weird).  You're loading the code for AT.Actors.Player first which depends on the createPlayer function which depends on the initAnimations function which assumes that, by the time the code is loaded, has some method called this.animations.  However, because you have not yet inherited from the Phaser.Sprite class, there's no animations method to be found.  Just a thought, but I don't have time at the moment to verify whether that's true or not.

Link to comment
Share on other sites

Also here's another problem (I'm not sure about whether this is actually an issue or not since JavaScript can be kind of weird).  You're loading the code for AT.Actors.Player first which depends on the createPlayer function which depends on the initAnimations function which assumes that, by the time the code is loaded, has some method called this.animations.  However, because you have not yet inherited from the Phaser.Sprite class, there's no animations method to be found.  Just a thought, but I don't have time at the moment to verify whether that's true or not.

No. I've tried run code with timeout but it's not help. 

Link to comment
Share on other sites

Sorry guys it was my stupid mistake 

right : this.player.add(new AT.Actors.Player(this.game,this._player.x,this._player.y,"player"))- game context 

not right: this.player.add(new AT.Actors.Player(this,this._player.x,this._player.y,"player")); - scene context

 

I just type wrong context

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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