Game Development Community

Strange problem when I respawn

by nick fortier · in Game Design and Creative Issues · 06/03/2011 (2:42 am) · 3 replies

I've been modifying the shooter tutorial to make it more interesting. One of the things I've done is to give my ship life and have the missiles do a specific amount of damage.I have the life of my ship set at 100 and the missiles damage at 25 so my ship dies after after 4 hits. This works fine except for one thing, my ship takes four hits to die the first time but after I respawn it only takes one hit to be killed.I can't figure it out, I've tried all sorts of things but can't get it to work right.
Heres my code for this:

//dead//
function playerShip::onCollision(%srcObj,%dstObj,%srcRef,%dstRef,%time,%normal,%contactCount,%contacts)
{
if(%this.isDead)
return;
if(%dstObj.class $="enemyShip")
{
%srcObj.modifyLife();
%dstObj.spawn();
}
}
//respawn//
function playerShip::explode(%this)
{
%this.isDead=true;
%this.setEnabled(false);
%this.schedule(2000,"spawn");
}
function playerShip::spawn(%this)
{
%this.isDead=false;
%this.setPosition(%this.startX,%this.startY);
%this.setEnabled(true);
}
//Life//
function playerShip::modifyLife(%this,%dmg)
{
%this.life +=%dmg;

if(%this.life > 100)
{
%this.life=100;
}else if(%this.life<0)

{
%this.life = 0;
}
if(%this.life<=0)
{
%this.spawn();
}
}

#1
06/03/2011 (3:14 am)
Right off the Bat, your Respawning the same Object.
You never reset the %this.life value so when it respawns
it has the Current Value for %this.life

In the Respawn code, add a reset of the life value
like
function playerShip::spawn(%this)
{
  %this.isDead=false;
  %this.modifyLife(100);
  %this.setPosition(%this.startX,%this.startY);
  %this.setEnabled(true);
}

You have your modifyLife(..) setup right
just forgot to call it during spawn :)
#2
06/03/2011 (3:40 am)
Oh I see thanks. I figured if the ship respawned all the code would just respawned with it, never thought I'd have to include the modifyLife function in with it.
#3
06/03/2011 (6:45 am)
Yeah, can be confusing..
Basically, your object is never deleted, just ReActivated in
the Spawn location.

You would have to actually call %(someObject).delete()
to actually Remove the object, but then you would also
need to ReCreate a New object.. like

// Create the demo player object
   %player = new AiPlayer()
   {
      dataBlock = DemoPlayer;
      path = "";
   };
   MissionCleanup.add(%player);
   %player.setShapeName(%name);
   %player.setTransform(%spawnPoint);
   return %player;


then you start running into tracking objects :)