Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
The next step in creating the character is making him jump. Again, we will add all that functionality to the Boarder class. We'll start by creating a few properties. Right below where you create the Accelerometer property, create another property called speedY, data type that to a Number, and set the value equal to zero. Create another property called groundY:Number, also = 0. Create another property called gravity.
That'll be a Number and we'll set it = 1.5. This is going to represent well gravity, and speedY is going to represent the vertical speed and groundY is going to represent the position of the ground. So we can detect that when the character jumps. On the next line we are going to create an object so we can dispatch an event when the character lands. So public static const. This is a value that can't change and it's static, so it's actually attached to the class and not the instance.
So we access it by class name.property and not instance name.property. So here we are going to type LANDED, all caps, datay type it to a String and we will set it = landed, all lowercase. We can use this value to dispatch an event later on if we want to. Finally, we will create one more property also public and we will call this jumpPower, data type it to a Number, and set the value equal to 20.
Now this represents the force being applied to the character when he jumps. So if you change the number, you can make him have a higher jump or a lower jump. Now scroll down to the allLoaded method. Right above acc.addEventListener, I'm going to say groundY = y. So we are setting the groundY property's value to the current position of the player. So that represents the ground. Keep in mind you don't want to put this in the constructor because it's possible that the code may run before the object gets added to the stage and then you have the problem that you're trying to modify a property that doesn't yet exist.
So it's best to have it inside of the added to stage handler. So go to the next line and type stage.addEventListener and the event is going to be MouseEvent.MOUSE_DOWN and this is what we are going to do to handle the jump. So we will create a method, so type Comma and then space, and type touchStarted. Now notice event is MOUSE_DOWN but I'm calling the function touchStarted.
Flash actually converts mouse events to touch events when you export the device to iOS. So you actually don't have to add any additional code to support touches. So let's define the touchStarted method. Scroll down below updateSpeed and create a private method called touchStarted. Make sure to make it receive an event object with the data type of MouseEvent, :void for no return data, and some curly braces.
In here we will run a method called jump. So just type jump and some parentheses and Semicolon. Right below touchStarted we will define the jump method. No parameters, no return data. In the jump method we want to make the player jump; however, we do not want to make the player jump all the time. We only want the player to jump, if he is on the ground. So the first thing we are going to do is check to see if the player is on the ground. Let's type an if statement and inside of the parentheses check to see if the player's Y position is equal to, that's two equal signs, groundY.
If so, then we can make the player jump by setting speedy, make sure you capitalize the Y, = -jumpPower. Remember the value is negative because we want the player to move upward. So negative values in Flash move things up and positive values move them down. So we set the speedY property, and then we are going to need to change the update method to support the vertical movement. So scroll up to the update method. Right below where you set the x value in there, create a new line and set the Y value by typing Y += speedY.
Let's save the file and test the movie to see what we have so far. (Music Playing) So you should be able to click and see the character jump. Notice that he just flies off the string. That's because we haven't yet applied gravity. So, let's take a look at how to do that. Right below the code where I check to see if the accelerometer is muted, I'm going to add a line of code that applies gravity. So I am going to type speedY += gravity.
So we are going to apply gravity to the character now. Save the file, and test the application again to watch gravity be applied. (Music Playing) Notice that the character just falls straight down. So we need to control it so that the character does not go below the ground. To do that, right below the line of code we just wrote, create a conditional statement. So type if, some parentheses, and some curly braces. There we are going to check to see if the character's Y position is greater than groundY.
So if y > groundY. So if the player is below where it should be on the ground, then what we are going to do is set y = groundY. So the player can't go below the ground. So now let's save the file again, and test it in the Flash Player. (Music Playing) So I can click to jump and he doesn't go below the ground. If I click a whole bunch of times, you'll notice that he doesn't jump when I click.
He just only jumps when he is on the ground. And here you can actually dispatch an event for when the player lands. I am not going to write that code right now, but I'll just make a mental note of that if you ever wanted to track that for something else. So remember, to make the character jump you apply a gravitational force and you update the character's Y velocity. To keep him on the ground, you store the ground position in a property and set the character at that position if he goes below it.
Get unlimited access to all courses for just $25/month.Become a member
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.