Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have been making a tile (block) based 2D side-view game.

So far I have the basic rendering and world generation going. Each tile (block) has its own class for different actions or properties that block has. But I need a way to create these objects using a number.

So every tile (block) has its own id, and so I can use that id to create new objects. And for a inventory system.

share|improve this question
    
What's your question? – SubSevn May 29 '13 at 11:17
    
Designing levels? You can easily do that with strings. – Sri Harsha Chilakapati May 29 '13 at 11:18
    
Game Development? ask at gamedev.stackexchange.com – Sri Harsha Chilakapati May 29 '13 at 11:19
    
I don't think he's asking about level design. It sounds almost like he's just telling us how he creates objects (that is, "I need a way to create..." " so every tile has its own id"..) – SubSevn May 29 '13 at 11:20

1 Answer 1

up vote 1 down vote accepted

The way I do is hardcoding every Tile with it's number.

public Tile getTile(int id, int x_pos, int y_pos)
{
    switch (id)
    {
        case 0:  return new GroundTile(x_pos, y_pos); break;
        case 1:  return new SpringTile(x_pos, y_pos); break;
        ...
    }
    return Tile.getEmptyTile(x_pos, y_pos);
}

I doubt that you are creating tile based level for the game. If so you can use Tiled Map Editor

share|improve this answer
    
Thanks Sri Harsha Chilakapati, I tried that it worked. But is there a more dynamic way of doing it? Like without hard-coding all the tiles. – Maxstupo May 29 '13 at 12:50
    
More dynamic way? You can use Reflection but I'm not sure of it. – Sri Harsha Chilakapati May 29 '13 at 13:17
    
Never mind, the answer you gave will work well. – Maxstupo May 29 '13 at 13:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.