Scrolling everything but the player character


(Jacob) #1

The game I’m working on is a sidescroller with 1280 x 720 screen size. The player traverses through a level filled with many 1280 x 720 rooms. Nothing actually “scrolls” yet; each room is the same size (1280 x 720) as the screen so I just change all the images to a different room when the player reaches an edge of the screen. I am now adding rooms with larger widths and would like everything to scroll with the player’s movement until the edge of the room reaches the edge of the screen (A near perfect example would be Zelda II). I’m looking for the best way to go about this because so far I’m having a few problems like the player’s hitbox not sticking with the player’s graphic.


(Jacob Albano) #2

So you want to have the camera follow the player until he gets too close to the edge of the room, and keep the camera from showing the empty space outside the room?


(Jacob) #3

Yes. Just like in Zelda II, when Link enters a new room, he starts from the edge of the screen & moves across it until reaching the center. Then he’s locked in the center of the screen & everything but him scrolls until he gets too close to the edge of the room.


(der_r) #4

You could create a Camera entity with the size [edit: by size I actually mean hitBox] of 1280x720 and add it to your scene. Then use clampHorizontal/clampVertical to keep the camera inside the map bounds.

In the update() function, you would would call FP.setCamera().


(Willard Torres) #5

If you would want the player to be in the middle of the camera, then:

FP.camera.x = FP.clamp(player.x - (FP.screen.width * 0.5), 0, worldWidth);
FP.camera.y = FP.clamp(player.y- (FP.screen.height * 0.5), 0, worldHeight);

Something like this, yeah.


(Jacob) #6

Thank you. I’ll give it a try.


(Jacob) #7

Here’s what I ended up doing & it’s working out:

In GameWorld:

override public function update():void 
{
	followPlayer();
	super.update();
}


private function followPlayer():void
{
   // Check if the player's in a double width room
   if (currentRoom == doubleWideRoom)
   {
	// If the player character is 170 pixels from the center of the camera,
	// have the camera follow the player character.
	if (collideLine("hero", FP.camera.x + 470, 0, FP.camera.x + 470, 720) && !hero.faceRight) FP.camera.x = (hero.centerHead - 16) - 470;
	else if (collideLine("hero", FP.camera.x + 810, 0, FP.camera.x + 810, 720) && hero.faceRight) FP.camera.x = (hero.centerHead + 16) - 810;
	
	// As soon as an edge of the camera goes beyond an edge of the
	// background image, set the camera at the edge of the background image.
	if (FP.camera.x < cavesBaseBg2.x) FP.camera.x = cavesBaseBg2.x;
	else if (FP.camera.x + 1280 > cavesBaseBg2.x + cavesBaseBg2.width) FP.camera.x = (cavesBaseBg2.x + cavesBaseBg2.width) - 1280;
   }
}