Vertical Shooter
by Paul Jackson · in Torque Game Builder · 08/15/2006 (10:19 am) · 7 replies
I am making a vertical shooter and i have the enemy objects moving the way they need to, but i cannot make them respawn at all. I think i am missing something or reworking the code the wrong way. Here is what i have to far
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
// Setup Enemy Constants
%this.startY=%this.getPositionY();
%this.spawn();
}
function enemyShip::onWorldLimit(%this,%mode,%limit)
{
if(%limit $= "down")
{
%this.spawn();
}
}
function enemyShip::spawn(%this)
{
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
}
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
// Setup Enemy Constants
%this.startY=%this.getPositionY();
%this.spawn();
}
function enemyShip::onWorldLimit(%this,%mode,%limit)
{
if(%limit $= "down")
{
%this.spawn();
}
}
function enemyShip::spawn(%this)
{
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
}
About the author
#2
This is the code that is found in the tutorial:
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
%this.startX=%this.getPositionX();
%this.spawn();
}
function enemyShip::onWorldLimit(%this,%mode,%limit)
{
if(%limit $= "left")
{
%this.spawn();
}
}
function enemyShip::spawn(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
}
08/15/2006 (10:36 am)
Nope this is it. I took it from the shooter tutorial and just reworked it for the Y axis instead of the X axis. This is the code that is found in the tutorial:
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
%this.startX=%this.getPositionX();
%this.spawn();
}
function enemyShip::onWorldLimit(%this,%mode,%limit)
{
if(%limit $= "left")
{
%this.spawn();
}
}
function enemyShip::spawn(%this)
{
%this.setLinearVelocityX(getRandom(%this.minSpeed, %this.maxSpeed));
%this.setPositionY(getRandom(%this.minY,%this.maxY));
%this.setPositionX(%this.startX);
}
#3
08/15/2006 (10:42 am)
I take it that minY and maxY are dyamic fields. In TGB have you specifically given your enemies these fields? If not, then open up TGB, click on the enemy, click the 'Edit' tab, click the 'Dynamic fields' rollout and enter minY, maxY and their relative values. The same goes for minSpeed and maxSpeed. In your code, you've entered "%this.setPositionX(%this.startX);" but you haven't defined the value of startX. Notice in the tutorial code, this value has been assigned. Check the console and see if there are any error messages that could help you figure out what the problem is.
#4
function enemyShip::onLevelLoaded(%this, %scenegraph)
{
%this.startY=%this.getPositionY();
%this.spawn();***************************It doesn't like this line of code.
}
When i run the game it says there is an error on this line and i just can't figure out what it doesn't like about it.
08/15/2006 (10:50 am)
I have the min and max Y dynamic fields. The error it throws me is in this piece of code:function enemyShip::onLevelLoaded(%this, %scenegraph)
{
%this.startY=%this.getPositionY();
%this.spawn();***************************It doesn't like this line of code.
}
When i run the game it says there is an error on this line and i just can't figure out what it doesn't like about it.
#5
Try this.
You will need to create the dynamic fields minX and maxX. You can remove the minY and maxY.
08/18/2006 (10:36 pm)
Your code looks like an incomplete switch to the y axis. Try this.
function enemyShip::spawn(%this)
{
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed)); //Set random speed for ship
%this.setPositionX(getRandom(%this.minX,%this.maxX)); //Place ship at a random X location
%this.setPositionY(%this.startY); // Place ship at the saved Y location when object was first loaded
}You will need to create the dynamic fields minX and maxX. You can remove the minY and maxY.
#6
08/19/2006 (9:34 pm)
Thanks guys i got it i was just missing a semicolon somewhere(it never fails).
#7
08/20/2006 (6:39 am)
Didn't an error message come up? When I miss a semi-colon, I get a syntax error.
Associate Anthony Rosenbaum
like initiallizing %this.startX .minY .maxY