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

Author Topic: Side scrolling 2D game problem  (Read 6444 times)

0 Members and 1 Guest are viewing this topic.

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
Side scrolling 2D game problem
« on: August 14, 2011, 07:13:57 pm »
I am trying to make a side scrolling game (like mario) using SFML 1.6 in c++. I have already implemented the side scrolling mechanism and now the screen's background gets scrolled perfectly. I also have made my character(move along the screen). Now the problem is : How do I get other sprites (such as the walls in mario) which are not currently visible; to scroll along with the background while player presses a key???

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Side scrolling 2D game problem
« Reply #1 on: August 14, 2011, 07:54:30 pm »
You should not move the background and the static tiles, but rather the player. If you get close to the window borders, move the sf::View, so that the part of the map you look at changes.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

xinux

  • Newbie
  • *
  • Posts: 13
    • View Profile
Side scrolling 2D game problem
« Reply #2 on: August 14, 2011, 11:37:55 pm »
As nexus says, only move the player and use sf::View's to 'scroll' along the map.

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
Side scrolling 2D game problem
« Reply #3 on: August 15, 2011, 04:07:37 am »
So basically what I have to do is get the entire level on a large image and then move sf::view to match what the player is seeing?? (correct me if I'm wrong)

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Side scrolling 2D game problem
« Reply #4 on: August 15, 2011, 07:00:58 am »
Yes, that's the idea.

Load the entire level, and just move your view to keep the player on the screen.

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
Side scrolling 2D game problem
« Reply #5 on: August 15, 2011, 08:14:30 am »
Ok thanks for the reply guys.. the side scrolling problem has been solved...  :)
But I have run into another logical problem :
I am trying to make the character jump when the space key is pressed but the jumping motion turned out very fast.
Here is the jump snippet:
int velocity =0;
int gravity = 0;
bool jumping = false;

// Rest of the code is here

if(App.GetInput().IsKeyDown(sf::Key::Space) && jumping == false)
{
  velocity = -50;
  gravity = 2;
  jumping = true;
}

if(jumping)
{
         
  velocity = velocity + gravity;
  sprite1.SetPosition(sprite1.GetPosition().x, sprite1.GetPosition().y+(velocity));
  if (sprite1.GetPosition().y == groundlevel)
  {
   jumping = false;
   velocity = 0;
   gravity = 0;
  }
      
}

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Side scrolling 2D game problem
« Reply #6 on: August 15, 2011, 05:14:55 pm »
I don't know how the rest of your code is laid out. Would increasing the velocity, from -50 to -25 half the speed your character jumps at, or just half the maximum height?

Could you just decrease your characters normal movement speed while jumping so that it takes longer to use up all that negative velocity?

rojan_neo

  • Newbie
  • *
  • Posts: 28
    • View Profile
    • http://makeagame.tumblr.com
Side scrolling 2D game problem
« Reply #7 on: August 15, 2011, 05:27:57 pm »
well if i take my velocity to -25 from -50 it just halves the maximum height and not the distance, because of which the jumping motion happens very fast.

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Side scrolling 2D game problem
« Reply #8 on: August 16, 2011, 03:59:04 am »
Well you could split your jumping into two states. One for going up, where your velocity decreases at the speed you want to rise at until it reaches -50.

And then your current jump state that is triggered once you reach the desired height.

So something like this(just copy pasting and messing with, not tested code):

Code: [Select]
int velocity =0;
int gravity = 0;
int uprate = 0; //rate at which you character gains hight while jumping
bool jumping = false; //going up
bool falling = false; //going down

// Rest of the code is here

if(App.GetInput().IsKeyDown(sf::Key::Space) && jumping == false && falling == false)
{
velocity =0;
gravity = 0;
jumping = true;
}

if(jumping)
{

velocity = velocity - uprate;
sprite1.SetPosition(sprite1.GetPosition().x, sprite1.GetPosition().y+(velocity));
if (velocity == -50)
{
jumping = false;
falling = true;
gravity = 2;
}

if(falling)
{

velocity = velocity + gravity;
sprite1.SetPosition(sprite1.GetPosition().x, sprite1.GetPosition().y+(velocity));
if (sprite1.GetPosition().y == groundlevel)
{
falling = false;
velocity = 0;
gravity = 0;
}

 

anything