Game Development Community

Dynamically Creating t2dStaticSprite (and objects in general)

by Ian Poma · in Torque Game Builder · 06/12/2006 (9:40 am) · 5 replies

I have a static sprite in the level editor with the class assigned to "gem". The code for gem is simple:

function gem::OnAdd(%this)
{
   %this.setUseMouseEvents(true);
   
   %this.SetRandomColor();
}

function gem::SetRandomColor(%this)
{
   %this.color = getRandom(7);   
   %this.setFrame(%this.color);   
}



function gem::onMouseDown(%this, %modifier, %worldPosition, %mouseClicks)
{
   echo("Clicked on gem!");
   echo("Color: " SPC %this.color);
   
   %this.SetRandomColor();   
}

I am using the image "puzzleGem_4ImageMap" from the puzzleArt Resource. The code for this all works fine. Clicking on the gem changes the color of the gem by randomly assigning a frame number.

Now, I am trying to dynamically create one of these gems in script outside the level editor using this code:

$g = new t2dStaticSprite("gem1")
            {
               imageMap = "puzzleGem_4ImageMap";
               class= "gem";               
            }; 
   
   $g.SetPositionX(50 + 10);  
   $g.SetPositionY(50 + 10);  
   
   $g.setLayer(5);
   
   sceneWindow2D.getSceneGraph().addtoScene($g);   
   
   echo("created new gem");   
   echo($g.GetLayer());

The object is being created - the echo statements are being displayed in the console. However, I am not seeing a gem on the screen apart from the one that I created in the level builder. What am I doing wrong? I am really confused about dynamically creating objects and using datablocks in general. Thanks in advance.

#1
06/12/2006 (9:58 am)
Bung in a .setSize along with the .setPositions and it'll probably be OK.
#2
06/12/2006 (10:16 am)
1. I'm not sure you wrap your class name in quotes, or if it matters..
2. I normally set a few extra variables initially like the size, otherwise it seems as if the images dont get displayed at the right size for whatever reason and in a few test cases were actually rendered just so small on my scene graph I couldn't see them like size ="0 0", so I pumped those up to what I wanted, and I make it a habit to set the position to "0 0" , before I move the position with setPosition.


The code looks sane enough though..


Rod
#3
06/12/2006 (10:17 am)
I added .SetSize, didn't work either. I can access all the object's properties in the console, but it just isn't showing up on the screen. Am I doing something wrong with the imageMap property? Is it causing problems because the image is in a resource file?

Should I be doing all this initialization in a datablock?
#4
06/12/2006 (10:20 am)
You can also use clone if you do something like this (ripped from the checkers tutorial):

function gem::onLevelLoaded(%this, %scenegraph)
{
   $baseGem = %this;
   echo("baseGem created");
}

and then to create your gem you do
$g = $baseGem.clone(true);
#5
06/12/2006 (10:26 am)
Duh, ok, i get it now. The problem was in setPosition. The positons I was setting were way outside my camera area.

Thanks for the help!

And regarding my previous question, should i be doing part of the setup for the gem in a datablock? Like setting the layer, size, etc? If so what type of datablock should it be? A t2dSceneObjectDatablock ? Is there a datablock type of t2dStaticSprite?