Jump to content

Single Sprite in Group Kill


Heppell08
 Share

Recommended Posts

Little niggle here but i need a bit of help.

I've posted on this before here:- http://www.html5gamedevs.com/topic/3130-sprite-groups-question/

 

But this time its a bit different. I have a single entity in the group and nothing more. I plan on have 2 pretty identical groups but on the overlap i will fire a new function.

This is all relatively easy work for me to do but what my problem is, is this:

 

I have the sprites in the group to spawn at random places within the world. What I'm having an issue with is having the sprite that the player overlaps with needs to kill();

 

Now i can get the sprites to kill with:

var i;collectGroup.getAt(i).kill();

But that kills them all and i only want the one i collide with to die. So i tried this instead but i kills what seems to be the order of creation and not the one i overlap with:

            gravTime: function()            {            token = collectGroup.getFirstAlive(); // i believe something needs to be here differently.                if(token)                {                    token.kill();                    gravNorm += 1;                }                                 console.log('killed collect');            },

So what other was is there for me to kill the specified sprite the is overlapped?

I have code working for this in my other project that works for what i want here:

 function enemyShot()    {        var i;        for(i = 0; i < enemies.length; i++)        {            enemies.getAt(i).damage(this.game.difficulty /2);        }        bullet.kill();            }

That is in a different project and it works perfectly, i tried to encorporate the same ideaology but to no avail.

Link to comment
Share on other sites

I am passing the groups the hold the sprite in the overlap and trying to get the sprite that is overlapped to die.

 

The group is created here:

setGravs: function()        {            collectGroup = this.add.group();            for(var i = 0; i < collectables; i++)            {                gravitoken = collectGroup.create(this.world.randomX, this.world.randomY, 'gravitoke');                gravitoken.health = 1;            }        },

The overlap is called below:

this.physics.overlap(player, collectGroup, this.gravTime, null, this);

and heres the function:

gravTime: function()            {token = collectGroup.getFirstAlive(); // i believe something needs to be here differently.            if(token)                {                token.kill();                    gravNorm += 1;                }                                 console.log('killed collect');            },
Link to comment
Share on other sites

Ok so i just did what i did in my other game. I still think there is probably a different way to do this. If you guys do know them please let me know but for now my solution so this post doesnt go redundant:

 

Created the Vars:

var grav1;var grav2;var grav3;var grav4;var grav5;var grav6;var grav7;var grav8;var grav9;var grav10;

Then the group:

setGravs: function()        {            collectGroup = this.add.group();            for(var i = 0; i < 1; i++)            {                grav1 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav2 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav3 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav4 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav5 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav6 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav7 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav8 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav9 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');                grav10 = collectGroup.create(this.world.randomX, this.world.randomY/2, 'gravitoke');            }        },

Then collision code:

this.physics.overlap(player, collectGroup, this.gravTime, null, this);

And finally kill them like so:

gravTime: function()            {                if(this.physics.overlap(player, grav1))                {                    grav1.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav2))                {                    grav2.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav3))                {                    grav3.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav4))                {                    grav4.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav5))                {                    grav5.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav6))                {                    grav6.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav7))                {                    grav7.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav8))                {                    grav8.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav9))                {                    grav9.kill();                    gravNorm += 1;                }                if(this.physics.overlap(player, grav10))                {                    grav10.kill();                    gravNorm += 1;                }            },

That's all i could come up with for killing a specific part of a group on overlap.

Link to comment
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...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...