Game Development Community

Proper way to create new projectiles

by Greg C · in Torque Game Builder · 03/01/2011 (10:53 pm) · 5 replies

Hi, I am trying to create a sample, that scales each projectile over its life time.

So basically, you shoot the projectile and it gets smaller over time.

If I create a new static sprite and put it outside the scene to use for the projectile, and I use the .clone procedure to create each new projectile, then my scaling code effects the clones. I want each sprite to have completely independent behavior. What do I need to do to ensure that when I scale one of these sprites, the other related sprites are left unaffected or scale on their own.

Thank you.

#1
03/02/2011 (1:07 am)
It's probably a good idea to post your scaling code here too.
#2
03/02/2011 (6:03 am)
Here is the code for the player projectile:

function PlayerProjectile::onLevelLoaded(%this, %scenegraph)
{
%this.enableUpdateCallback();
}
function PlayerProjectile::onUpdate(%this)
{
%newX = %this.size.x - %this.sizeStepIncrement;
%newY = %this.size.y - %this.sizeStepIncrement;

%this.size = %newX SPC %newY;
}

Here is how the projectile is thrown from the Player:

function Player::throwShuriken(%this, %val)
{
if (%val == 1)
{
%newProjectile = PlayerProjectile.clone(true);
%newProjectile.sizeStepIncrement = .01;

%position = Player.getLinkPoint(1);
%newProjectile.setPosition(%position);
%newProjectile.setLinearVelocityY(%newProjectile.speed);
}
}

The PlayerProjectile is a static sprite I dragged into the level and I set properties on, so I could later duplicate them with the above code.

My problem is that after the first projectile is thrown, the second projectile starts out the current size of the first projectile thrown and so on and so on until you can't even see the projectile you are throwing any more because it scaled to super tiny until not visible.

It's like the scaling is effecting every clone and I wanted them to work independently of each other and they should all start the original size of the sprite and fade out over time.

Thank you.
#3
03/02/2011 (11:34 am)
Looks like sizeStepIncrement field on your reference object has non-zero value.
#4
03/02/2011 (2:26 pm)
I am not sure what you mean. The scaling works fine, it's just that each clone that is created takes on the size of an existing projectile and it doesn't begin fully sized. It's like the scaling on the objects is not happening independently, they are all being scaled simultaneously. Is this from the cloning? Is cloning things the recommended practice when you need more than one?
#5
03/02/2011 (10:08 pm)
I ended up figuring this out. As I suspected, the cloning part seemed to be the culprit. I ended up new'ing up new sprites and populated as much detail on them as possible with a datablock. Then I created a behavior to scale the sprite and attached that behavior to the newed up sprite and it seems to be working well now. Each projectile scales properly, starting at a specified value and shrinking down, all independently and not at the same time like with the cloned sprites.