Game Development Community

Creating a StaticSprite Through Script

by Tom Lenz · in Torque Game Builder · 10/01/2008 (9:43 am) · 11 replies

I just need some help creating Sprites and Animated Sprites through script.

I have a feeling I need to create datablocks for each type of object I want but I really don't know where to start with them. Along with just creating the object I need to know how to set classes to the objects along with superClasses.

Thanks for the help.

Tom

#1
10/01/2008 (10:14 am)
Hi,

if the t2dImageDatablock you want to use already exists, it is pretty easy to create a sprite in script:
%sprite = new t2dStaticSprite()
{
     class = "someClass";
     superClass = "someOtherClass";
     imageMap = "theImageMap";
     frame = 0;
};

// don't forget to add the sprite to the scene you want to have it in
%theScene.addToScene( %sprite );

With a t2dAnimatedSprite it is the same procedure except that you assign an animation instead of an imagemap and frame. See for tdn.garagegames.com/wiki/TGB/Reference for field and method names.

-Michael
#2
10/01/2008 (10:19 am)
How would I go about creating a datablock?
#3
10/01/2008 (10:56 am)
Just like a normal object:
// "explodeImageMap" is the name of the newly created datablock
new t2dImageMapDatablock(explodeImageMap) {
      imageName = "~/data/images/explode.png";
      imageMode = "CELL";
      frameCount = "-1";
      filterMode = "NONE";
      filterPad = "1";
      preferPerf = "1";
      cellRowOrder = "1";
      cellOffsetX = "0";
      cellOffsetY = "0";
      cellStrideX = "0";
      cellStrideY = "0";
      cellCountX = "-1";
      cellCountY = "-1";
      cellWidth = "64";
      cellHeight = "32";
      preload = "1";
      allowUnload = "0";
   };

You can take a look at the "Image Maps" article in the Reference section of [url="www.garagegames.com/docs/tgb/official/"]TGB's documentation[/url] if you don't know what the various fields do.

-Michael
#4
10/01/2008 (10:57 am)
Thats kinda what I thought I needed to do, thanks for the help

Tom
#5
10/01/2008 (11:00 am)
Glad I could help :)
#6
10/02/2008 (10:33 am)
I created a different post but im having troubles with this if you can comment that would be so greatfull!

Here is my code

%test = new t2dStaticSprite() { scenegraph = testScene; class = SunClass; Layer=1; };
%test.setImageMap(particles6ImageMap);
%test.setSize(6.25, 6.25);
%test.setPositionY(-5.00);
%test.setPositionX(7.00);
testScene.addToScene( %test );

I have a counter that counts all SunClass objects. When I run this code it increases the counter like they are there. But they do not show on the screen. the xy position should be almost right in the middle of the screen. Background layer of testScene is set to 31.

This is driving me nuts haha.
#7
10/02/2008 (11:34 am)
Do you get any errors in console.log?
#8
10/02/2008 (11:37 am)
None at all and I can even go thru the console to kill the objects and what not. The only thing they are not doing what they are suppose to be is displaying on the screen.
#9
10/02/2008 (11:45 am)
Try this:
// after your code, activate collision, so its active in debug view
%test.setCollisionSendActive( true );

// activate the debug view
testScene.setDebugOn( 0, 1, 2, 3, 4, 5 );

Also echo the scenewindows camera area (via getCurrentCameraArea()) to verify that it is where you think it is. And check if the selected frame (zero?) of the imageMap is not empty. Is there anything that could be in front of the objects that is hiding them?
#10
10/02/2008 (11:56 am)
If you have sceneGraph = testScene inside the braces you don't need to call testScene.addToScene. Those are equivalent. In fact all those can be moved inside the braces if you assign to the correct field, not that its necessary.
#11
10/02/2008 (1:00 pm)
Ok got it working. I followed James advice from my other post

http://www.garagegames.com/mg/forums/result.thread.php?qt=79628

created a global sceneGraph variable. so my code ended up being

%test = new t2dStaticSprite() { 
         scenegraph = $sceneGraph; 
         class = SunClass; 
         Layer=1; 
         imageMap = particles6ImageMap;
         size = "10 10";
         position = "1 1";
      };
      
      // after your code, activate collision, so its active in debug view
      %test.setCollisionActive( 1,1 );

      // activate the debug view
      $sceneGraph.setDebugOn( 0, 1, 2, 3, 4, 5 );

and worked fine! Thanks guys, a lot!