I can´t add animations to my extended sprite.

Recommended Posts

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:



  1. Uncaught TypeError: Cannot read property 'add' of undefined phaser.min.js:12
    1. b.Animationphaser.min.js:12
    2. b.AnimationManager.addphaser.min.js:12
    3. play_state.createplay.js:17
    4. b.StateManager.loadCompletephaser.min.js:6
    5. b.StateManager.preUpdatephaser.min.js:6
    6. b.Game.updatephaser.min.js:8
    7. b.RequestAnimationFrame.updateRAFphaser.min.js:11
    8. 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:



I am using:   Phaser v2.0.3


Please, any help is very much appreciated.

Thank you!

Share this post

Link to post
Share on other sites

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 : 




It brings that same error :  Uncaught TypeError: Cannot read property 'add' of undefined


Any thoughts? Are we trying to implement it wrong 



Share this post

Link to post
Share on other sites

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();    };

Share this post

Link to post
Share on other sites


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! =)

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.