Game Development Community

Is it possible to draw a sprite without dragging it into the camera

by Angelo · in Torque Game Builder · 09/17/2009 (8:10 pm) · 13 replies

The title might seem kind of vague, I've got no problem with displaying objects, sprites whatever onto the screen as long as i drop them into the camera, i can move them out of view of the camera it's just that i need to put the image there so i can give it a class name. I figure there has to be a better way to draw an object instead of dragging and dropping every image and moving them out of the way of the camera. I figure datablocks are involved in this process some how but i'm still a little fuzzy on datablocks.

#1
09/18/2009 (6:13 am)
What exactly are you trying to do? It sounds like it may be easier to do what you are doing via script and have the objects made dynamically...
#2
09/18/2009 (2:03 pm)
That's what I want to do. As long as the object has a class field in the script section I can render it. I don't want to do that for all my objects though. I tried sceneWindow2d().getSceneGraph().addToScene(%image); but I get an error on addToScene(I only get this error if it's in a function besides onLevelLoaded)
#3
09/18/2009 (2:08 pm)
Could you post the code that you are using to generate your objects?

Edit:
Have you tried:
$lastLoadedScene.add(%myObject); ?
#4
09/22/2009 (5:19 pm)
function Background::displayBackground(%this)
{
    %obj = new t2dStaticSprite() 
	 {    
	 	scenegraph = t2dScene; 
	 };
     %obj.setPosition("0 0");  
     %obj.setSize(100);
     %obj.setImageMap(Grass_FieldImageMap);    
}
#5
09/22/2009 (5:59 pm)
Try this:

function Background::displayBackground(%this)  
{  
    %obj = new t2dStaticSprite()   
     {      
        scenegraph = t2dScene;   
     };  
     %obj.setPosition("0 0");    
     %obj.setSize(100);  
     %obj.setImageMap(Grass_FieldImageMap);      

     $LastLoadedScene.add(%obj);  //Add This Line
}
#6
09/23/2009 (5:47 pm)
When adding that line of the code I get an error saying: Unable to find object: '' attempting to call function 'add'.
#7
09/24/2009 (12:12 am)
I'm not sure I understand what you're trying to do to begin with. Are you saying you want to load in static sprites into the scenegraph, but not have them visible to the player?

If that's the case, why don't you just use setVisible()?

P.S. You can use datablocks to handle the setPosition, size, etc. Here's an example:

/* stick this in your datablock.cs file or w/e you use to house your datablocks */
datablock t2dSceneObjectDatablock( GrassFieldConfig )
{
   Position = "0 0";
   size     = "100 100";
   imageMap = Grass_FieldImageMap;
   Visible  = false;
};

function Background::displayBackground(%this)
{
    %obj = new t2dStaticSprite() 
	 {    
	    scenegraph = t2dScene; 
            config = GrassFieldConfig;
	 };  
}
#8
09/24/2009 (2:14 pm)
I want to draw a static sprite object in code. I don't want to drag the image onto the scene and just put it out of the cameras view then just give it a class name and go from there. Hope this helps.
#9
09/24/2009 (3:08 pm)
@Angelo: The main thing going on here is if you are trying to add the object in script like with your code above, then it exists in memory, however it does not exist in your game world yet - that is what $lastloadedscene.add(%obj); does, it adds it to the game world. $lastloadedscene is the global variable that Torque2D uses to define the currently opened scene. Is it possible you changed that variable to something else?
#10
09/24/2009 (10:32 pm)
I've looked around and I can't seem to find $lastloadedscene, I don't believe I've changed it. What file is it in? Also when I try to use the add function by getscenegraph().add(%obj); I get the same error. There's something in front of getscenegraph but I can't remember right now.

Edit: I found $lastLoadedScene in the common folder I assume since it's given the value "" that that's ok. I added sceneWindow2D.add(%obj); which stopped the error but still nothing has changed.
#11
09/28/2009 (2:49 am)
$lastLoadedScene gets populated at the end of the t2dSceneWindow::loadLevel function. First, make sure it is properly populated by doing $lastLoadedScene.dump();, and then do the same with your new %obj.

Are you certain you have the correct imagemap? Also, does that imagemap use tiled sprites, or is it just a single image?
#12
09/28/2009 (11:46 pm)
God thank you so much, I can't believe it was that simple. I was calling my display function before loadLevel was called so $lastLoadedScene still had a NULL value. Everything works fine now, again thank you.
#13
09/28/2009 (11:54 pm)
Glad I could be of service. :)