LeonardoDigital Posted April 28, 2014 Share Posted April 28, 2014 Hi all.I cant add animations to my extended sprite. This is de code I use for my extended sprite:// Here is a custom game objectBee = function (game, x, y, frame) {Phaser.Sprite.call(this, game, x, y, 'bee',frame);this.animations.add('fly');this.animations.play('fly',12, true);};Bee.prototype = Object.create(Phaser.Sprite.prototype);Bee.prototype.constructor = Bee;/** * Automatically called by World.update */Bee.prototype.update = function() {};This how it is instantiated:this.bee1 = new Bee(this, 100, 300);this.bee1.anchor.setTo(0.5, 0.5);this.add.existing(this.bee1);This how the spritesheet is loaded:this.load.spritesheet('bee', 'assets/bee_3x1.png', 27, 32, 3); And this is the error I get: Uncaught TypeError: Cannot read property 'add' of undefined phaser.min.js:12 b.Animationphaser.min.js:12 b.AnimationManager.addphaser.min.js:12 BeeBee.js:7 play_state.createplay.js:17 b.StateManager.loadCompletephaser.min.js:6 b.StateManager.preUpdatephaser.min.js:6 b.Game.updatephaser.min.js:8 b.RequestAnimationFrame.updateRAFphaser.min.js:11 window.requestAnimationFrame.forceSetTimeOut._onLoop However this works:this.bee2 = this.game.add.sprite(100,10,"bee");this.bee2.animations.add('fly');this.bee2.animations.play('fly',12, true);It seems I am not the only one with this problem:http://www.html5gamedevs.com/topic/4830-animations-adding-error-in-extended-sprite/ I am using: Phaser v2.0.3 Please, any help is very much appreciated.Thank you! Link to comment Share on other sites More sharing options...
Oliver Joyce Posted April 30, 2014 Share Posted April 30, 2014 I'm having the exact same problem as Leonardo - My starling created texture atlas animation fails when bringing it in pretty much the same way, via a prototype extended phaser sprite. And it's not just starling, it's failed with other spritesheet animation JSON/XML combos. Strange thing is, the frames are all there. My player animation has 9 frames, I can console.log( this.animation.frames) and it returns 9. I can type this.animations.frame=5 and it will show frame 5 of the anim. But if I try something like : this.animations.add('run',[0,1,2,3,4,5,6,7],30,true); It brings that same error : Uncaught TypeError: Cannot read property 'add' of undefined Any thoughts? Are we trying to implement it wrong Link to comment Share on other sites More sharing options...
Miroku_87 Posted July 31, 2014 Share Posted July 31, 2014 I've got the same exact problem!Can anybody explain to us how to fix this? Thank you! Link to comment Share on other sites More sharing options...
eguneys Posted August 1, 2014 Share Posted August 1, 2014 When you instantiate Bee, can you log `this` to make sure it is the game object.this.bee1 = new Bee(this, 100, 300);this might work.this.bee1 = new Bee(this.game, 100, 300); Tilde 1 Link to comment Share on other sites More sharing options...
Kobaltic Posted August 1, 2014 Share Posted August 1, 2014 The only difference I can see is that I declare a global (not sure if it is needed) and I initialize in the Enemy function. This is working code:var Enemy;Enemy = function(game, x, y, image) { Phaser.Sprite.call(this, game, x, y, image); game.add.existing(this); var EastArray = [0, 1, 2, 3, 4, 5, 6, 7]; var NorthArray = [8, 9, 10, 11, 12, 13, 14, 15]; var SouthArray = [32, 33, 34, 35, 36, 37, 38, 39]; var WestArray = [57, 58, 59, 60, 61, 62, 63, 64]; this.animations.add('east', EastArray); this.animations.add('west', WestArray); this.animations.add('north', NorthArray); this.animations.add('south', SouthArray); game.physics.enable(this); this.debug = true; this.dir = 'east'; this.xValue = 1; this.yValue = 0;};Enemy.prototype = Object.create(Phaser.Sprite.prototype);Enemy.prototype.constructor = Enemy;Enemy.prototype.move = function() { this.speed = 1; this.x = (this.x + this.xValue * this.speed); this.y = (this.y + this.yValue * this.speed); this.animations.play(this.dir, 10, true);};and my update: Enemy.prototype.update = function() { this.move(); this.followPath(); }; Link to comment Share on other sites More sharing options...
Miroku_87 Posted August 2, 2014 Share Posted August 2, 2014 When you instantiate Bee, can you log `this` to make sure it is the game object.this.bee1 = new Bee(this, 100, 300);this might work.this.bee1 = new Bee(this.game, 100, 300); Hey Eguneys this worked for me!Thank you very much! =) Link to comment Share on other sites More sharing options...
Recommended Posts