Compsitesprite as Tilemaps - at first glance
by Caleb · in Torque 2D Beginner · 02/16/2013 (12:26 pm) · 4 replies
I've been running some initial tests with the compsitespritetoy in the sandbaox to see how it will be used in replacement for some of the basic functionality of tile layers used in TGB.
To start, I've edited the createRectLayout function found in the compsitespritetoy in an attempt to create the typical layout we might see in TGB's tile maps. I've set the sprite stride and size to 10 so we have no spaces between our "tiles" and populated the compsitesprite with a 20x15 layout.
The compsitesprite is created and added to the scene with no issues. However, my first thoughts are on the centering of our "tiles"(or sprites). The compsitesprite is not necessarily the same size of the populated sprites. For instance, in this example a 20x15 tile layer in TGB with tile sizes of 10 would create an image size of "200 x 150". However, the compsitesprite in this example is 210x160 and positioning the compsitesprite at a given coordinates, say "0 0" will not center the "tiles" as might be desired.
Perhaps there a better way to populate the sprites in order for the compsitesprite to size appropriately? This is just my first impressions working with compsitesprites as a tile layer given the information shown in the examples.
To start, I've edited the createRectLayout function found in the compsitespritetoy in an attempt to create the typical layout we might see in TGB's tile maps. I've set the sprite stride and size to 10 so we have no spaces between our "tiles" and populated the compsitesprite with a 20x15 layout.
The compsitesprite is created and added to the scene with no issues. However, my first thoughts are on the centering of our "tiles"(or sprites). The compsitesprite is not necessarily the same size of the populated sprites. For instance, in this example a 20x15 tile layer in TGB with tile sizes of 10 would create an image size of "200 x 150". However, the compsitesprite in this example is 210x160 and positioning the compsitesprite at a given coordinates, say "0 0" will not center the "tiles" as might be desired.
Perhaps there a better way to populate the sprites in order for the compsitesprite to size appropriately? This is just my first impressions working with compsitesprites as a tile layer given the information shown in the examples.
function MyCompositeSpriteToy::createRectLayout( %this )
{
// Set the layer #0 sort mode to be depth.
SandboxScene.setLayerSortMode( 0, "z" );
// Create the composite sprite.
%composite = new CompositeSprite();
// Set the default sprite stride.
// This is used in rectilinear layout mode to scale the specified logical position arguments.
%composite.setDefaultSpriteStride( 10, 10 );
// Set the default sprite size used to a little less than the stride so we get a "gap"
// in between the sprites.
%composite.setDefaultSpriteSize( 10, 10 );
// Set the batch layout mode. We must do this before we add any sprites.
%composite.SetBatchLayout( "rect" );
//Set the batch sort mode for when we're render isolated.
%composite.SetBatchSortMode( "z" );
//Set the batch render isolation.
%composite.SetBatchIsolated( MyCompositeSpriteToy.RenderIsolated );
%xrange = 20 * 0.5;
%yrange = 15 * 0.5;
// Add some sprites.
for ( %y = -%yrange; %y < %yrange; %y++ )
{
for ( %x = -%xrange; %x < %xrange; %x++ )
{
// Add a sprite with the specified logical position.
// In rectilinear layout this two-part position is scaled by the default sprite-stride.
%composite.addSprite( %x SPC %y );
// The sprite is automatically selected when it is added so
// we can perform operations on it immediately.
// Set the sprite image with a random frame.
// We could also use an animation here.
%composite.setSpriteImage( "ToyAssets:Tiles", getRandom(0,15) );
}
}
// Add to the scene.
SandboxScene.add( %composite );
// Set the composite sprite toy.
MyCompositeSpriteToy.CompositeSprite = %composite;
}About the author
#2
So, when I added the compositesprite to the scene I could do something like
%compositesprite.setposition(%my200x150image.getposition());
...and my 20x15 grid of sprites(10 in size) would align perfectly over the underlying 200x150 image.
Anyway, my code above to populate the compositesprite was incorrect. I played around with it a little more and found what I was doing wrong.
For what it's worth, here are the changes I made:
02/18/2013 (12:38 am)
Thanks for the reply and sorry for the confusion...chalk this up to my limited understandings and my lack of writing skills. All I was attempting to do was overlay another image, in this case one that was 200x150 in size, with a grid of sprites using a compositesprite. So, when I added the compositesprite to the scene I could do something like
%compositesprite.setposition(%my200x150image.getposition());
...and my 20x15 grid of sprites(10 in size) would align perfectly over the underlying 200x150 image.
Anyway, my code above to populate the compositesprite was incorrect. I played around with it a little more and found what I was doing wrong.
For what it's worth, here are the changes I made:
%xrange = 9.5;
%yrange = 7;
// Add some sprites.
for ( %y = -%yrange; %y <= %yrange; %y++ )
{
for ( %x = -%xrange; %x <= %xrange; %x++ )
{...
#3
02/18/2013 (12:57 am)
No problem and no need to be sorry, this is all new stuff. There will be problems in the engine so I'm always keen to understand what's going wrong to see if someone has found something.
#4
02/18/2013 (9:39 am)
Posts like this are good. You had a problem, then found a solution and posted it. You have my thanks. For people like me who don't yet know much this kind of stuff helps a lot.
Associate Melv May
You should even get a warning like "Cannot set the size of a type 'CompositeSprite' as it automatically sizes itself." if you set its size directly.
The size that is calculated has no effect whatsoever on the use of the composite-sprite so you can ignore the size. You should only be interested in the sprites themselves. The size of the composite-sprite also has no relation to your positioning the composite-sprite itself if that's what you're saying.
FYI though, size is symmetric along each axis centered on the origin meaning the largest offset of any sprite from the origin in both X & Y is doubled and that gives the composite-sprite size but again, this has no effect externally, it's purely internal use.
For sure, you don't have to do what you did with the old t2dTileLayer which was set a size of the layer itself, choose the grid size and how many grid items in both X & Y then populate them. The composite-sprite is unbounded i.e. you can place sprite wherever you like and not have to worry about the bounding size. Just set a layout mode and add as many sprites as you like.
In your case, you're doing something like -X to (X-1) and not "-X to +X" so you have more sprites to the "left" than the "right". Again, to hammer the point home, the size is for rendering and you can't control it and it doesn't affect you.
I'm kind of grabbing at stuff here, not sure if I'm following what your problem actually is apart from you being bothered by the auto-calculated size.