DriftMove Class:


Download DriftMove Class

To use DriftMove in a project, first save the class in the folder for your project. To ensure that you are downloading the file instead of showing it in your browser, select-click the file and choose 'Save link as...'

After you've saved your link in the folder, close your project and re-open it and you will see the DriftMove class in your Actor classes. If you don't see it, you have probably put the link in the wrong folder.

DriftMove allows an actor to move in a drifting way and accelerate in different directions. It allows turning and acceleration using the arrows or the mouse. It also includes a method to wrap and bounce at the edge of the screen. It also includes a fall method, causing the actor to fall. It has a possible maxSpeed argument, which can keep your actor from accelerating to an out-of-control speed. (Future versions will include methods allowing the actor to land on an object and reflect off an object.

To use the DriftMove class, create a DriftMove object in the beginning of your actor class, using the following code:

DriftMove driftmove = new DriftMove(this);

By default your actor can go any speed, so if you keep accelerating in the same direction your actor will soon be moving so fast you can't see it. To keep this from happening you can include the optional maxSpeed argument. For example, if you don't want your actor to be able to go faster than 6 pixels per turn, use the code:

DriftMove driftmove = new DriftMove(this, 6);

(Technically, the maxSpeed is the maximum speed in the x and y direction. So at a perfect diagonal the actor can go a bit more than the maxSpeed.)

To cause your actor to drift, you must call the drift() method in your act() method, using the following code:

driftmove.drift();

To cause your actor to turn and accelerate using the arrows, use the turnAccelArrows() method. The simple version of this method has no arguments and turns five degrees every time you press the arrows and accelerates at a rate of 0.2 pixels per turn every time you press the up arrow. To use this method use the code:

driftmove.turnAccelArrows();

The more advanced version of turnAccelArrows calls a decimal (double) argument for acceleration and an integer argument for turn speed. Left and right arrows rotate your actor and the up arrow accelerates it. For example, to make your actor accelerate at a rate of 0.1 pixels per turn when you press the up arrow and turn at a rate of 2 degrees per turn, you would use the code:

driftmove.turnAccelArrows(0.1, 2);

To cause your actor to turn and accelerate using the mouse, use the turnAccelMouse() method. This causes your actor to point towards the mouse at all times and accelerate whenever you press the mouse button. The simple version calls for no arguments and accelerates at a rate of 0.2 pixels per turn every time you press the mouse button. To use this, type the code:

driftmove.turnAccelMouse();

The more advanced version of turnAccelMouse() calls for a double argument to represent acceleration. For example, to make your actor accelerate 0.1 px/turn when you press the mouse, use the code:

driftmove.turnAccelMouse(0.1);

DriftMove features a fall() method that causes your actor to accelerate downward at the rate of the double argument you pass it. For example, to make your actor accelerate downward 0.05 pixels per turn per turn you can pass the argument:

driftmove.fall(0.05);

The bounceAtEdge() method makes you bounce when you hit the edge of the screen. It needs no arguments. To make your actor bounce at the edge of the screen use the code:

driftmove.bounceAtEdge();

The wrapAtEdge() method makes you wrap to the other side of the screen when you hit the edge of the screen. It needs noarguments. To make your actor bounce at the edge of the screen use the code:

driftmove.wrapAtEdge();

DriftMove also features the setSpeed() method, which allows you to set the ship's absolute speed in the x and y direction. This is useful if you want an object to be moving at the beginning of the game. So to make an object move at a rate of 5 upward and 3 to the right, you would use the code:

driftmove.setSpeed(-5, 3)

The setRandomSpeed() method sets the object at a random speed up to a maximum x and y speed you pass. For example, to set a random speed with a maximum of 4 in either direction, you would use the code:

driftmove.setRandomSpeed(4, 4);

The bounceOffObject() method is experimental and doesn't work perfectly. It will make an object bounce off another object of a certain class. It may bounce before it touches, and it may not bounce in certain circumstances. To make your actor bounce off an actor of class Balloon, use the code:

driftmove.bounceOffObject(Balloon.class);

@author Jim Stewart
@version 1.0