Hi, this is my first post here.I'm developing my own 2D platform game. I'm only at the beginning. Now I'm trying to make the player jump.The jump itself works fine but I'm not able to stop the player. Here is some code:

jumpAnimator is an object that calculates the current y position. It works very well and the jump follows a realistic curve.I want the player jumping, falling and when he reaches the bottom of the screen, stopping.To stop the jump my idea was to set the _status variable to STOPPED when the player reaches the bottom of the screen.

Prepare a boolean, let's say if true then jump else not. Jump is assumed to be decreasing Y value every tick. Anytime when you need it to stop, just set the boolean to false. Ofc you need "if(jump) then jump" check every tick.

My first suggestion would be to create separate variables for your actions. There are many ways to do it, and you could go elegant and use bit-masks and enumerations (static finals) so that your _status variable could contain many statuses at the same time. You seem to have a jumpAnimator object, so I'm guessing you may just keep your variables in there. Since games are time/frame based, you'll probably want to allow impulse for a certain amount of time, and after that, you could switch into fall or no-collision mode. You can end fall mode when a collision occurs. You could stop your jumps on collisions, but for certain collision types you may not want that behavior, say if you jump through a collectible object.

Jumping can require a lot of checking. In games like Super Mario Bros. you jump higher based on how long you hold down the button, and if you end up hitting the ground while still having the button held down, you need to account for that too so the player doesn't repeatedly jump. If the player is in the air you don't want them to jump again on repeated button presses unless you allow a double jump, or are in water and swimming.

You should have a maximum impulse that you allow, and if you reach that, the jump is over until the button is let go and it is established your are on the ground again. If you collide with an object of the right type, the jump is over. I keep a variable called jump_counter to know how many frames in I am so I can keep track of the impulse. At first you might not even want to distinguish between top and bottom collisions. In the end the world just needs to work, and that can be very difficult.

The best advice I could give is to spend the time and figure out why it doesn't work. It isn't particularly helpful advice, but game programming can be done in wildly different ways, and as long as it works it is right. You should view yourself as the guru, because ultimately you will be providing most of the answers.

Some possibilities:* You could set up a state machine for this, with states for MOVING, STOPPING, FALLING, etc. instead of numerous if-statements* Do collision checking with the environment instead using coordinates.* Before updating positions, let the collision detector verify the new position, upon approval do the update* Calculate collision check points of your game entities, based on the current entities heading direction, like if someone is moving to the left, check the right hand side

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.

In my top-down-shooter there are entities like droids and bullets.Each entity is always in one state like moving or waiting.Basic collision detection for example, is the same for both kinds of entities, so the detection invocation goes into the parent state class. But the reaction on collision is different: droids just stop at walls whereas bullets explode. And if a droid happens to run into a closed door, the door shall be opened.So, there are additional inherited state classes for droids and bullets for handling events.Relatively easy to implement different behaviours and easier to test.

Look up for finite state machines or keep asking

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.

I googled up "finite state machines" and now I'm asking. (dear administrator, smileys in this site are ugly )I found some examples on how to implement finite state machines in Java, but how can I implement them in a game? Can you (or someone else) give me a 10-lines example for the states JUMPING, FALLING, LANDED, please?

Actor#act() is called repeatedly from the game loopIn State#process() do whatever you need to do: update actor positions, do collision checks, perform state changes, look for the meaning of life...

For example, a FallingState would calculate a new y-coordinate, asks the opinion of the collision detector about. In case of ground collision, a state change would be done:

1 2

StoppedStatestate = newStoppedState();actor.setState(state);

Now, in StoppedState#enter() any movement/velocity/heading would be cleared, and in StoppedState#process() no position updates are necessary. But, in case of incoming input events and upon approval of the collision detector, a state change to WalkingState or whatever is done.

Input events should be delegated from actor to state in order to keep the state encapsulated.

Basic principle is very simple. However, as always, details can be tricky

Lethal Running - a RPG about a deadly game show held in a futuristic dystopian society.

Is it a good thing to implement the frame management inside a State object?

I mean...

When the player jumps, the jump animation is needed. When the player falls, the fall animation is needed. The jump and fall animation are 4-5 frames long. I wonder if it is a good thing to encapsulate the frame-changing mechanism inside the State object. The State interface will be like this:

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org