JFish Posted September 24, 2014 Share Posted September 24, 2014 Hello all. I just discovered Phaser.io and think it is great. I am a bit stuck, however, on how to manipulate members of a group. I have a space shooter where I spun up about a dozen enemies within a group. Now I want to select individual members of the groups to move toward the player in some pattern. I can't figure out how to get just one member of the group. I tried something like:var leader=enemies.getFirstAlive();leader.x-=1; but it did not work. Also, what if I wanted it to be a random enemy that was alive? Any guidance would be most appreciated. Link to comment Share on other sites More sharing options...
j0hnskot Posted September 24, 2014 Share Posted September 24, 2014 I don't know if i clearly understand what you ask, sorry for this. This may be helpful for you http://gamemechanicexplorer.com/#follow-3Also, about the random enemy check Group.getRandom() Link to comment Share on other sites More sharing options...
JUL Posted September 24, 2014 Share Posted September 24, 2014 Use body.velocity.x instead of .x http://examples.phaser.io/_site/view_full.html?d=games&f=invaders.js&t=invaders Link to comment Share on other sites More sharing options...
JFish Posted September 24, 2014 Author Share Posted September 24, 2014 Thanks @j0hnkot and @Jul @j0hnkot Group.getRandom() does indeed look useful, thanks. @Jul: I tried this function called from the update state but nothing happens - no error and no movement.function leaderAttack(){var leader=enemies.getFirstAlive();leader.body.velocity.x-=10;}Any thoughts? Link to comment Share on other sites More sharing options...
j0hnskot Posted September 24, 2014 Share Posted September 24, 2014 If nothing happens on both leader.x or leader.body.velocity.x i suspect that something is not working correctly. Try calling console.log(leader) after getFirstAlive(). What's the output? Link to comment Share on other sites More sharing options...
JFish Posted September 24, 2014 Author Share Posted September 24, 2014 @j0hnkot I apolgize for this question (I admit to beeing a noob) ... but I added the console.log(leader) code. Where am I supposed to see the output? In the browser? If so, I see nothing. Link to comment Share on other sites More sharing options...
JUL Posted September 24, 2014 Share Posted September 24, 2014 use an alert(myStuffhere), instead of console, it's less handy for debugging, but that should do the trick for now Also, (and again), take a close look at the code structure of this example http://examples.phaser.io/_site/view_full.html?d=games&f=invaders.js&t=invaders And spot what fundamentally differs from yours . var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update, render: render });function preload() {game.load.image('bullet', 'assets/games/invaders/bullet.png');//etc...}var player;var aliens;var bullets;//etc...function create() {game.physics.startSystem(Phaser.Physics.ARCADE);// The scrolling starfield backgroundstarfield = game.add.tileSprite(0, 0, 800, 600, 'starfield');// Our bullet groupbullets = game.add.group();bullets.enableBody = true;bullets.physicsBodyType = Phaser.Physics.ARCADE;bullets.createMultiple(30, 'bullet');bullets.setAll('anchor.x', 0.5);bullets.setAll('anchor.y', 1);bullets.setAll('outOfBoundsKill', true);bullets.setAll('checkWorldBounds', true);// The enemy's bulletsenemyBullets = 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);// The hero!player = game.add.sprite(400, 500, 'ship');player.anchor.setTo(0.5, 0.5);game.physics.enable(player, Phaser.Physics.ARCADE);// The baddies!aliens = game.add.group();aliens.enableBody = true;aliens.physicsBodyType = Phaser.Physics.ARCADE; //etc...}function update() {// Scroll the backgroundstarfield.tilePosition.y += 2;// Reset the player, then check for movement keysplayer.body.velocity.setTo(0, 0);if (cursors.left.isDown){player.body.velocity.x = -200;}else if (cursors.right.isDown){player.body.velocity.x = 200;} //etc...// Run collisiongame.physics.arcade.overlap(bullets, aliens, collisionHandler, null, this);game.physics.arcade.overlap(enemyBullets, player, enemyHitsPlayer, null, this);} function collisionHandler (bullet, alien) {// When a bullet hits an alien we kill them bothbullet.kill();alien.kill();//etc..} function enemyHitsPlayer (player,bullet) {player.kill();//etc...} Link to comment Share on other sites More sharing options...
JFish Posted September 24, 2014 Author Share Posted September 24, 2014 The alert(console.log(leader)); shows "undefined" The code for the invaders has the whole group moving - which I can do. This is what the aliesn do - which I believe impacts the entire group.function descend() { aliens.y += 10;}I am wondering how to get the first alien to move some way without moving the others in the group. Link to comment Share on other sites More sharing options...
JUL Posted September 24, 2014 Share Posted September 24, 2014 Just use alert(leader); Also, what parameters have you passed to the group ? like... myGroup.fixedToCamera or stuffs like that Link to comment Share on other sites More sharing options...
JFish Posted September 24, 2014 Author Share Posted September 24, 2014 Okay, now it says "[object Object]" in the alert. This is how I create the members of the group://enemies enemies=game.add.group(); enemies.enableBody = true; enemies.physicsBodyType = Phaser.Physics.ARCADE; // enemies.createMultiple(30, 'galaga'); for (var i = 0; i < 7; i++) { yellowJacket = enemies.create(game.world.width-40, 75 * i, 'galaga', 'yellowJacket'); yellowJacket.body.gravity.x = horizGravity*-1; } for (var i = 0; i < 6; i++) { yellowJacket = enemies.create(game.world.width-90, (75 * i)+100, 'galaga', 'yellowJacket'); yellowJacket.body.gravity.x = horizGravity*-1; } for (var i = 0; i < 5; i++) { yellowJacket = enemies.create(game.world.width-140, (75 * i)+125, 'galaga', 'yellowJacket'); yellowJacket.body.gravity.x = horizGravity*-1; } enemies.setAll('anchor.x', 0.5); enemies.setAll('anchor.y', 1); Link to comment Share on other sites More sharing options...
JUL Posted September 24, 2014 Share Posted September 24, 2014 yellowJacket.body.gravity.x = horizGravity*-1; I think it might come from here... Try something like mySprite.body.velocity.x=horizGravity*2; Link to comment Share on other sites More sharing options...
JFish Posted September 24, 2014 Author Share Posted September 24, 2014 horizGravity currently = 0 so that would not do anything ... it is the same as * -1. I shut off the gravity in favor of trying to move members of the group. I know that on creation I could do something like this:body.velocity.x = game.rnd.integerInRange(-200, 200);to set the individual members in motion. Now I am trying to call them individually after the creation to move them. I wanted to start with firstAlive ... but I don't know why it does not work. EDIT: I am trying to do this in the update loop by calling the functionfunction leaderAttack(){var leader=enemies.getFirstAlive();leader.body.velocity.x-=10;}The getFirstAlive() seems to be returning an object. Is the problem that I am constantly calling it before I attempt the move? EDIT 2: This works to select the first enemy and move it:enemies.getAt(1).body.velocity.x-=1;Don't know why getFirstAlive won't do the same thing? EDIT 3: getRandom also works - will eventually move them all since it is in a loop that gets called very frequently (could slow it down with a timer) but it works. Link to comment Share on other sites More sharing options...
Heppell08 Posted September 24, 2014 Share Posted September 24, 2014 I would have used a for iteration loop with that group and moved them like that with some parameters in place for distance/alive etc. Even a forEach may have done the trick here too. Link to comment Share on other sites More sharing options...
JFish Posted September 24, 2014 Author Share Posted September 24, 2014 @Heppell08 - if I understand your post correctly that would be fine for iterating every member of the group to a common action (setAll might work too). However, I just want to work with individual members of the group without regard to disturbing t he rest of the group. That is why enemies.getAt(1).body.velocity.x-=1; works to a degree by allowing me to select any one member of the group to move. That was the thrust of the original post. However, I still don't understand why group.getFirstAlive().body.velocity.x-=1; does not do the same thing ... Link to comment Share on other sites More sharing options...
JUL Posted September 25, 2014 Share Posted September 25, 2014 hm.That's weird... getFirstAlive should work... Maybe you used kill() on them at some point ?Without using revive(); to resurrect them ? Link to comment Share on other sites More sharing options...
JFish Posted September 25, 2014 Author Share Posted September 25, 2014 @JUL ... I am not doing anything yet except for creating everything. Maybe my syntax is wrong?enemies.getAt(1).body.velocity.x-=1; // worksenemies.getFirstAlive().body.velocity.x-=1; // does nothing Link to comment Share on other sites More sharing options...
Recommended Posts