[SOLVED] Best way to generate/add random obstacles in a 2D Game?

Hello,

I am following the Parkour tutorial since I am new to Cocos2D and game development. I am at Chapter 8. Add Coin and Obstacles and I see they use the TiledMaps to create objects/obstacles in the game and I understand the code and static objects.

I am making a similar orthogonal game where the player moves left to right on the screen. My game background is a road and I want the objects to be randomly placed at different heights on the road/screen. I am thinking to use tile maps with static background but if I want to implement a random obstacle generation (between 3 different objects, being generated at random times) then is this static object method the best way? Or can I write an algorithm that would generate a physics sprite from a spritesheet at random?

So:

  1. Generate random physics sprite from spritesheet.
  2. Use static objects within the tiledMap

Please advice which of the above method would work better.

Depends what you need to do like I would personally use random generation algorithm which gives you much more control over object placements but on the other hand if you want to be very specific and wont have many objects the tile map would probably quicker and easier to implement.

Thanks for the quick reply.

I think I’m going to go with the random generation algorithm. I do want full control over object placement for sure. I feel like if I use tile map, I’m going to have to make a map for each object and then possibly make even more maps with objects being placed in different places on it.

My game background is a road and I want the objects to be randomly placed at different heights on the road/screen.

Well if your only doing random stuffdefinatly dont use tilemaps it just gets messy - tilemaps are good for visual points in a level like spawning points where certain types of obstacles should be in a set level not really for just generating random items.

1 Like

Awesome, thanks for letting me know.

What about for backgrounds? Say I have a plain black road with a horizontal striped line in the middle and I want to show the lines moving from right to left. I have a .tmx file for that and it seems to look good. Do you suggest I keep that or save the background as an image that I load from a spritesheet?

Thanks in advance for your tips :smile:

Well its up to you I mean theres no harm in using a map but just for simplicity i would just use an image attach two together and scroll them save the two map files that would be all but its not necessary.

1 Like

Ok, I just finished doing that and it works just as well as a tile map for what I’m doing :slight_smile: thanks a lot!