Book Image

Creative Greenfoot: RAW

By : Michael Haungs
Book Image

Creative Greenfoot: RAW

By: Michael Haungs

Overview of this book

Table of Contents (17 chapters)
Creative Greenfoot
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Launching actors


We are going to take the preceding incomplete implementation and turn it into a game by adding jumping, bouncing, a particle effect, and bullets fired from a turret.

Gravity and jumping

Presently, our player character is stuck at the bottom of the screen. We are going to fill in the missing code in the Jumper class and the Bob class to enable our character to jump and finally have a way to reach the cupcake reward at the top of the screen. Jumping is applying a force to move an object upwards. We are also going to need a downwards force operating on the object, in order for it to fall back down. As in real life, we are going to call this force gravity. The changes to the Jumper class are so extensive that we are going to first look at the complete implementation and then discuss it afterwards. Here's the code:

import greenfoot.*;

public abstract class Jumper extends Actor
{
  protected int actorHeight;
  private int fallSpeed = 0;
  private boolean jumping = false;
  
  /...