How to make a character fall off the block after it has been on it

Started by
8 comments, last by BeerNutts 9 years, 10 months ago

Ignore the title: the actual title is here. How to prevent the character from jumping again if it is already in a "jumping state"?


if(spacePressed)
{
state = ActionState.JUMPING;
}


public enum ActionState {
 
RUNNING,
JUMPING 
}

Code is in Java

Advertisement

It highly depends on more parts of your code (those few lines are really not useful, it's just one line of code and an enum), but since you're working with states you must define what state transitions are valid and what triggers them. When you press the "jump" key check if the state is a valid state to trasition to jumping.

Also... that enum doesn't have a JUMPING element, is that your actual code?


How to prevent the character from jumping again if it is already in a "jumping state"?

Your question kind of answers your question.

Make separate states for "initiating the jump" and "jumping" (I like to call it "falling").


if(spacePressed && (state != ActionState.JUMPING))
{
state = ActionState.JUMP;
}
else if(state == ActionState.JUMP) // .... and then in the next frame
{
state = ActionState.JUMPING;
}

It highly depends on more parts of your code (those few lines are really not useful, it's just one line of code and an enum), but since you're working with states you must define what state transitions are valid and what triggers them. When you press the "jump" key check if the state is a valid state to trasition to jumping.

Also... that enum doesn't have a JUMPING element, is that your actual code?

Strange. I can use ActionState.JUMPING even though I did not define it in my ActionState class. Well I added the JUMPING to the ActionState class now.

Thanks. The problem has been fixed!

Warnexus, I assume, if your player steps off a platform and is falling, he won't be able to jump while falling right? What I'm trying to get at is, you might need a generic solution which works whether you've just jumped or you're falling.

I'd suggest looking at trying to use a IsOnGround flag, unless you have a special can jump in mid air ability.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Your question kind of answers your question.

Make separate states for "initiating the jump" and "jumping" (I like to call it "falling").


if(spacePressed && (state != ActionState.JUMPING))
{
state = ActionState.JUMP;
}
else if(state == ActionState.JUMP) // .... and then in the next frame
{
state = ActionState.JUMPING;
}

Good approach, but easily improved. There might be more situations. Jumping, falling, climbing, swimming, and more.

To make it more general:


if(spacePressed && CanJump())

Then add a short function that checks if they can jump. Perhaps they must be actually on the ground, must not be burdened or stunned, or otherwise incapable of jumping.

And to improve it yet again, why is jumping related to space? What if they reassigned the keys, or are using an xbox controller or a mouse to control? Translate input from buttons to events as soon as you are able. That gives this:


if(jumpPressed && CanJump()) {...}

which makes the intent even more clear to anyone maintaining the code later.

Warnexus, I assume, if your player steps off a platform and is falling, he won't be able to jump while falling right? What I'm trying to get at is, you might need a generic solution which works whether you've just jumped or you're falling.

I'd suggest looking at trying to use a IsOnGround flag, unless you have a special can jump in mid air ability.

I managed to fix it. I decided to write a logic that tells the game that the character is off the block. So I can do something like this:

if(!onBlock && getY() + idleRightAnim.getHeight() < Screen.HEIGHT - 35)
{
         if(state != ActionState.JUMPING)
         {
              state = ActionState.FALLING;
         }
}

Your question kind of answers your question.

Make separate states for "initiating the jump" and "jumping" (I like to call it "falling").


if(spacePressed && (state != ActionState.JUMPING))
{
state = ActionState.JUMP;
}
else if(state == ActionState.JUMP) // .... and then in the next frame
{
state = ActionState.JUMPING;
}

Good approach, but easily improved. There might be more situations. Jumping, falling, climbing, swimming, and more.

To make it more general:


if(spacePressed && CanJump())

Then add a short function that checks if they can jump. Perhaps they must be actually on the ground, must not be burdened or stunned, or otherwise incapable of jumping.

And to improve it yet again, why is jumping related to space? What if they reassigned the keys, or are using an xbox controller or a mouse to control? Translate input from buttons to events as soon as you are able. That gives this:


if(jumpPressed && CanJump()) {...}

which makes the intent even more clear to anyone maintaining the code later.

spacePressed is a boolean flag that is set to true when the space key is pressed

spacePressed is a boolean flag that is set to true when the space key is pressed


what frob is saying is don't directly tie your logic to your input, instead make it so the input tells you what action is to be performed when it's pressed, then you can make it so thet you can change key-bindings much more easily.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Warnexus, I assume, if your player steps off a platform and is falling, he won't be able to jump while falling right? What I'm trying to get at is, you might need a generic solution which works whether you've just jumped or you're falling.

I'd suggest looking at trying to use a IsOnGround flag, unless you have a special can jump in mid air ability.

I managed to fix it. I decided to write a logic that tells the game that the character is off the block. So I can do something like this:


if(!onBlock && getY() + idleRightAnim.getHeight() < Screen.HEIGHT - 35)
{
         if(state != ActionState.JUMPING)
         {
              state = ActionState.FALLING;
         }
}

That's a little better, but, you still should be trying to use a more generic solution. Why have a jumping state, or a falling state? Why not determine if a player is jumping or falling based on his Y velocity. You can use some simple physics to figure out if your character is jump, falling or standing.

Here's simple code to get you thinking about other options


// some numbers I made up
#define GRAVITY 5.0
#define JUMP_Y_VELOCITY -50.0
#define MOVE_VELOCITY 8.0
 
void Character::Update()
{
  int tempYVelocity = m_Velocity.y;
  int tempXVelocity = m_Velocity.x;
  int tempYPos = m_Pos.y;
  int tempXPos = m_Pos.x;
 
  // all input's require used to be on the ground (unless you want character to move while in the air)
  if (m_IsOnGround)
    // if user has pressed Jump, and he's on the ground, jump
    if (Input.KeyPressed(JUMP_KEY)) {
      tempYVelocity = JUMP_Y_VELOCITY;
    }
 
    // Change X velocity based on key pressed
    if (Input.KeyPressed(MOVE_LEFT_KEY)) {
      tempXVelocity = -MOVE_VELOCITY;
    }
    if (Input.KeyPressed(MOVE_RIGHT_KEY)) {
      tempXVelocity = MOVE_VELOCITY;
    }  
  }
  // move character horizontally
  tempXPos += tempXVelocity
 
  // check if moving him caused a collision
  if (CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y)) {
    // reset location
    tempXPos = m_Pos.x;
  }
 
  // always apply gravity to the characters
  tempYVelocity += GRAVITY;  
  tempYPos += tempYVelocity;
  
  // after moving Y position, check if we've hit anything, using location and size of the character
  bool isYcollision = CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y);
 
  // check if the velocity is downwards (positive), and if the player collided with anything, then we're on the ground; reset velocity to 0
  // if velocity is downwards, then we're falling...
  if (tempYVelocity > 0) {
    // if the player collided with anything, then we're on the ground; reset velocity to 0, and move player back to original posiiton
    if (isYcollision) {
      m_IsOnGround = true;
      tempYPos = m_Pos.y;
      tempYVelocity = 0.0;
    }
    else {
      // we're falling
      m_AnimationState = FALLING;
      m_IsOnGround = false;
    }
  }
   else {
    // else we're jumping, but check if we hit our head on anything
    if (isYcollision) {
      // reset YVelocity to 0, but change state to falling
      tempYVelocity = 0.0;
      m_AnimationState = FALLING;
  }
  else {
    // we're going up, so show jumping
    m_AnimationState = Jumping;
    m_IsOnGround = false;
  }
 
 
  // set all the member variables to temp variables
  m_Velocity.y = tempYVelocity;
  m_Pos.y = tempYPos;
  m_Velocity.x = tempXVelocity;
  m_Pos.x = tempXPos;
}

This is just a sample to get you to think about generic ways of determining what a character is doing.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

This topic is closed to new replies.

Advertisement