Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add animation to a group sprite in phaser.js?

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

Here's my spritesheet:

game.load.spritesheet('enemyBullet', 'assets/games/invaders/enemy-bullet.png', 11, 19);

Here's the 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.

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

However, if I add that anywhere in the above group code block I get the error 'enemyBullets.animations.' undefined'.

Any ideas?

like image 946
Agent Zebra Avatar asked May 07 '15 01:05

Agent Zebra


1 Answers

I figured out my answer:

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

Tested and it works.

like image 154
Agent Zebra Avatar answered Nov 15 '22 11:11

Agent Zebra