• Create Account

# Jump Feature Breaking My Game?

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1MrChrisnis  Members   -  Reputation: 336

Like
0Likes
Like

Posted 30 September 2012 - 01:10 PM

Hey guys,

In one of my classes we are doing Scrolling backgrounds and general sprite movement stuff. Up until now everything has worked properly, however when I put in a jump feature, it seems to cause problems when the player jumps. Before jumping, the player will be confined within the sides of the screen when moving left/right, however after the player jumps, the confines of the screen seem to break and the player moves at a higher pace than they are supposed to when moving side to side.

The code is attached to this post.

Any help would be appreciated,
Thanks
Chris

### #2Servant of the Lord  Crossbones+   -  Reputation: 14357

Like
0Likes
Like

Posted 30 September 2012 - 01:28 PM

I'm not wanting to download a questionable file, nor am I wanting to crawl through >12MB of code, so would you be willing to post the relevant portions between [ code ] and [ /code ] tags?
Don't post the entire project, just the area where the bug is most likely to be (post the jump code, screen collision code, and the movement code - the entire function for each).

Edited by Servant of the Lord, 30 September 2012 - 01:28 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #3MrChrisnis  Members   -  Reputation: 336

Like
0Likes
Like

Posted 30 September 2012 - 01:37 PM


//The Jump Function

void Player::jump()

{

set_velocities(0, -30);

set_auto_move(40);

set_y_acceleration(9.8);

set_auto_accelerate(40);

}

//Which is stopped by this to stop the

if(player->get_y() > 300)

{

player->set_y_velocity(0);

player->set_y_acceleration(0);

player->set_world_position_y(300);

isJumping = false;

}

//The screen collision code is just put in with the SDK_KEYDOWN stuff

case SDLK_LEFT:

if(player->get_x() >= 10)

player->moveHorizontally(-10); break;

case SDLK_RIGHT:

if(player->get_x() + player->get_width() <= 630)

player->moveHorizontally(10); break;

[size=4]//And the moveHorizontally function

void Player::moveHorizontally(float speed)

{

set_velocities(speed, 0);

move();

}



Edited by MrChrisnis, 30 September 2012 - 01:39 PM.

### #4SuperVGA  Members   -  Reputation: 1107

Like
0Likes
Like

Posted 30 September 2012 - 01:55 PM

Hi Mr Chrisnis,
What does the move() function look like, and is there a movement related function that's called on every main game-loop iteration? (If you have a such...)
Also, the units you use are unknown to us. Are they pixels or tiles?

How about rather than player->moveHorizontally, you create a set_velocity_x() or something like that?
Make it so that the movement of the player is defined by a vector, limited by a maximum on both axes, and maybe added to on each iteration by an acceleration vector.
This is also far from ideal, but I can imagine the confusion when you call the movement bit more than one place.

I just found that moveHorizontally seems to set the vertical movement to 0:
void Player::moveHorizontally(float speed)
{
set_velocities(speed, 0);
move();
}

Is this intentional? Again, make it possible to set the velocity on each axis, independent on oneanother, or call set_velocities(speed, velo.y).

### #5EngineProgrammer  Members   -  Reputation: 295

Like
1Likes
Like

Posted 30 September 2012 - 02:04 PM

MrChrisnis, don't send your ipch, debug folder and the .sdf file. We don't need those files
As soon as we open your project and rebuild those files are created again. Your rar file takes 12MB+ ... without the files from the sentence above your rar file should be around 170Kb.

By the way, I can't run your project. I think you have separated your your SDL files from your game files. Unless you have linked them in the properties? And also, I opened your project and looking at your code, honestly. I see allot of bad coding there.

I'm sorry but I can't find the problem because I can't get out of your code.

When you make a player class you need to think a game-logic. How do you want your player to act in-game. You want to say: "I give you all info, you take care about it' A player has an AI, 'also in coding..'. the class needs to collect information around them and use them.

class Player
{
public:
Player();
~Player();

void Update(EnemyList* pEnemyList, LevelObjects* pLevelObjects;); // something like this, NOTE: this isn't done this way for big games.
void Draw();
};

Your player can receive the information about everything, there are design patterns for this, not showing them now. It's called the Observer if I'm correct.

So your Player can check if he has hit a wall, hits another player, has hit a player with a bullet, and so on. You just need to Update and Draw him in the main function.
To handle key/mouse input I would suggest you for now to use a global(or singleton) Input class. Which stores all the keyboard and mouse info.
I know singletons are bad.. But everyone, really every programmer has made a singleton is his life because it's 'cool' and 'easy'
Though I don't know how SDL works, maybe you need to send all the input through the Update method also.

Have a look at this: http://www.angelfire...lusin21days.pdf
Look at the day 6: basic classes. Look at how they structure their classes.

~EngineProgrammer

### #6Servant of the Lord  Crossbones+   -  Reputation: 14357

Like
0Likes
Like

Posted 30 September 2012 - 02:35 PM

Thanks for posting the code! It makes it easier for lazy or paranoid people (like me) to help you.
Thank you for also using the [ code ] tags, it makes it easier to read (and again easier to help you).

One of the reasons why your screen collision might not be working is this:
case SDLK_LEFT:
if(player->get_x() >= 10)
player->moveHorizontally(-10); break;

You're only checking it when the player clicks the key... but the player can move (from velocity from previous clicks) without clicking the key. You ought to check for screen collision after any position update from velocity, not after any keypress.

Also, when colliding, don't just undo the horizontal movement, but set the player back to a save place.

Imagine this:
• Player (from velocity) moves more than 10 units to the left. Let's say he moves 25 units.
• He goes past the edge of the screen by those 25 units.
• You undo 10 units of movement.
• He's still past the screen by 15!
Instead, set his position to the correct position upon collision.

This is example code that you'll need to adapt:
//Check if the player went past the boundry he wasn't supposed to cross.
if(player->get_x() > LeftScreenBoundry)
{
//Set the player's position to the right position.
player->set_x(LeftScreenBoundry);

//If the player still has velocity in the wrong direction...
if(player->get_x_velocity() > 0)
{
//...cancel further movement in that direction.
player->set_x_velocity(0);
}
}

Edited by Servant of the Lord, 30 September 2012 - 02:42 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

### #7MrChrisnis  Members   -  Reputation: 336

Like
0Likes
Like

Posted 30 September 2012 - 04:20 PM

Thanks guys for the help.

I am looking through to see if I can change it to get it working with the help you guys have posted.

As for the poor programming, sadly that is the way I have been taught to program up until now. My lecturer said that first year was just a "get it work no matter what the code is like" year, and that during this year, my second year, we learn why certain programming methods are better than others (Currently doing Data Structures and Software Engineering modules so they should help).

I shall also look through that angelfire link that was posted and see what I can learn

Up until now with this work, I have just been following through the worksheets we are given and doing what they say each week. I wish I could do more but baby steps eh?

Anyways, thank you again for your help with this

EDIT: Forgot to add @EngineProgrammer, yes. I have the SDL Files separate from my project and I link to them in the properties

Edited by MrChrisnis, 30 September 2012 - 04:47 PM.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS