GourmetGorilla Posted May 6, 2015 Share Posted May 6, 2015 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 More sharing options...
drhayes Posted May 7, 2015 Share Posted May 7, 2015 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 More sharing options...
GourmetGorilla Posted May 7, 2015 Author Share Posted May 7, 2015 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 More sharing options...
drhayes Posted May 7, 2015 Share Posted May 7, 2015 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 More sharing options...
GourmetGorilla Posted May 7, 2015 Author Share Posted May 7, 2015 I tried this: enemyBullets.callAll(Phaser.Sprite.animation.add('fly3', [ 0, 1, 2, 3], 20, true)); enemyBullets.callAll(Phaser.Sprite.prototype.play, null, 'fly3');I get the error TypeError: Phaser.Sprite.animation is undefined Link to comment Share on other sites More sharing options...
grumpygamer Posted May 8, 2015 Share Posted May 8, 2015 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. drhayes 1 Link to comment Share on other sites More sharing options...
drhayes Posted May 8, 2015 Share Posted May 8, 2015 I'd do what grumpygamer says. Looks way more clear. ( = Link to comment Share on other sites More sharing options...
GourmetGorilla Posted May 8, 2015 Author Share Posted May 8, 2015 Thanks all, I figured it out with a little help: enemyBullets.callAll('animations.add', 'animations', 'fly3', [0,1,2,3], 10, true);enemyBullets.callAll('play', null, 'fly3'); Link to comment Share on other sites More sharing options...
Recommended Posts