Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

How to animate a spritesheet that's been added to a group?


GourmetGorilla
 Share

Recommended Posts

How do I animate a sprite that's already been added to a group?

 

Here's my group: 

  // The enemy's bullets    enemyBullets = game.add.group();    enemyBullets.enableBody = true;    enemyBullets.physicsBodyType = Phaser.Physics.ARCADE;    enemyBullets.createMultiple(30, 'enemyBullet');    enemyBullets.setAll('anchor.x', 0.5);    enemyBullets.setAll('anchor.y', 1);    enemyBullets.setAll('outOfBoundsKill', true);    enemyBullets.setAll('checkWorldBounds', true);

Here's the code I think I should add, but if I add it anywhere in that code block I get the error 'enemyBullets.animations.' undefined' .  

    enemyBullets.animations.add('fly3', [ 0, 1, 2, 3], 20, true);    enemyBullets.play('fly3');

Here's my loading spritesheet: 

game.load.spritesheet('enemyBullet', 'assets/games/invaders/enemy-bullet.png', 11, 19);
Link to comment
Share on other sites

Instead of adding animations and trying to play them on the group, you want to add animations and play them on the Sprites within the group. Try "enemyBullets.callAll(Phaser.Sprite.prototype.play, null, 'fly3');" instead. That second "null" param will invoke the function in the context of the Sprite.

Link to comment
Share on other sites

Thank you. What is that 'null' param?

I tired it out and get the error TypeError: enemyBullets.animations is undefined - it doesn't like the first line.

 

Here's the code I tried.

enemyBullets.animations.add('fly3', [ 0, 1, 2, 3], 20, true); enemyBullets.callAll(Phaser.Sprite.prototype.play, null, 'fly3');
Link to comment
Share on other sites

I said in my answer: "That second "null" param will invoke the function in the context of the Sprite."

 

Yeah, you need to change that line as well. Groups don't have an animation manager like Sprites do so they don't have an animations property. What you really want is to call all on the sprites' animations property. It'll look a lot like your new callAll call I think.

Link to comment
Share on other sites

IMHO I think you're doing this the wrong way round, but GURU's might just say that I'm talking BS.

 

Ideally when you add sprites to a group you would/should keep track of them.

 

Let's say you add a bunch of sprites to a group, here's how I'd do it:

var group = game.add.group(),    enemyArray = [];// we add 10 spritesfor (i=0; i<10; i++){        // I chuck the enemy sprites instances in an array so I can reference them whenever.    enemyArray[i] = game.addSprite(x,y,'enemy');    enemyArray[i].animations.add('fly3', [ 0, 1, 2, 3], 20, true);   // Then add them to the group   group.add(enemyArray[i]);}

at that point wherever they are I can access them: 

enemyArray[0].position.x = 120;enemyArray[0].play('fly3');

regardless of the group.

 

Code is not perfect but it gives you the idea.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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