• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Nicholas Kong

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

9 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

 

 

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

0

Share this post


Link to post
Share on other sites

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.
2

Share this post


Link to post
Share on other sites

 

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
2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0