3
votes

In short: How do I make a scrolling gameScene which is NOT infinite?

I'll try to explain what I want to achieve with an example: Hill Climb Race

In this game you drive a car (or any sort of crazy vehicle, actually ;)) up a hill.
Now there is one particular thing about the game that I can't get my head around:

It's pretty obvious that the tracks of each individual stage are NOT laid out randomly. i.e. the course of the track is always the same each time you play it.

What I want to learn is:

  • How do you create a scrolling game scene like that? Is it a huge background node which gets scrolled or is there some sort of fancy tiling involved?

My game needs to scroll both axis (x,y). The players node starts in the center of the game area and can be moved around. There are some obstacles spread around in the area, some of which are not visible initially, because they lie at the edges of the 'game world'.



I suppose the easiest solution would be to use a big background node, but how will that affect the memory consumption of the game?

Thanks for your help!

1

1 Answers

5
votes

We built something like this into our SKATiledMap. The trick is to add the object you want to follow to the background you want to scroll. This will also keep the background on screen.

-(void)update
{
    if (self.autoFollowNode)
    {
        self.position = CGPointMake(-self.autoFollowNode.position.x+self.scene.size.width/2, -self.autoFollowNode.position.y+self.scene.size.height/2);

        //keep map from going off screen
        CGPoint position = self.position;

        if (position.x > 0)
            position.x = 0;


        if (position.y > 0)
            position.y = 0;

        //self.mapHeight*self.tileWidth gives you the size of the map in points
        if (position.y < -self.mapHeight*self.tileWidth+self.scene.size.height)
            position.y = -self.mapHeight*self.tileWidth+self.scene.size.height;
        if (position.x < -self.mapWidth*self.tileWidth+self.scene.size.width)
            position.x = -self.mapWidth*self.tileWidth+self.scene.size.width;

        self.position = CGPointMake((int)(position.x), (int)(position.y));
    }
}

self in this case is the background and autoFollowNode is the players. You could just use self.size.width instead of self.mapHeight*self.tileWidth Hopefully that makes sense and his helpful.