Layering sprites
by Phil · in Torque X 2D · 03/09/2007 (9:00 am) · 13 replies
Hi all,
Torque allows you to set the layers of your sprites at design time. However if I want to use a sprite that was not loaded at design time, how do I set the layer it is drawn at?
As far as I can see I can overide the DRAW method and then depending on whether I draw my new sprite (using the spriteBatch.draw method) before or after the call to base.draw, determines whether this sprite draws above of beneath ALL of the the layers I have set up in Torque.
What I actually want to do is draw it at a layer inbetween.
Is this possible, or would I need to wait until the source code is released for Torque X to be able to do this?
thanks in advance.
Torque allows you to set the layers of your sprites at design time. However if I want to use a sprite that was not loaded at design time, how do I set the layer it is drawn at?
As far as I can see I can overide the DRAW method and then depending on whether I draw my new sprite (using the spriteBatch.draw method) before or after the call to base.draw, determines whether this sprite draws above of beneath ALL of the the layers I have set up in Torque.
What I actually want to do is draw it at a layer inbetween.
Is this possible, or would I need to wait until the source code is released for Torque X to be able to do this?
thanks in advance.
#2
03/09/2007 (2:54 pm)
I'm curious at why you must draw it in between layers as opposed to having three layers and drawing it in the middle one.
#3
What I have is for example 2 images - background and foreground loaded into torque. The background is set to say layer 30 and the foreground to layer 10.
Now what I need to do is load up a sprite into the game but it is not one that I have previously loaded up into torque. Using the texture2d.fromfile method I can do this, then I overide the draw method like this:
03/10/2007 (5:28 am)
Oh dear I didn't really explain it very well, you are right Luke I want to draw it in a middle layer.What I have is for example 2 images - background and foreground loaded into torque. The background is set to say layer 30 and the foreground to layer 10.
Now what I need to do is load up a sprite into the game but it is not one that I have previously loaded up into torque. Using the texture2d.fromfile method I can do this, then I overide the draw method like this:
protected override void Draw(GameTime gameTime)
{
_gameLogo = Texture2D.FromFile(GraphicsDeviceManager.GraphicsDevice, @"..\..\..\data\images\GGLogo.png");
_batch = new SpriteBatch(GraphicsDeviceManager.GraphicsDevice);
base.Draw(gameTime);
_batch.Begin();
_batch.Draw(_gameLogo, spritePosition, Color.White);
_batch.End();
}But I don't know how to set the level that this sprite is drawn at - anywhere in between level 11 and 29 will be fine. neither (in my example) _batch or _gamelogo have a .layer option so I am not sure how I use the example Mordern has given of myobject.layer - which is what I want to do.
#4
03/10/2007 (7:33 am)
It's not a functionality that XNA provides by default as far as I am aware. If you want your new sprite to play well with Torque X, you should probably create it as a T2DSceneObject of the appropriate type.
#5
03/10/2007 (9:16 am)
Sorry, I was assuming you were using TorqueX.. you are right, my example might not apply to what you are trying to do. I assumed you were using the TorqueX draw method.
#6
But the reason I tried the above solution is that as far as I can tell all Torque images need to be present when the game is built - I think maybe a .xnb file is used when the game is run? Obviously I don't have photos of all the players in advance so cannot do this. The only way I could see to utilise an image provided by the player was to use XNA 2D Textures (since they can be dynamically accessed at runtime) rather than a Torque sceneobject.
03/15/2007 (6:49 am)
Thanks for all the tips. The problem I have is that I want to allow the player to provide an image to be shown on the game (So they could take a photo of themselves and their own image would be displayed as their character avatar). I really wanted this to be a torque object (T2DStaticSprite for example), since everything will work much smoother then (and the layering issue would never have arisen).But the reason I tried the above solution is that as far as I can tell all Torque images need to be present when the game is built - I think maybe a .xnb file is used when the game is run? Obviously I don't have photos of all the players in advance so cannot do this. The only way I could see to utilise an image provided by the player was to use XNA 2D Textures (since they can be dynamically accessed at runtime) rather than a Torque sceneobject.
#7

03/15/2007 (9:26 am)
What you can do Phil is use XNA to load the sprite, and then create a new t2dstaticsprite and cast the xna sprite to the t2dstaticsprite for actual use in-game.
#8
03/15/2007 (10:03 am)
The other option is to make a custom T2D class which will load and render the sprite the way you've mentioned, but inherits from T2DSceneObject. In this way you can control the Layer property for the scene object and get proper sorting.
#9
03/15/2007 (10:17 am)
Great - thanks guys, that gives me a good head start on what I should be doing :)
#10
03/15/2007 (3:23 pm)
Can you really cast an XNA sprite to a t2dstaticsprite? That's awesome.
#11
03/15/2007 (5:13 pm)
Actually now I have looked at trying to do that, I can't see a way to cast an XNA sprite to T2DStaticSprite. If someone has done it then it would be great to know how. I think I am going to need to go down the route of making a custom 2D class - just got to figure out how to create the methods to load the image for the sprite at runtime now ;)
#12
Lets say I have a set of say, 24 templates, all the same size and shape.
And I have 15 sets of those.
I want to in the editor only use one material for each set,
...and in code change the template material so it will be a unique material, one of my 24. By adding a material and changing the template during runtime. Or... adding a material, and then changing the sprite during "onRegister"
I can change a sprite material during register, but how do I create a material without using the editor?
I tried about 15 different code variations, and my errors were kind of out of my scope of reasoning. Any help here would be great...
05/09/2007 (12:16 pm)
Any follow-ups on this would be great. specifically, I am looking into cutting down materials in the editor. Lets say I have a set of say, 24 templates, all the same size and shape.
And I have 15 sets of those.
I want to in the editor only use one material for each set,
...and in code change the template material so it will be a unique material, one of my 24. By adding a material and changing the template during runtime. Or... adding a material, and then changing the sprite during "onRegister"
I can change a sprite material during register, but how do I create a material without using the editor?
I tried about 15 different code variations, and my errors were kind of out of my scope of reasoning. Any help here would be great...
#13
//the following was written into the movement component
and
//a brand new XML txscene document...
and of course...
//in the game.cs, during begin run.
//Note, you probobly want to load this one up first.
05/09/2007 (6:37 pm)
Ok, I figured it out.. But it involves manually copy and pasting some xml, and loading an extra xml doc for the materials.. //the following was written into the movement component
Quote: void _setRenderMaterial()
{
T2DStaticSprite _mySprite = _sceneObject as T2DStaticSprite;
RenderMaterial _material = (RenderMaterial)TorqueObjectDatabase.Instance.FindObject("RockMaterial");
_mySprite.Material = _material;
}
and
//a brand new XML txscene document...
Quote:
1.0
data/images/rock.png
true
1
1
1
1
1
false
false
and of course...
//in the game.cs, during begin run.
//Note, you probobly want to load this one up first.
Quote:
SceneLoader.Load(@"data\levels\levelData2.txscene");
Torque 3D Owner Will O'Reagan
Modern Intrigues
myObject.Layer = 14 will give you a layer of 14
as far as going inbetween layers, I am not sure of that. Also, I'm not too sure about going above layer number 32 though I hear its possible.