Game Development Community

Creating sprites in script

by Koen Van Baelen · in Torque Game Builder · 05/13/2007 (4:08 am) · 2 replies

I know it's probably a very stupid mistake I'm making, but I can't get this to work. I want to make the player shoot fireballs by pressing the spacebar. Here's the function for firing:

function Player::onLevelLoaded(%this, %scenegraph)
{
$PlayerShip = %this;
...
moveMap.bindCmd(keyboard,"space","Player::SpacePress();","");
}

function Player::SpacePress(%this)
{
%PlayerFire = new t2dStaticSprite()
{
scenegraph = %this.scenegraph;
WeaponSpeed = %this.WeaponSpeed;
};
%PlayerFire.setImageMap(Player_Weapon);
%PlayerFire.setSize(40,30);
%PlayerFire.setVisible(true);
}

I haven't set any speed or other options, all this should do is create a fireball sprite in the middle of the screen. I have checked if the SpacePress function is actually executed by writing some text to the console and it is. No sprite appears, however. What am I doing wrong?

#1
05/13/2007 (6:54 am)
You are setting the bindCmd to "Player::SpacePress()" which is a static call (i.e. it wont have %this set when it calls it and so %this.scenegraph etc will be null).

You need to change that to:

...

moveMap.bindCmd( keyboard, "space", %this @ ".SpacePress();", "" );

...
#2
05/13/2007 (10:49 am)
Ok, it works now. Thanks a lot for the help!