Toxiclab.org

Switch and case statements and statements summary

11.2.2008, 20:39
Submited in: Flash | Total Views: 30655

This lesson will show you how to create switch and case statement and how to apply it on any object. You will also learn something about statements usually and I will also show you Statements summary.


Statements are language elements that perform or specify an action. For example, the return statement returns a result as a value of the function in which it executes. The if statement evaluates a condition to determine the next action that should be taken. The switch statement creates a branching structure for ActionScript statements.

Statements summary

Statement  Description
break Appears within a loop (for , for..in, do..while or while) or within a block of statements associated with a particular case within a switch statement.
case Defines a condition for the switch statement
class  Defines a custom class, which lets you instantiate objects that share   methods and properties that you define.
continue Jumps past all remaining statements in the innermost loop and starts the next iteration of the loop as if control had passed through to the end of the loop normally.
default Defines the default case for a switch statement.
delete Destroys the object reference specified by the reference parameter, and returns true if the reference is successfully deleted; false otherwise.
do..while Similar to a while loop, except that the statements are executed once before the initial evaluation of the condition.
dynamic Specifies that objects based on the specified class can add and access dynamic properties at runtime.
else Specifies the statements to run if the condition in the if statement returns false.
else if Evaluates a condition and specifies the statements to run if the condition in the initial if statement returns false.
extends Defines a class that is a subclass of another class; the latter is the superclass.
for Evaluates the init (initialize) expression once and then starts a looping sequence.
for..in Iterates over the properties of an object or elements in an array and executes the statement for each property or element.
function Comprises a set of statements that you define to perform a certain task.
get Permits implicit getting of properties associated with objects based on  classes you have defined in external class files.
if Evaluates a condition to determine the next action in a SWF file.
implements Specifies that a class must define all the methods declared in the interface (or interfaces) being implemented.
import  Lets you access classes without specifying their fully qualified names.
interface Defines an interface.
intrinsic Allows compile-time type checking of previously defined classes.
private Specifies that a variable or function is available only to the class that declares or defines it or to subclasses of that class.
public Specifies that a variable or function is available to any caller.
return Specifies the value returned by a function.
set Permits implicit setting of properties associated with objects based on classes you have defined in external class files.
set variable Assigns a value to a variable.
static Specifies that a variable or function is created only once per class rather than being created in every object based on that class.
super Invokes the superclass' version of a method or constructor.
switch Creates a branching structure for ActionScript statements.
throw Generates, or throws , an error that can be handled, or caught , by a catch{}code block.
try..catch..finally Enclose a block of code in which an error can occur, and then respond to the error.
var Used to declare local or Timeline variables.
while Evaluates a condition and if the condition evaluates to true , runs a statement or series of statements before looping back to evaluate the condition again.
with Lets you specify an object (such as a movie clip) with the object parameter and evaluate expressions and actions inside that object with the statement(s) parameter.


Switch/case statements

Example:



Step 1

Create a new flash document. Press Ctrl+J key on the keyboard (Document Properties) and set the Width of your document to 300 pixels and the Height to 200 pixels. Select any color as background color.Set your Flash movie's frame rate to 28 and click ok.





Step 2


After that, draw or Import (Ctrl+R) any object which you like to use for this example.

Step 3

While the object is still selected, press F8 key (Convert to Symbol) to convert this object into a Movie Clip Symbol.



Step 4

While the new made Movie Clip is still selected, go to the Properties Panel below the stage. On the left side, You will find the Instance name input field there. Call this Movie Clip "object".



Step 5

Create after that a new layer above the layer 1 and name it action script.

Step 6

Select the first frame of layer action script and go to the action script panel (F9) below the stage. Then, enter this code inside the actions panel:

this.onKeyDown = function() {
var code:Number = Key.getCode();
switch(code) {
case Key.LEFT:
moveLeft();
break;
case Key.RIGHT:
moveRight();
break;
case Key.UP:
moveUp();
break;
case Key.DOWN:
moveDown();
break;
}
} Key.addListener(this);

var SPEED:Number = 5;

function moveLeft():Void {
var leftWall:Number = 0;
if(object._x > leftWall) {
object._x -= SPEED;
}else {
object._x = leftWall;
}
}

function moveRight():Void {
var rightWall:Number = Stage.width - object._width;
if(object._x < rightWall) {
object._x += SPEED;
}else {
object._x = rightWall
}
}

function moveUp():Void {
var topWall:Number = 0
if(object._y > topWall) {
object._y -= SPEED;
}else {
object._y = topWall;
}
}

function moveDown():Void {
var bottomWall:Number = Stage.height - object._height;
if(object._y < bottomWall) {
object._y += SPEED;
}else {
object._y = bottomWall;
}
}

Test your Movie (Ctrl+Enter)


We're done!

Download example

Are you a website owner?
Click here to register and submit your tutorials!

Browse by category


Most popular tutorials