The Wanderer class

The Wanderer class

The Wanderer class will match the character’s intention with the user’s key presses. When the character changes directions, it will now orient toward that new direction. Additionally, the character’s internal timeline will only animate when the character is in motion.

1. Create a new class named Wanderer.

2. Save the ActionScript file as Wanderer.as in the exam- ples/com/anim/character folder.

3. Add the following highlighted code to have Wanderer subclass BoundedMover:

public class Wanderer extends BoundedMover {

Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques

4. Update the package statement with the following highlighted code to match the saved location of the

Wanderer.as file: package com.anim.character {

5. Add the following updatePosition method below the constructor method to override the method (of the

same name) in the superclass: override protected function

➥ updatePosition(e:Event):void {

super.updatePosition(e); if(vx == 0 && vy == 0){ this.stop(); } else { this.play(); } }

The only update that this new method implements to the superclass’s functionality is to either stop the animation on the symbol’s Timeline if the object has no

velocity (in any direction) or play the symbol’s anima- tion (mouth chomping in this example).

6. Add the following keyDown method below the updatePosition method:

override protected function ➥ keyDown(e:KeyboardEvent):void {

if (e.keyCode == Keyboard.LEFT) {

vx = -currentSpeed; this.rotation = 0; this.scaleX = -currentScaleX;

} else if (e.keyCode == Keyboard.RIGHT) {

vx = currentSpeed; this.rotation = 0; this.scaleX = currentScaleX;

} else if (e.keyCode == Keyboard.UP) {

vy = -currentSpeed; this.rotation = -90; this.scaleX = currentScaleX;

} else if (e.keyCode == Keyboard.DOWN) {

Chapter 3 Introduction to ActionScript Classes

vy = currentSpeed; this.rotation = 90; this.scaleX = currentScaleX;

The logic in this method is exactly the same as the Mover class. The only updates are to assign values to the rotation and scaleX properties. These assignments

are to orient the character to its direction. For exam- ple, since your character is facing right by default, you have to flip the character’s horizontal scaling to have the character face to the left. To face the character up or down, you need to use rotation . Both scaleX and

rotation are necessary, since only using rotation would

Figure 3.41 The character as shown if

cause the character to be upside down when traveling

only rotation were used rather than

to the left (Figure 3.41).

setting the scaleX property.

7. Ensure that the following import statements are included just inside the package declaration: import com.anim.character.BoundedMover;

import flash.events.KeyboardEvent; import flash.ui.Keyboard; import flash.events.Event;

The completed Wanderer class should read: package com.anim.character {

import com.anim.character.BoundedMover; import flash.events.KeyboardEvent; import flash.ui.Keyboard; import flash.events.Event;

public class Wanderer extends BoundedMover {

public function Wanderer() {

override protected function ➥ updatePosition(e:Event):void {

super.updatePosition(e);

Animation with Scripting for Adobe Flash Professional CS5 Studio Techniques

if(vx == 0 && vy == 0){ this.stop(); } else { this.play(); } }

override protected function ➥ keyDown(e:KeyboardEvent):void {

if (e.keyCode == Keyboard.LEFT) { vx = -currentSpeed; this.rotation = 0; this.scaleX = -currentScaleX;

} else if (e.keyCode == Keyboard.

➥ RIGHT) {

vx = currentSpeed; this.rotation = 0; this.scaleX = currentScaleX;

} else if (e.keyCode == Keyboard.UP) { vy = -currentSpeed; this.rotation = -90; this.scaleX = currentScaleX;

} else if (e.keyCode == Keyboard.DOWN) { vy = currentSpeed; this.rotation = 90; this.scaleX = currentScaleX;

8. Save your class, return to your WandererExample.fla document, and update the character symbol properties

to have a Class value of com.anim.character.Wanderer.

9. Save your document and test the movie. Move your character around and note how the character changes

direction when pressing a new arrow key and how the character stops animating when no keys are pressed (Figure 3.42).

Chapter 3 Introduction to ActionScript Classes

Figure 3.42 The Wanderer in action, chomping away and facing in the intended direction.

Now you’ll jump from the Wanderer to a much more advanced character. You can close any open Flash docu- ments or scripts.