How to make an object spawn?
by lama · in Torque Game Builder · 04/04/2013 (3:45 pm) · 1 replies
Hi everyone,
I'm trying to make some objects drop and then spawn when they hit the bottom of the world limit... I adjusted the "Limit Mode" to NULL and checked the "callback" box... but the object spawns from the middle of the screen when it hits the bottom... Did I miss something here????
Here's the script I created:
function BigReward::onLevelLoaded(%this, %scenegraph)
{
%this.startPosition = %this.getPosition();
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
function BigReward::onWorldLimit(%this, %mode, %limit)
{
if(%limit $= "bottom")
{
%this.spawn();
}
}
function BigReward::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCounts, %contacts)
{
if(%dstObj.class $= "Player")
{
%srcObj.spawn();
}
}
function BigReward::spawn(%this)
{
%this.setPosition(getRandom(-50,50), %this.startPositionY);
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
I'm trying to make some objects drop and then spawn when they hit the bottom of the world limit... I adjusted the "Limit Mode" to NULL and checked the "callback" box... but the object spawns from the middle of the screen when it hits the bottom... Did I miss something here????
Here's the script I created:
function BigReward::onLevelLoaded(%this, %scenegraph)
{
%this.startPosition = %this.getPosition();
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
function BigReward::onWorldLimit(%this, %mode, %limit)
{
if(%limit $= "bottom")
{
%this.spawn();
}
}
function BigReward::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCounts, %contacts)
{
if(%dstObj.class $= "Player")
{
%srcObj.spawn();
}
}
function BigReward::spawn(%this)
{
%this.setPosition(getRandom(-50,50), %this.startPositionY);
%this.setLinearVelocityY(getRandom(%this.minSpeed, %this.maxSpeed));
}
About the author
Torque Owner Doc308
I haven't used that book in particular, but there are several areas to get information and tutorials, just also be aware that TGB is no longer "officially" supported. As the new T2D MIT has been made the primary 2D product for Torque. And, whilest TGB not being a bad program, many people are now focused on the "newer" tech.
Now, to hopefully answer your question: %this.startPositionY is referenced to the dynamic field associated with BigReward. Either you have it set to somewhere in the middle of the screen, or it's getting a bad number and then spawing to a default of 0, but more likely it does have a value, otherwise it would result in an error.
To generalize your problem and give a simple solution, you can check your world bounds and set your Y position to be somewhere near the top of your level. Or, for simplicity sake, change your dynamic field value of startPositionY to be near the top of the screen (or even above the world limit so as to seem to fall from the sky and not just appear and fall).