Game Development Community

Array of T2DStaticSprite

by Ivan Gromov · in Torque X 2D · 07/12/2009 (10:19 am) · 4 replies

Hi,
I have an array of static sprites in a bejewelled-like game. When I to initialize them, everything is OK while there are less than 78 sprites, but if I increase that number, some of them aren't drawn. Is there a way to walk around that around or should I think of another way to implement jewels?

About the author

Recent Threads


#1
07/12/2009 (10:52 am)
This sounds like a common problem that's not, unfortunately, very apparent when starting with Torque. The standard camera defaults to a limited number of objects on screen at-a-time. See this thread for a possible solution to your problem.
#2
07/12/2009 (5:41 pm)
Are your object aligned on a grid? If so, you should consider using a tile map instead of a batch of sprites. Then, you only need a sprite or two to handle animated transitions from one state to another. The basic idea is something like (swap two tiles):

Blank the image for the source and target tiles to swap.
Position sprites in the exact position and sizes of the source and target tiles with the same imagery, mark them visible.
Animate and move your sprites to their target positions.
Hide both sprites.
Set the new source and target images in the tile map.

This results in only three T2D objects and is way more efficient.
#3
07/15/2009 (10:29 am)
Scott Zarnke,
thanks, that worked for me.

Jason Cahill,
yeah, your way is much more better, but how can I access a tile map from code? I guess it should be something like
tilemap = TorqueObjectDatabase.Instance.FindObject<TileMap>();
but all objects I tried return null.

thanks
#4
07/15/2009 (10:51 am)
@Ivan:
When you create an instance of a TileMap in TXB, it is actually known as a TileLayer.

Something like this should work:
T2DTileLayer tiles = TorqueObjectDatabase.Instance.FindObject<T2DTileLayer>("Your Layer's Name Here");
The name is not necessary if you will only have one registered at a time.

Then you can use the methods PickTile, GetTileByGridCoords, and SetTileByGridCoords to access individual tiles:
/// <summary>
        /// Find a tile by world coordinates.
        /// </summary>
        /// <param name="worldPos">Position, in world coordinates.</param>
        /// <returns>The found T2DTileObject, or null if no match was found.</returns>
        public T2DTileObject PickTile(Vector2 worldPos)

        /// <summary>
        /// Find a tile by grid (x,y) coordinates.
        /// </summary>
        /// <param name="x">Position, in grid (x,y) coordinates.</param>
        /// <param name="y">Position, in grid (x,y) coordinates.</param>
        /// <returns>The found T2DTileObject, or null if no match was found.</returns>
        public T2DTileObject GetTileByGridCoords(int x, int y)

        /// <summary>
        /// Sets the tile at the specified grid coordinates.
        /// </summary>
        /// <param name="x">Position, in grid (x,y) coordinates.</param>
        /// <param name="y">Position, in grid (x,y) coordinates.</param>
        /// <param name="tile">The tile object to insert into the tile layer at the specified grid coordinates.</param>
        /// <returns>True if the tile was successfully replaced.</returns>
        public bool SetTileByGridCoords(int x, int y, T2DTileObject tile)
For a given T2DTileObject, many of its properties like material are set through its T2DTileType. Basically, the T2DTileLayer keeps a list of T2DTileType's, one for each different material which is being used, then the T2DTileObject's each point to one of these T2DTileType's, though those can be replaced. I haven't worked a whole lot with TileLayers in code, but with a little work at it, hopefully you won't find it too difficult.