Problem with scrolling

Here is some pseudocode to model my problem

1
2
3
4
5
If the player's ( x position + player height ) reaches 436
            stop the player and start scrolling.

If the player's x position goes lower then 213 
             stop the player start scrolling backwards


Heres what happens. everything is working fine, then the player goes forward and when he reaches 416 is starts scrolling fine. when he released the right button he is uncontrollably moved backwards and keeps going backwards until he reaches 213 when he stops and the scrolling starts scrolling backwards.

what am i doing wrong?
You haven't given enough information for a spot diagnosis.

But it is also something you should try and work out yourself. You conditional logic is making the wrong decision for some reason, so what you need to do is add some/more diagnostic logging. Either one of your tests is wrong, or some of the maths (obviously?)

Start off logging the test which decides whether to move again or not. If that doesn't provide enough information, add logging to the places where the relevant variables are modified.

With enough logging the bug in the decision making process should become clear.

Andy
Thanks i think i got it
I hit a new problem.

I need the image to stop scrolling after a certain point. like, if the x position of image = 0, stop moving the image and let the player move.

how would i do this?
It's very simple if the player position and scroll position are two separate variables (which they should be)
@disch: telling me its simple doesnt really help :/

At least help me with the left side of the screen

presses left decreases player.xVel by ten. If you release the key it increases player.xVel by 10 if player.xVel = -10.

here is my code for scrolling
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//If player gets to screen left
	if( FOREGROUND_XPOS < 1 )
	{
		if( player.GetXPos() < 213)
		{
			MIDGROUND_XPOS += MIDGROUND_MOVEMENT;
			FOREGROUND_XPOS += FOREGROUND_MOVEMENT;

			player.xVel = 0;
		}
	}
	if( FOREGROUND_XPOS < 1280 )
	{
		// If player gets to far of the screen, start scrolling
		if( player.GetXPos() >= 436)
		{
			MIDGROUND_XPOS -= MIDGROUND_MOVEMENT;
			FOREGROUND_XPOS -= FOREGROUND_MOVEMENT;

			player.xVel = 0;
		}
	}
Last edited on
The exact implementation depends on how you want the camera to behave. But generally you would treat the Camera as its own object. Typically the Camera follows the player, so the camera might need to get information from the player... but the logic for the player and camera should be separate.

This is a simple enough problem so that I feel I shouldn't have to walk you through it.

My advice:

- Forget about the camera when writing player/input code. Player/input code should not care about where the camera is or what it's doing.
- Update the camera every frame, just like you update other in-game objects
- When you update the camera, have it move to follow the player around in whatever manner you desire.
- With that, it's easy to stop the camera from going offscreen. Note again the camera going off screen has nothing to do with where the player is.. it's only about where the camera is.
Last edited on
huh? camera? I got it too work with the code below, but this talk of cameras intrigues me. Could you explain that better?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// scroll left
	if( player.xVel == 10 && player.GetXPos() >= 436 && FOREGROUND_XPOS > -635)
	{
		MIDGROUND_XPOS -= MIDGROUND_MOVEMENT;
		FOREGROUND_XPOS -= FOREGROUND_MOVEMENT;

		player.SetXPos( 436 );
	}
	// scroll right
	if( player.xVel == -10 && player.GetXPos() < 213 && FOREGROUND_XPOS < 0 )
	{
		MIDGROUND_XPOS += MIDGROUND_MOVEMENT;
		FOREGROUND_XPOS += FOREGROUND_MOVEMENT;

		player.SetXPos( 213 );
	}
A "Camera" just indicates what part of the world is visible on screen. In a 3D world it's easy to conceptualize (3D games often even give you control over the camera), but the concept applies equally well to 2D games.

The most basic of basic 2D cameras would just have an X,Y position. But cameras can have other attributes as well, like velocities, rotation, whatever.

The idea is you have an object that represents the camera, and when it moves, the on-screen image scrolls around.
Topic archived. No new replies allowed.