kriket Posted July 17, 2015 Share Posted July 17, 2015 I have different enemies and was thinking of using just one group for them all instead of one separate group for each type of enemy since the enemies are really simple and identical game-play wise (just different look). I wanted to use just one group cos these enemies will be pooled and I wanted to save on performance. So any way of having a group with say two types of sprites and a pool of say 10 copies of each? Link to comment Share on other sites More sharing options...
CodeToWin Posted July 17, 2015 Share Posted July 17, 2015 check out my tutorial on the space shooter. This can be done. I have tow colors of UFOs, that fire differently based on their color (missles vs lasers). You will probably want to read all three parts, but in part 3 I implement the two different types of enemies. http://codetowin.io/?page_id=101 kriket 1 Link to comment Share on other sites More sharing options...
rich Posted July 17, 2015 Share Posted July 17, 2015 Yes you can do this - a Group doesn't care what type of sprite it has in it, it can contain all kinds. kriket and Tilde 2 Link to comment Share on other sites More sharing options...
kriket Posted July 19, 2015 Author Share Posted July 19, 2015 Yes you can do this - a Group doesn't care what type of sprite it has in it, it can contain all kinds. but how do u add multiple sprites to a group and get them out?group.createMultiple(20, 'sprite1');only allows to assign one sprite. @codetowin - can you please specify which code I shall be looking at? Went through part 1,2,3 and didnt find how you implemented two different sprite kinds in one group. Appreciate the help. Link to comment Share on other sites More sharing options...
substandardgaussian Posted July 20, 2015 Share Posted July 20, 2015 What do you mean by "get them out"? createMultiple is useful to generate many sprites with the same spritesheet. If you want different spritesheets you'll need to pass a different key on a different call to createMultiple. If you want to do processing on individual sprites after you've created a bunch, you'll need to walk your group with forEach or iterate or something equivalent. Link to comment Share on other sites More sharing options...
CodeToWin Posted July 20, 2015 Share Posted July 20, 2015 Here's how I implemented it: function createUfos() { for ( var i = 0; i < 4; i++) { for ( var j = 0; j < 2; j++) { if ( i%2 == 0) { var ufo = ufos.create(i * (game.world.width) / 6 + j * game.world.width / 6, j * game.world.width / 8, 'ufo'); } else { var ufo = ufos.create(i * (game.world.width) / 6 + j * game.world.width / 6, j * game.world.width / 8, 'ufo_blue') } ufo.anchor.setTo(0.5, 0.5); } } ufos.x = 100; ufos.y = 50;This sets every other ufo to a blue ufo to access WHICH color it is, you access the "key" property, like so (bolded for emphasis):function ufoFires() { ufoMissile = ufoMissiles.getFirstExists(false); ufoLaser = ufoLasers.getFirstExists(false); livingEnemies.length = 0; ufos.forEachAlive(function(ufo) { livingEnemies.push(ufo); }); if ((ufoMissile || ufoLaser) && livingEnemies.length > 0) { var random = game.rnd.integerInRange(0, livingEnemies.length-1); var shooter = livingEnemies[random]; if ( shooter.key == 'ufo_blue') { ufoLaser.reset(shooter.body.x +50, shooter.body.y +50); ufoMissiles.shootSound.play(); ufoLaser.body.velocity.y = 150*waveNumber; } else { ufoMissile.reset(shooter.body.x+50, shooter.body.y+50); ufoMissiles.shootSound.play(); game.physics.arcade.moveToObject(ufoMissile, player, 120*waveNumber); } // if the last UFO alive is blue, the firing rate of 500 ms makes it almost impossible to kill it without dying -> need // a longer firing timer if (livingEnemies.length > 1) { firingTimer = game.time.now + 500; } else { firingTimer = game.time.now + 2000; } } kriket 1 Link to comment Share on other sites More sharing options...
kriket Posted July 20, 2015 Author Share Posted July 20, 2015 What do you mean by "get them out"? createMultiple is useful to generate many sprites with the same spritesheet. If you want different spritesheets you'll need to pass a different key on a different call to createMultiple. If you want to do processing on individual sprites after you've created a bunch, you'll need to walk your group with forEach or iterate or something equivalent. All I wanted to do is this, Have a group with say, 10 red sprites and 10 blue sprites (obv red and blue colors are just for simplicity and to show that they are different sprites/art), then have the group spawn one out of this pool at random every few seconds. Everything else is easy, I just didnt know if it was possible to create a group of multiple different sprites and not just multiple instances of a single sprite, if u know what I mean. Thanks again, @CodeToWin, will have a look at how u did it once i get home. Link to comment Share on other sites More sharing options...
CodeToWin Posted July 20, 2015 Share Posted July 20, 2015 no problem! If something doesn't work, shoot me a private message. I can check out your code and see what's up. kriket 1 Link to comment Share on other sites More sharing options...
kriket Posted July 20, 2015 Author Share Posted July 20, 2015 @CodeToWin that was perfect!!!!! thanks a lot!! I got it now. Helps immensely. Link to comment Share on other sites More sharing options...
Recommended Posts