SFML community forums
Help => Graphics => Topic started by: rojan_neo 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???
-
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.
-
As nexus says, only move the player and use sf::View's to 'scroll' along the map.
-
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)
-
Yes, that's the idea.
Load the entire level, and just move your view to keep the player on the screen.
-
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;
}
}
-
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?
-
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.
-
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):
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;
}