Previously on the Chronicles of a Videogame in Java: How to Make the Character Move + Simple Animation.

The code discussed below is already available on my GitHub page at theBoyWithNoName (“jump” branch). Please feel free to download it to take a better look. Ask me anything about it.

Hi guys and welcome to the third part of my diary/tutorial about making a game in Java Swing. So today we’re not gonna make huge steps forward, but we’re gonna see how to make our little main character jump around a black screen (which will soon be a lot less black-ish, I promise). Before we start here’s another little mockup I drew about a week ago. Hopefully this is how the game will start to look in a week. Or a couple of weeks maybe. Check it out: mockup2 Not that bad, uh? Sadly for now we’ll have to settle with our “beautiful” black screen. Anyway let’s get into code business. At the end of the last part of the Chronicles I left you with a character able to just run around with a simple animation. Today we’re adding the simplest jump mechanic you can make in a Java game. So, here’s how it works when the player presses <jump>, established that we’re going to associate <jump> with the SPACE key:

  1. The KeyboardController detects a new SPACE key pressed and stores it in the activeKeys HashSet.
  2. The GameManager (main thread of the game) checks activeKeys continuously. When it finds a SPACE key in it, the GameManager tells the boy to jump (if he’s not already in the process of jumping).
  3. The boy, magically, jumps.

If you don’t remember how the GameManager and KeyboardController classes work, please check my last post. If you do, then take a look at how the GameManager has slightly changed since last time:

      public void run() {
               //updates the character movement if he's 'jumping'

               //manage the keys currently pressed


               try {
               } catch (InterruptedException e) {

	//the function manages the keys currently pressed associating concrete
	//actions to them
	private void manageKeys() {
		//get the currently pressed keys from the KeyboardController
		HashSet<Integer> currentKeys=KeyboardController.getActiveKeys();
		//manage the two possible run direction
			//move right
		} else if (currentKeys.contains(KeyEvent.VK_LEFT)){
			//move left
		} else if(currentKeys.isEmpty() && !boy.getJumping()){
			//if the player is not pressing keys, the protagonist stands still
                //if the character is not jumping, tell him to jump
		if(currentKeys.contains(KeyEvent.VK_SPACE)) {

As you can see, since the last post I only added a few lines of code in the GameManager class. This code you see up here is how things work from the perspective of the GameManager, but you may notice there’s no evidence of the actual jumping thing here. That’s because the GameManager does not move the character in my pattern, it simply tells the character to move. From the perspective of the Boy, infact, the first thing that happens is that he receives an “order” from the GameManager, that order is the one you see above at line 41: boy.jump(). Instead, here’s how the jump method looks like from the perspective of the character in the class named “Boy”:

        //called every time the player presses the jump key (SPACE for now)
	//if the character is not already jumping (boolean jumpin=true)
	public void jump() {
		//sets the jumping state to true
		//reinitialize the jump_count, useful to determine for how 
		//much time the character is going to stay in the air
		//sets the current jumping frame based on the last direction 
		} else {

As you can see, the function simply sets the jumping state to true and the jump_count to zero. But what the hell is a jump_count? The variable jump_count works with a static final integer called JUMP_COUNTER_THRESH: in particular the jump_count is incremented every time the main thread (GameManager) calls the checkState() function. And it goes on and on incrementing this variable until it reaches JUMP_COUNTER_THRESH. During this period of time, the position of the character on the y-axis gets smaller and smaller. We’re in the ascending phase of the jump. When the jump_count exceeds JUMP_COUNTER_THRESH, au contrair, the jump_count keeps going up but the position of the boy on the y-axis is incremented for the character is in the descending phase of the jump. It goes on incrementing until jump_count reaches JUMP_COUNTER_THRESH*2, then the jumping boolean is set to false and the count is reinitialized. The jump is over. Here’s how I put in code the just explained operation:

public void checkJumpState() {
					} else {
				boundingBox.setLocation(currentX, currentY);
			} else {
				boundingBox.setLocation(currentX, currentY);

Notice that the boundingBox, or the collision box of the protagonist as you may call it, needs to be updated even when jumping (lines 12 and 15). This is going to be crucial when we get to add mobs and obstacles to the game. Aaaaaaand that would be all for today’s diary. Once again, remember that this is not a proper tutorial, but just a “how I make things” kind of thing. So if you have suggestions or you don’t understand what I say, please comment below or get in touch with me on twitter. I’m also on tumblr, drawing stuff.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s