Search code examples
iphonecollision-detection

Scrolling a game environment in various directions


How does one create the game "area" for a scroller game?

How does one then put various obstacles with collision detection along this scrolled environment.

I want to try out a project which will allow the user to scroll to a certain direction in order to progress through the game.

How does one map the objects within the environment and then move what I guess is the "camera", the view of the environment.

Thanks


Solution

  • The trick is that there is no "area". The only bits that exist are what's under the camera (the view you currently see) and a small surrounding area giving you time to prepare more world in the direction you are moving..

    Your world coordinates need to be defined as do the starting coordinates for the view. You use tiles to create the view - at its simplest that is 9 tiles, one you are currently "on" and one in each direction. If you look at the keyboard numberpad you are "on" the 5. If you move a little to the top right you are displaying parts of tiles 8, 9, 5 & 6. At that point you would create new tiles in case you move further. As you leave tile 5 you would probably release tiles 4, 1 & 2. Nine tiles may not be the optimal number of course.

    If doing this with UIViews (probably not the high-performance choice) you are probably going to define one big view that can handle all the tiles and tile them onto the view (add and remove subviews), setting the large view's frame to define your camera position. As you move you change the frame to move your camera, when you need to shuffle tiles you move both the tiles and the frame to recenter giving room to move further within the coordinates of your view.

    Collision detection is pretty simple since you define your own dimensions (the thing representing "you" in this world) and objects in your view have dimensions you can check against. CGRectIntersectsRect might be the simplest function to use but if you have irregularly-sized views it will get more complicated.

    This answer about implementing a cyclic UIScrollView is a similar idea but it only handles scrolling on one direction.

    This is a pretty common topic and if you google you will find a lot of sample code and tutorials around.