Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Map Scrolling (C#)  (Read 2805 times)

0 Members and 1 Guest are viewing this topic.

Megatron

  • Newbie
  • *
  • Posts: 22
    • View Profile
Map Scrolling (C#)
« on: December 19, 2010, 11:34:03 pm »
I posted this before but realized that it was a bad way to post it. i created a small sample project to demonstrate the problem I'm having. Should be clearer then last time.

Basically, I want to use views to scroll through maps in a game I'm making. However, i only want the view to scroll if the player has move over half the x/y resolution in x/y, otherwise I just want to move the character sprite while leaving the map alone. Moving the sprite is easy enough, I just increment or decrement the sprites position by the size of a tile over some period of time. When the character's s[rite position has passed half the x/y resolution, I then proceed to increment the character map position while leaving the sprite position alone. The drawing routine will detect that the character's map position is sufficiently large, and will create a new view by the appropriate value.

What I have now, sort of works, but I'm having trouble/losing coordinates when transitioning between moving the character sprite and moving the map. If someone could take a look at my code and help me out with it, our post how they handled map scrolling with views, I'd greatly appreciate it.

Thanks :)

Sample:
http://www.garyoak.com/MapScrollingExample2.zip

Davitosan

  • Newbie
  • *
  • Posts: 5
    • View Profile
Map Scrolling (C#)
« Reply #1 on: December 27, 2010, 01:17:32 pm »
I'm not sure if I understood your question, and I'm a newbie, but since I think I've implemented this in my own platformer, here goes my suggestion:

In your main game loop set the view using SetFromRect, like this

View.SetFromRect(sf::FloatRect(cam_x,cam_y,cam_x2,cam_y2));

where all the parameters are float variables.

Then, in the function where you move your character sprite increment or decrement the x and y values of the view at the same rate as the movement of the character. I did it like this:

(I know that most of this code doesn't make sense without the rest of the program. Just focus on how the view and the character's coordinates change at the same rate when the character is 300 pixels to the right of the x value of the view)

Code: [Select]

if (ventana.GetInput().IsKeyDown(sf::Key::Left) && (animar_x > 0) )
{
caminando_izq = true;
if (caminando_der == true) arranque = -50;
caminando_der = false;

///////// View movement starts here

if ( (animar_x < (cam_x + 300)) && (cam_x >= 0) )
{cam_x = cam_x + arranque * ventana.GetFrameTime(); cam_x2 = cam_x2 - (-arranque) * ventana.GetFrameTime();}

//////// View movement ends here

///////// Character movement starts here

animar_x = animar_x - (-arranque) * ventana.GetFrameTime();
if (arranque > -400) arranque -= 10;
set_x(animar_x);
}


Hope this helps out.