Jump to content

I can´t add animations to my extended sprite.


LeonardoDigital
 Share

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:

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

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

  • 3 months later...

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

 Share

  • Recently Browsing   0 members

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