Game Development Community

Spawning Help

by AndrewD92 · in Game Design and Creative Issues · 03/12/2012 (11:24 am) · 3 replies

I want to create a function which allows a object to randomly spawn anywhere outside of the world limits and then fall to the opposite side. I already have a function which spawns an object randomly at the top, this object then falls to the bottom. Here is that function:

function asteroid::onLevelLoaded(%this, %scenegraph)
{
%this.startPositionY = %this.getPositionY();
%this.setLinearVelocityY(getRandom(%th� %this.maxSpeed));
}

function asteroid::onWorldLimit(%this, %mode, %limit)
{
if(%limit $= "bottom")
{
%this.spawn();
}
}

function asteroid::spawn(%this)
{
%this.setPosition(getRandom(-50, 50), %this.startPositionY);
%this.setLinearVelocityY(getRandom(%th� %this.maxSpeed));
}

How would I go about changing these functions so that the object spawns ANYWHERE outside of the world limits (eg, spawns on the left and falls to the right) and fall to the opposite side? I'm not very good at Torque! :[
Thanks for the help!

#1
03/22/2012 (1:36 am)
So,
you want the object to spawn on the left (or right side) and "fall" or slide across the screen to the right side?

Edit: Is this for 2d or 3d version of Torque?
#2
03/22/2012 (1:41 am)
Yes. So, for example, if an object spawned at the top of the screen it would fall to the bottom. If an object spawns on the right it would fall to the left side of the screen, etc. I want it to spawn outside the world limits randomly on either the top, bottom, left or right.

And im using Torque 2D. I appreciate the reply.
#3
03/25/2012 (3:36 am)
This is just a hunch, since I don't own a liscense to Torque 2d, but have you tried changing
{
%this.startPositionY = %this.getPositionY();
%this.setLinearVelocityY(getRandom(%th� %this.maxSpeed));
}

to this

{
%this.startPositionX = %this.getPositionY();
%this.setLinearVelocityX(getRandom(%th� %this.maxSpeed));
}

You would also need to fix up
function asteroid::onWorldLimit(%this, %mode, %limit)
. You would need to find out where you can figure out if %limit $= bottom and either add the code to figure it out for left and right or use the terms for left and right.

Long story short:
a good place to start is to try to plug X in place of Y in the asteroid::onLevelLoaded and asteroid::spawn(%this)functions. Because 2d is like a math grid with X and Y axis, I would assume that you can change out Y for X.