kriket

Can u add several different sprites to one group?

Recommended Posts

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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;
        }
    }

Share this post


Link to post
Share on other sites

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. 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.