Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


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.

  • You cannot reply to this topic
9 replies to this topic

#1 warnexus   Prime Members   -  Reputation: 1474

Like
0Likes
Like

Posted 03 June 2014 - 03:56 PM

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


Edited by warnexus, 03 June 2014 - 04:41 PM.


Sponsor:

#2 DiegoSLTS   Members   -  Reputation: 1796

Like
0Likes
Like

Posted 03 June 2014 - 04:34 PM

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?


Edited by DiegoSLTS, 03 June 2014 - 04:34 PM.


#3 ultramailman   Prime Members   -  Reputation: 1582

Like
2Likes
Like

Posted 03 June 2014 - 04:39 PM


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;
}

Edited by ultramailman, 03 June 2014 - 04:52 PM.


#4 warnexus   Prime Members   -  Reputation: 1474

Like
0Likes
Like

Posted 03 June 2014 - 04:41 PM

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!


Edited by warnexus, 03 June 2014 - 05:25 PM.


#5 BeerNutts   Crossbones+   -  Reputation: 2980

Like
0Likes
Like

Posted 04 June 2014 - 09:10 AM

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)

#6 frob   Moderators   -  Reputation: 22271

Like
3Likes
Like

Posted 04 June 2014 - 01:53 PM

 

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.


Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.

Also check out my personal website at bryanwagstaff.com, where I write about assorted stuff.


#7 warnexus   Prime Members   -  Reputation: 1474

Like
0Likes
Like

Posted 04 June 2014 - 02:27 PM

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;
         }
}


#8 warnexus   Prime Members   -  Reputation: 1474

Like
0Likes
Like

Posted 04 June 2014 - 04:04 PM

 

 

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



#9 slicer4ever   Crossbones+   -  Reputation: 3948

Like
2Likes
Like

Posted 04 June 2014 - 08:26 PM

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.

#10 BeerNutts   Crossbones+   -  Reputation: 2980

Like
2Likes
Like

Posted 05 June 2014 - 09:51 AM

 

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.


Edited by BeerNutts, 05 June 2014 - 01:41 PM.

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)




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