Game Development Community

Removal and respawn with collision detection using pickRadius.

by Jon Mitchell · 04/04/2007 (10:47 pm) · 0 comments

//*********************Blow up and Respawn function********************************
//If Player blows up, remove it from screen via disable, count down and call respawn function


//This function was called from an onCollision detection because the player was hit.
function player1::explode(%this)
{
%this.isDead = true;
%this.setEnabled(false);
%this.schedule(2000, "spawn");
}


//Get random X and Y cords to respawn into, check doing a pick radius to see if there is an obstacle or
//enemy where we want to spawn
//If there is something in the way call the function and start again.
//If the space is not occupied then respawn the player
//min, max X and Y are set in a dynamic field on the player, maxX maxY minX minY
//I used the world limit since you can see the whole playfield in one window, you may want
//to tweak your own values.

function player1::spawn(%this)
{
%this.setPositionY(getRandom(%this.minY, %this.maxY));
%this.setPositionX(getRandom(%this.minX, %this.maxX));
%objectList = %this.sceneGraph.pickRadius( %this.getPosition(), 10, 0xFFFFFFFF, BIT(3)|BIT(6) );
//The first parameter (10) is radius the second (0xFFFFFFFF) is group mask the last (BIT(3)|BIT(6))
//is the layer mask(s) 0xFFFFFFFF is for all 32 bits, thanks to Dan Maruschak for that and help with the pick //radius!


//Count how many objects are in the radius
%objectCount = getWordCount(%objectList);
for(%i = 0; %i < %objectCount; %i++)
{
}
//If there is one or more objects we call spawn again
if (%i >= 1 ) {
%this.schedule(0, "spawn");
} else {
//If there is nothing in the radius go ahead and respawn the player
%this.isDead = false;
%this.setEnabled(true);
}
}