Nick
2004.08.19, 04:21 AM
I'm thinking of entering a small skateboarding game in for uDevGames. I've been able to easily use what I have of my 2D sidescrolling engine and use it well with some tweaking. I need to find a way to have the spacebar be the jump button. But instead of pushing spacebar to jump, I want the player to hold the spacebar, have it increase a variable to represent the height of the jump, and when the player releases the spacebar the character jumps. I of course want a limit on this height. I've tried a few things and I can only get one jump out of the character before the code stops working. It probably has to do with some of my bool values. Below is the code I think you'll find necessary to help me (if you feel like doing so):
(Init.cpp)
bool upPressed = false , downPressed = false , leftPressed = false , rightPressed = false ;
bool spacePressed = false, iPressed = false;
/*
HandleKeyPressEvent
handles the key presses of the keyboard
*/
void HandleKeyPressEvent(SDL_keysym * keysym)
{
switch(keysym -> sym) // which key have we got
{
case SDLK_ESCAPE:
Quit(0);
case SDLK_UP :
upPressed = true;
break;
case SDLK_DOWN :
downPressed = true;
break;
case SDLK_RIGHT :
rightPressed = true;
break;
case SDLK_LEFT :
leftPressed = true;
break;
case SDLK_SPACE :
spacePressed = true;
break;
case SDLK_i :
iPressed = !iPressed;
break;
default: // any other key
break; // nothing to do
}
}
/*
HandleKeyReleaseEvent
handles the key releases of the keyboard
*/
void HandleKeyReleaseEvent(SDL_keysym * keysym)
{
switch(keysym -> sym) // which key have we got
{
case SDLK_UP :
upPressed = false;
break;
case SDLK_DOWN :
downPressed = false;
break;
case SDLK_RIGHT :
rightPressed = false;
break;
case SDLK_LEFT :
leftPressed = false;
break;
case SDLK_SPACE :
spacePressed = false;
break;
default: // any other key
break; // nothing to do
}
}
(Character.h)
#ifndef __CHARACTER__
#define __CHARACTER__
#include "ActiveObject.h"
#include "Obstacle.h"
/*
Sets out functions and variables responsible for maintaining a user controlled character
*/
enum { GOING_UP, COMING_DOWN, AT_REST }; //constants to set the jump direction
class CCharacter : public CActiveObject
{
public:
//our constructor takes the coordinates, dimensions, mass, and speed along the x and y axis
CCharacter(float x, float y, float z, float w, float h, float d, float m);
void SetSpeedX(float m) { mSpeedX = m; } //sets the speed along the x axis
void SetSpeedY(float m) { mSpeedY = m; } //sets the speed along the y axis
void SetSpeedZ(float m) { mSpeedZ = m; } //sets the speed along the z axis
float GetSpeedX() { return mSpeedX; } //returns the current speed of the object
float GetSpeedY() { return mSpeedY; } //returns the current speed of the object
float GetSpeedZ() { return mSpeedZ; } //returns the current speed of the object
void AddSpeedX(float m) { mSpeedX += m; } //adds to the speed along the x axis
void AddSpeedY(float m) { mSpeedY += m; } //adds to the speed along the y axis
void AddSpeedZ(float m) { mSpeedZ += m; } //adds to the speed along the z axis
void CheckCollision(CObstacle *obstacle); //checks collision with another active object
void Jump(float s); //makes the character jump
void Update(float s); //updates the character
void CheckForMovement(float s); //checks for keyboard input
void CheckGravity(float s); //uses gravity on the character
void CheckWorld(); //checks the character to be in the world
private:
float mSpeedX, mSpeedY, mSpeedZ; //values of the speed along each axis
float mCameraX, mCameraY; //values of the x,y coordinate for the camera
float mPeak; //jump peak
float mJumpDirection; //going to, at, or coming down from the peak
bool mJumping; //is the character jumping?
};
#endif
(Character.cpp)
extern bool leftPressed,rightPressed,upPressed,downPressed;
extern bool spacePressed,iPressed;
bool spaceReleased;
bool mPeakAddY;
float mPeakFigure;
void CCharacter::Jump(float s)
{
if (mJumpDirection == GOING_UP) //if still going up
{
SetDescent(0); //remove the descent
AddY(mSpeedY * s); //add to the Y coordinate
if (GetY() >= mPeak) //if above or at peak
{
mJumpDirection = COMING_DOWN; //set direction to coming down
}
}
}
void CCharacter::CheckForMovement(float s)
{
if ((GetDescent() == 0) && ((mJumpDirection != GOING_UP))) //if not going up and not descending
{
mJumpDirection = AT_REST; //must be at rest
}
if (spacePressed && mJumpDirection==AT_REST)
{
if (mPeakAddY)
{
mPeakFigure += GetY();
mPeakAddY = false;
}
mPeakFigure += ( .4 * s * GetH() );
}
if (!spacePressed && mPeakFigure!=0)
{
mPeakAddY = true;
spaceReleased = true;
}
if (spaceReleased && !mJumping && (mJumpDirection == AT_REST)) //if 'space' is pressed and at rest
{
mPeak = mPeakFigure;
mPeakFigure = 0;
mJumpDirection = GOING_UP; //set direction to going up
mJumping=true; //yes we are jumping
spaceReleased = false;
}
[more key checking and close of the function]
void CCharacter::Update(float s)
{
CheckForMovement(s); //first check for any keypresses by the player
if (mJumping){Jump(s);} //if jumping run through the jump function
AddX(mSpeedX * s);
AddSpeedX( -s * .9 );
if (mSpeedX < 0) { SetSpeedX(0); }
CheckGravity(s);
CheckWorld();
}
(main.cpp)
Player.Update(g_FrameInterval); //frame independent movement
I understand that this may not be the clearest of help asking posts (it is 3:20 am here) but I hope that if you want to help me, you have the ability with the given information to do so. If you feel that I should figure it out on my own, please keep your opinion to yourself and do not reply. I, as always, will continue to try things out and if I figure it out I apologize for any inconvenience I will have caused you for answering this post. Thanks in advance.
(Init.cpp)
bool upPressed = false , downPressed = false , leftPressed = false , rightPressed = false ;
bool spacePressed = false, iPressed = false;
/*
HandleKeyPressEvent
handles the key presses of the keyboard
*/
void HandleKeyPressEvent(SDL_keysym * keysym)
{
switch(keysym -> sym) // which key have we got
{
case SDLK_ESCAPE:
Quit(0);
case SDLK_UP :
upPressed = true;
break;
case SDLK_DOWN :
downPressed = true;
break;
case SDLK_RIGHT :
rightPressed = true;
break;
case SDLK_LEFT :
leftPressed = true;
break;
case SDLK_SPACE :
spacePressed = true;
break;
case SDLK_i :
iPressed = !iPressed;
break;
default: // any other key
break; // nothing to do
}
}
/*
HandleKeyReleaseEvent
handles the key releases of the keyboard
*/
void HandleKeyReleaseEvent(SDL_keysym * keysym)
{
switch(keysym -> sym) // which key have we got
{
case SDLK_UP :
upPressed = false;
break;
case SDLK_DOWN :
downPressed = false;
break;
case SDLK_RIGHT :
rightPressed = false;
break;
case SDLK_LEFT :
leftPressed = false;
break;
case SDLK_SPACE :
spacePressed = false;
break;
default: // any other key
break; // nothing to do
}
}
(Character.h)
#ifndef __CHARACTER__
#define __CHARACTER__
#include "ActiveObject.h"
#include "Obstacle.h"
/*
Sets out functions and variables responsible for maintaining a user controlled character
*/
enum { GOING_UP, COMING_DOWN, AT_REST }; //constants to set the jump direction
class CCharacter : public CActiveObject
{
public:
//our constructor takes the coordinates, dimensions, mass, and speed along the x and y axis
CCharacter(float x, float y, float z, float w, float h, float d, float m);
void SetSpeedX(float m) { mSpeedX = m; } //sets the speed along the x axis
void SetSpeedY(float m) { mSpeedY = m; } //sets the speed along the y axis
void SetSpeedZ(float m) { mSpeedZ = m; } //sets the speed along the z axis
float GetSpeedX() { return mSpeedX; } //returns the current speed of the object
float GetSpeedY() { return mSpeedY; } //returns the current speed of the object
float GetSpeedZ() { return mSpeedZ; } //returns the current speed of the object
void AddSpeedX(float m) { mSpeedX += m; } //adds to the speed along the x axis
void AddSpeedY(float m) { mSpeedY += m; } //adds to the speed along the y axis
void AddSpeedZ(float m) { mSpeedZ += m; } //adds to the speed along the z axis
void CheckCollision(CObstacle *obstacle); //checks collision with another active object
void Jump(float s); //makes the character jump
void Update(float s); //updates the character
void CheckForMovement(float s); //checks for keyboard input
void CheckGravity(float s); //uses gravity on the character
void CheckWorld(); //checks the character to be in the world
private:
float mSpeedX, mSpeedY, mSpeedZ; //values of the speed along each axis
float mCameraX, mCameraY; //values of the x,y coordinate for the camera
float mPeak; //jump peak
float mJumpDirection; //going to, at, or coming down from the peak
bool mJumping; //is the character jumping?
};
#endif
(Character.cpp)
extern bool leftPressed,rightPressed,upPressed,downPressed;
extern bool spacePressed,iPressed;
bool spaceReleased;
bool mPeakAddY;
float mPeakFigure;
void CCharacter::Jump(float s)
{
if (mJumpDirection == GOING_UP) //if still going up
{
SetDescent(0); //remove the descent
AddY(mSpeedY * s); //add to the Y coordinate
if (GetY() >= mPeak) //if above or at peak
{
mJumpDirection = COMING_DOWN; //set direction to coming down
}
}
}
void CCharacter::CheckForMovement(float s)
{
if ((GetDescent() == 0) && ((mJumpDirection != GOING_UP))) //if not going up and not descending
{
mJumpDirection = AT_REST; //must be at rest
}
if (spacePressed && mJumpDirection==AT_REST)
{
if (mPeakAddY)
{
mPeakFigure += GetY();
mPeakAddY = false;
}
mPeakFigure += ( .4 * s * GetH() );
}
if (!spacePressed && mPeakFigure!=0)
{
mPeakAddY = true;
spaceReleased = true;
}
if (spaceReleased && !mJumping && (mJumpDirection == AT_REST)) //if 'space' is pressed and at rest
{
mPeak = mPeakFigure;
mPeakFigure = 0;
mJumpDirection = GOING_UP; //set direction to going up
mJumping=true; //yes we are jumping
spaceReleased = false;
}
[more key checking and close of the function]
void CCharacter::Update(float s)
{
CheckForMovement(s); //first check for any keypresses by the player
if (mJumping){Jump(s);} //if jumping run through the jump function
AddX(mSpeedX * s);
AddSpeedX( -s * .9 );
if (mSpeedX < 0) { SetSpeedX(0); }
CheckGravity(s);
CheckWorld();
}
(main.cpp)
Player.Update(g_FrameInterval); //frame independent movement
I understand that this may not be the clearest of help asking posts (it is 3:20 am here) but I hope that if you want to help me, you have the ability with the given information to do so. If you feel that I should figure it out on my own, please keep your opinion to yourself and do not reply. I, as always, will continue to try things out and if I figure it out I apologize for any inconvenience I will have caused you for answering this post. Thanks in advance.