Arrays and For Loops Notes - Powerpoint

Download Report

Transcript Arrays and For Loops Notes - Powerpoint

For Loops & Arrays
my_rect_pink_mc.width = 40 my_rect_pink_mc.alpha = .5 trace
my_rect_pink_mc.x= 20 my_rect_pink_mc.y= 20 for (var i:int = 0; i
< 250; i++) {trace (i + ' this is the current loop')}function
moveMe(evt:Event):void
{//trace
('i
am
moving....');
star_fish.rotation
+=
10
if
(star_fish.hitTestObject(myFishy_mc))
{trace ('I was it')}stage.addEventListener(MouseEvent.MOUSE_MOVE,
followMouse); 20 for (var i:int = 0; i < 250; i++) trace
If(event.keyCode
==
7){myFishy_mc.x
-=
5;}
function
moveFish(thisFish:MovieClip,targetLocH:Number):void
import flash.events.KeyboardEvent;import flash.events.MouseEvent;
import flash.ui.Keyboard; package{ import
flash.display.Sprite;
public class FishClass extends Sprite {public
function
FishClass()20 for (var i:int = 0; i < 250; i++) {trace (i + '
this is the current loop'){
this.addEventListner
(MouseEvent.ROLL_OUT,
scaleMeDown)}private
function
scaleMeUp
(event:MouseEvent):void
if
(star_fish.hitTestObject(myFishy_mc)){trace
('I
was
it')
}}{this.scaleX = 1.5; this.scaleY = 1.5;
}private
function
scaleMeDown
(event:MouseEvent):void{this.scaleX
=
1;
my_rect_pink_mc.width = 40 my_rect_pink_mc.alpha = .5
my_rect_pink_mc.x= 20 this.scaleY = 1;}}} addEventListner
to perform repetitive tasks, we use a loop
a loop is type of statement that causes a block of code to be repeated
Repeat Loops
How to read this?
var i:int;
for (i = 0; i < 5; i++) {
trace( i);
}
the statements to repeat are listed between the brackets: { }
the keyword for starts the loop
the comparison (i < 5) indicates when to quit repeating the loop
the loop stops repeating when x is equal to 5 no longer less than 5
to make the loop count to 500, simply change 5 to 500
Repeat Loops
How to read this?
for (i = 0; i < 5; i++)
{
trace( i);
}
for (initialize; compare; update)
{
Statements
}
Repeat Loops
How to read this?
Initialize Variable staring a 0
Compare
for (i = 0; i < 5; i++)
{
trace( i);
}
Action to carry out
Update…….
Arrays
What is an Array?
an array is a list of data values
Means not having to make lots and lots of variables
fruit1 = "oranges"; // A single string value
fruit2 = "apples"; // Another string value
this code creates an array to store both values:
var fruitList = ["oranges", "apples"];
Arrays
What is an Array?
this code creates an array to store both values:
var fruitList = ["oranges", "apples"];
array constructor:
new Array("item1", "item2")
in ActionScript, arrays can contain any kind of data
shoppingList = ["oranges", 5, "apples", 2];
Arrays
shoppingList = ["oranges", 5, "apples", 2];
each item stored in an array is called an element
each element's position in the array is its index
indices start at 0
for example, here are the indices for shoppingList:
0: "oranges"
1: 5
2: "apples"
3: 2
the number of elements in an array is called its length
for example, the shoppingList's length is 4
Arrays
to set an element's value, use this syntax:
theArray[theElementIndex] = theValue;
for example:
// Make an array
var cities = ["Toronto", "Montreal", "Vancouver", "Tokyo"];
// cities is now: ["Toronto", "Montreal", "Vancouver", "Tokyo"]
// Set the value of the array's first element
// cities becomes ["London", "Montreal", "Vancouver", "Tokyo"]
cities[0] = "London";
Arrays & For Loops
var myArray:Array = new Array("a", "b", "c");
var i:int;
for (i = 0; i < (myArray.length); i++) {
trace(myArray[i]);
}
Will print into the output window
a
b
c
CODE 101- SYNTAX
Live Demo