120 likes | 226 Views
Repetition. Objectives of this topic:. You can …. Write a conditional statement in ActionScript. Create more interactive applications. Create a loop in AS3. Overview. This topic covers Loops Creating a Code Loop Using a Loop to Generate Instances of a Class
E N D
Objectives of this topic: You can … Write a conditional statement in ActionScript Create more interactive applications Create a loop in AS3
Overview This topic covers • Loops • Creating a Code Loop • Using a Loop to Generate Instances of a Class • Placing Instances Created by a Loop
Loops • A loop is a function in AS that runs as many times as you specify • A loop allows the designer to control exactly how many times the function runs • easily to go back and make a simple change to AS • Loops are powerful tools that make repetitive tasks, which would be monotonous in design mode
Creating a Code Loop • Create a variable or placeholder, that is the data type Number to stands as index in loops Example: var i:Number = 0; • The initial value of this variable is to be 0 (zero) • The index is the number of times the loop will run
Loop has a condition – means the loop will run as long as it less then certain times Example: i < 20; the loop will run as long as I less than 20
Use mathematical operator to increase the value of a variable Example: i++; means the value of i variable will increase by for every time the code runs - first time, i will equal to 0 - second time, i will equal to 1 and so on, all the way up to 19
for(var i:Number =0; i < 20; i++) { trace(i); } (Flash: Loops) • telling Flash that every time the codes run, trace the value of i • The trace statement will run over and over again until the loop condition, i < 20, is no longer true
In loop statement, we can change the index to run any number of times by changing the value in the condition
Generate Instances of a Class using a loop • Using code loop to drag instance of a class into the movie at the run time (Flash: Generating_Instances) • Steps to drag: • Right-click on the movie-clip in window library • Choose linkage properties • Select Export for ActionScript • To export the class at run time in order to communicate with it via AS • Change the class name to the current movie-clip to call the object out of library panel
var _boarder:mcBoarder; for (var i:Number = 0; i < 6; i++) { _boarder = new mcBoarder(); addChild(_boarder); }
Placing Instances Created by a Loop var _boarder:mcBoarder; var _boarderX:Number = 132; var _boarderY:Number = 267; var _boarderR:Number = 0; for (var i:Number = 0; i < 6; i++) { _boarder = new mcBoarder(); addChild(_boarder); _boarder.x = _boarderX; _boarder.y = _boarderY; _boarder.rotation = _boarderR; _boarderX += 75; _boarderY -= 75; _boarderR -= 45; }