san40511 Posted November 29, 2015 Share Posted November 29, 2015 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 More sharing options...
jmp909 Posted November 29, 2015 Share Posted November 29, 2015 you could try this maybethis.initAnimations().bind(this)not sure if it's right Link to comment Share on other sites More sharing options...
san40511 Posted November 29, 2015 Author Share Posted November 29, 2015 you could try this maybethis.initAnimations().bind(this)not sure if it's rightthanks for help but it's not work. Link to comment Share on other sites More sharing options...
Shepless Posted November 29, 2015 Share Posted November 29, 2015 Interestingly, that code works for me in a plunker (as in this.animations is an instance of AnimationManager). How are you creating the instance of AT.Actors.Player? Link to comment Share on other sites More sharing options...
bobbydabrain Posted November 29, 2015 Share Posted November 29, 2015 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 More sharing options...
bobbydabrain Posted November 29, 2015 Share Posted November 29, 2015 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 More sharing options...
jmp909 Posted November 30, 2015 Share Posted November 30, 2015 Seems to be solved here...http://www.html5gamedevs.com/topic/11260-animations-wont-work-in-sprite-object/ Link to comment Share on other sites More sharing options...
san40511 Posted November 30, 2015 Author Share Posted November 30, 2015 thank you but it doesn't help Link to comment Share on other sites More sharing options...
san40511 Posted November 30, 2015 Author Share Posted November 30, 2015 Interestingly, that code works for me in a plunker (as in this.animations is an instance of AnimationManager). How are you creating the instance of AT.Actors.Player?new AT.Actors.Player(game,x,y) Link to comment Share on other sites More sharing options...
san40511 Posted November 30, 2015 Author Share Posted November 30, 2015 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 More sharing options...
san40511 Posted November 30, 2015 Author Share Posted November 30, 2015 Any ideas? Link to comment Share on other sites More sharing options...
san40511 Posted November 30, 2015 Author Share Posted November 30, 2015 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 More sharing options...
Recommended Posts