Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Making the character jump

From: Flash Professional CS5: Creating a Simple Game for iOS Devices

Video: Making the character jump

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.

Making the character jump

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.

Show transcript

This video is part of

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Flash Professional CS5: Creating a Simple Game for iOS Devices.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.