Game Development Community

Spawn and On Collision and Fire bullet

by Neal Guzman · in Torque 2D Beginner · 08/03/2014 (12:21 pm) · 4 replies

When you have a game object ie enemy or a pick up or even a bullet (laser). I'm having a few problems with them spawning correctly. Please read the comments of what I'm trying to do. Hopefully I explained it well enough. I haven't done anything with torque script since 2011 , and I'm taking what I can remember, and using c# knowledge that I do have to be able to code this correctly.

edit : Time : 14:47 (2:47pm) Date: 08/03/2014 Torque Game Builder 1.7.6 is what I'm using

function enemyclass::onLevelLoaded(%this, %scenegraph)
{
   %this.startX = %this.getPositionX();
   //%this.schedule(2000, "spawn");  //as you see here i was trying to get the enemy to spawn on a schedule 
   %this.spawn();
}


function enemyclass::onWorldLimit(%this, %mode, %limit)
{
   if(%limit $= "left")
   {
         //%this.schedule(2000, "spawn"); //%this.spawn();
         %this.spawn();
   }
}


function enemyclass::spawn(%this)
{
    %num = getrandom(0,1);  here i'm trying to get a random enemy image to change 

      if(%num == 0)
      {
      %this.setImageMap(Enemy1ImageMap);  
      %this.setSize(10 ,10);
      }
      else
      {
      %this.setImageMap(Enemy2ImageMap);
      %this.setSize(7 ,7);
      }

   %this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed));
   %this.setPositionY(getRandom(%this.minY, %this.maxY));
   %this.setPositionX(%this.startX);
   %this.fireMissile();

}

function enemyclass::explode(%this)
{
    //%this.schedule(2000, "spawn"); 
    %this.spawn();
}

function enemyclass::fireMissile(%this)
{
   %this.enemyMissile = new t2dStaticSprite()
   {

      scenegraph = %this.scenegraph;
      class = enemyMissile;
      laserSpeed = %this.laserSpeed;
      enemy = %this;
   };

   %this.enemyMissile.fire();
}

here is my pickup item code
pretty much the same code

function pickupClass::onLevelLoaded(%this, %scenegraph)
{
   %this.startX = %this.getPositionX();
   %this.schedule(10000,spawn);  //trying to time the spawn so the player doesn't always get this item
   //%this.spawn();
}


function pickupClass::onWorldLimit(%this, %mode, %limit)
{
   if(%limit $= "left")
   {
      %this.schedule(10000,spawn);
      //%this.spawn();
   }
}


function pickupClass::spawn(%this)
{
   %this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed));
   %this.setPositionY(getRandom(%this.minY, %this.maxY));
   %this.setPositionX(%this.startX);
}

function pickupClass::explode(%this)
{
   %this.schedule(10000,spawn);
   //%this.spawn();
}


function pickupClass::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, 
    %normal, %contactCount, %contacts )
{
   if(%this.isDead)
      return;

   if(%dstObj.class $= "enemyclass")
   {
      %srcObj.explode(); 
      %dstObj.explode();
   }
   if(%dstObj.class $= "playerclass")
   {
        %srcObj.explode(); 
       //%dstObj.explode();  //correct me if i'm wrong but won't this kill the playerclass and not the pickupclass?
         
   }

}

and this last one is my player code

function playerclass::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time, 
    %normal, %contactCount, %contacts )
{
   if(%this.isDead)
      return;
    
   if(%dstObj.class $= "enemyclass")
   {
      %srcObj.explode(); 
      %dstObj.explode();
      player.score+=5;

      //change the text in our score text object
      scoretext.text = "Score = " @ player.score;

      $Enemyaudio = alxPlay( EnemyHit ); 
   }
   
   
    if(%dstObj.class $= "pickupclass")
       {
            %dstObjs.explode();
             $fuelbar = 100;
             fuelbaricon.Extent =268;
       }
}

function playerclass::explode(%this)
{
   %this.isDead = true;
   %this.setEnabled(false);
   %this.spawn();  //%this.schedule(1000, "spawn");  //I always want the player to spawn right away.
}

function playerclass::spawn(%this)
{
   %this.isDead = false;
   %this.setPosition(%this.startX, %this.startY);
   %this.setEnabled(true);
}


Maybe I should do a blog I have questions everywhere on this forum. I hope this makes since. If you need any other coding let me know and I'll put it up for you.

About the author

I am an GUI Artist, HUD Artist, Texture Artist, GUI Editor, and Programmer and Game Designer/Developer. I know and still am learning c# and torque script. I earned my Bachelors' of Science in Game Design and Development on 12/16/11


#1
08/03/2014 (2:42 pm)
The forums are the right place for questions, don't worry about posting here!

One thing needs to be clarified first though - what version of Torque 2D are you using? The script code you posted looks like something from Torque Game Builder. The open source version of Torque 2D is a bit different, there are many subtle and not so subtle changes which prevent TGB scripts from being used 1 to 1.
#2
08/03/2014 (2:48 pm)
I'm currently using Torque Game Builder 1.7.6 . I'm assuming the open source would be 3.0? Which I'm trying to figure out how to use, but at the moment 1.7.6 sorry about not clarifying that in the beginning. Which I just edited my original post about.
#3
08/05/2014 (8:03 am)
Anyone (bumping in case this got lost in the sea.)
#4
08/05/2014 (11:09 pm)
This forum is for T2D MIT, so I'm guessing a lot of people that visit here have either not used TGB before or it's been so long that they've forgotten a bit about its specifics.

I didn't see anything wrong with your script at first glance - perhaps you should add echo lines to your functions to see if they are even being called. Is onLevelLoaded being called? Is spawn, is fireMissile, etc...