Start your free trial now, and begin learning software, business and creative skills—anytime, anywhere—with video instruction from recognized industry experts.

Start Your Free Trial Now

Making the character jump


From:

Flash Professional CS5: Creating a Simple Game for iOS Devices

with Todd Perkins

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.
please wait ...
Watch the Online Video Course Flash Professional CS5: Creating a Simple Game for iOS Devices
2h 8m Intermediate Feb 25, 2011

Viewers: in countries Watching now:

In this course, Todd Perkins shows how to use Flash Professional CS5 to create a game to play on iOS devices, utilizing device hardware like the accelerometer and integration with a local database. The course covers the game development from start to finish, including planning and preparation, character and background animation, creating interactions and scoring mechanism, and finally testing and publishing the app. Exercise files are included with the course.

Topics include:
  • Optimizing assets
  • Planning game play and viewing class structure
  • Making the character move and jump
  • Adding sound effects
  • Adding accelerometer support
  • Creating reusable classes
  • Looping background animations
  • Detecting collisions
  • Using a timer
  • Increasing scrolling speed
  • Acquiring testing certificates and provisioning profiles
  • Troubleshooting performance issues
  • Submitting to the App Store
Subjects:
3D + Animation Developer Web
Software:
Flash Professional iOS
Author:
Todd Perkins

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.

There are currently no FAQs about Flash Professional CS5: Creating a Simple Game for iOS Devices.

 
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

* Estimated file size

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

Upgrade to View Courses Offline

login

With our new Desktop App, Annual Premium Members can download courses for Internet-free viewing.

Upgrade Now

After upgrading, download Desktop App Here.

Course retiring soon

Flash Professional CS5: Creating a Simple Game for iOS Devices will be retired from the lynda.com library on September 17, 2015. Training videos and exercise files will no longer be available, but the course will still appear in your course history and certificates of completion.


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 ?

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:

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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Start your FREE 10-day trial

Begin learning software, business, and creative skills—anytime,
anywhere—with video instruction from recognized industry experts.
lynda.com provides
Unlimited access to over 4,000 courses—more than 100,000 video tutorials
Expert-led instruction
On-the-go learning. Watch from your computer, tablet, or mobile device. Switch back and forth as you choose.
Start Your FREE Trial Now
 

A trusted source for knowledge.

 

We provide training to more than 4 million people, and our members tell us that lynda.com helps them stay ahead of software updates, pick up brand-new skills, switch careers, land promotions, and explore new hobbies. What can we help you do?

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.