Robot

**I. Introduction**
Dr. Horrible is pleased with you work. Now that you have proven your usefulness to him, he has unveiled his evil robot (based on his own evil gait). Dr. Horrible needs you to train his evil henchmen to deploy the evil robots by implementing an Evil Robot Training Simulator (TM) Mark I.

media type="custom" key="7727231"

**II. Learning Objectives**
//**Knowledge**// //**Skills**// By the end of the session you will be able to...
 * Function parameters - functions can be called with different parameters to change the functions behavior without change the function's code.
 * Getter functions - getter functions are special functions who take no parameter and just return one value. They are a kind of syntactical sugar that allows you to access a function like you are accessing a variable.
 * Setter functions - setter functions are special functions, who take a single parameter as input. They are a kind of syntactical sugar that allows you to set some data using a function but make it look like you are setting a variable.
 * Static (class) properties - object instances can have data, and so can object classes.
 * Call an event listener with different parameters in Flex.
 * Access the static properties of a class

**III. Concepts**
Let's talk about accessing data and calling functions from object instances and classes.

Accessing data
Lets say you have a class like this: code public class Prius{

// This is an instance variable public var speed:Number;

} code To access the speed variable, you use code like this: code // Creates an instance of a prius var myCar:Prius = new Prius;

// Sets the speed of the prius myCar.speed = 60;

// Gets the value of currentSpeed to the speed of the prius (which will be 60). var currentSpeed:Number = myCar.speed; code That's how you get and set data (a variable) of an object instance.

Accessing function / using function parameters
Functions can be called with different parameters to change the functions behavior without change the function's code. For example, let's say you have a class like this: code public class Prius {

private var _speed:Number;

public function setSpeed(speed:Number):void{ _speed = speed; }

public function getSpeed:Number{ return _speed; } } code You can use the speed function like this: code var myCar:Prius = new Prius;

// Set the speed of the car to 15. myCar.setSpeed(15);

// Set the speed of the car to 60 myCar.setSpeed(60);

// Get the value of speed, which will be 60 var currentSpeed:Number = myCar.getSpeed; code Notice that you can "pass" the accelerate function different values for the speed parameter. This makes the car do different things even though you didn't change the code inside the setSpeed function.

The advantage of using a function to set data is that you can do other stuff inside the function each time the speed is set. For example, maybe you want to update the value of the speedometer each time someone changes the speed of the car.

**Accessing getter and setter functions**
Now lets say you have a class like this: code public class Prius{

private var _speed:Number;

public function get speed:Number{ return _speed; }

public function set speed(newSpeed:Number):void{ _speed = newSpeed; }

} code You use the getter and setter functions like this: code var myCar:Prius = new Prius;

//set the speed to 15 using a setter function myCar.speed = 15;

//set the speed to 60 using a setter function myCar.speed = 60;

//get the speed using a getter function var currentSpeed:Number = myCar.speed; code Notice that when you access setter and getter functions, it looks like you are accessing a variable, even though you are really calling a function. Setters and getters are a kind of "syntactical sugar" often used in Actionscript. It's not really necessary, but it makes the purpose of the code a more clear.

Accessing Static (class) properties
Object instances can have data. Object classes can also have data. Look at this Prius class below (in actionscript).

code format="actionscript3" public class Prius {

// This is a class variable because it's labeled static. // const means that the value is constant, i.e., you're not allowed to change it. static public const ENGINE_TYPE:String = "hybrid";

// This is a class variable because it's labeled static. // It's also labeled private which means code outside this file can't access it. static private var nextLicense:Number = 500;

public function Prius:void{ }

}

code

There are two pieces of class data: ENGINE_TYPE and nextLicense. The two variables: ENGINE_TYPE and _nextLicense are labeled as //static//, these are the class variables. Let's look at how you access this data.

code format="actionscript3" // Here we access a static (class) variable // This sets the value of type to "hybrid" var type:String = Prius.ENGINE_TYPE;

// Here we access a static variable var next:Number = Prius.nextLicense;

// nextLicense isn't a constant, so you can change it: Prius.nextLicense = 501;

code Notice that you access the static data (Prius.ENGINE_TYPE) by using the name of the class, rather than calling it from an object instance.

So there you have it. That's basically all there is to manipulating objects data and behavior!

**IV. Scenario**
In this project you need to create an interface for controlling an evil robot.

**Evil Robot Training Simulator Mark I**

Use Case 1. Henchmen trainee clicks the evil robot action 2. The evil robot does the evil action

Before you get started, there are some things you should know about the robot object:
 * The Robot class has a function called setAction(action:String), that takes a string as its input. Calling this function will animate the robot.
 * The values of string that setAction will accept are defined by static variables in the RobotAction class, specifically: RobotAction.STAND, RobotAction.CLAP, RobotAction.STOMP, RobotAction.JUMP.

**V. Program**
Here's the program you'll be writing: code format="mxml"  

 

 <![CDATA[ import edu.nu.easterday.robot.RobotAction;

private function action_clickHandler(action:String):void {               _robot.setAction(action); }

]]>          

 code

**VI. Self-explain (Try it!)**
Walk through each line of the program and try explaining what each line does.

**VII. Program Walkthrough (Try it!)**
To create the program, try the following steps:
 * Create a new flex project
 * Download the [[file:Robot.swc]] library file and put it in the lib directory
 * Add 4 buttons for each actions
 * Drag and drop the robot in the middle
 * Give the robot instance an id so that you can call it's functions in actionscript
 * Create event listeners for each of the buttons (have them all call the same function)
 * Rather than pass the event to the event listener, use the RobotAction constants
 * Write the code in the event listener to animate the robot

**VIII. On your own (Try it!)**
Try rewriting the Button program to use a single event listener, (or do something else cooler using the same concepts!)

**IX. Feedback**
Need examples/ quizzer for dot notation Get rid of the get/set section Need to explain casting Explain bugs: - import statements missing or misspelled - id property of robot missing

confusing where to put the robot.swc folder