2011-09-14

FLASH CS3 - Action Script part 8

Do you hungry right now.. If there is A noodle.. I would like to eat 2 right now.. Okay..enought the chit-chat!! This tutorial will explain about use of For, While and related function.

In previous Action Script, we can't use For!! For show since AS 3 (that's what we will learn now). Okay this tutorial will explain about Looping !! Looping able to perform using For, While and Repeat function.

You can looping for anything, for example you want to count one,two .. until ten!!


You can type like this
trace(1);
trace(2);
....
trace(10);
but if there more.. how about 100!! is wasting of time to type like that. You can short the code like this
for (var i:int 1<= 10i++) {
  
// Display the value of i.
  
trace(i);
}


basicly this code result same above. Let's I describe. We read from the for
for (var i:int 1<= 10i++)

  • for variable I from value 1. Please remember this I must integer or not working.
  • until I same or equal than 10
  • with Variable I add by 1 after looping

if the 2nd option/parameter I type
i < 10
the result will be stop at 9.. because when I=10.. the for will not run and exit the for function. about the 3rd option, we can control the I value not only in the for function, you can change inside the for function . For example like this

for (var i:int 110i++) {
  
// Display the value of i.
 
i++;
  trace(i);

}


the result will be
2
4
6
8
If your program manipulates variable that use in function For.. you should use while. Another example to For
for (var i:int 2<= 10i+=2) {
  
// Display the value of i.
  
trace(i);
}  


not only you can describe 1 parameter (in my example only variable I). You can do more like this


for (var i:int = 0, j:int = 10; i < 10; i++, j--) {
  trace("i is " + i);
  trace("j is " + j);
}
the for able to have more than 1.. and remember to use comma (,) as separated.

For advance use.. You can make nested for like this


for (var i:int = 1; i <= 3; i++) {
  for (var j:int = 1; j <= 2; j++) {
    trace(i + " X " + j + " = " + (i * j));
  }
}


and that's depend on what programing style do you use. While still exist but will be explain another tutorial.
This tutorial need you to make your own for you like and see what happen if you change this, that etc..

Actualy in Flash, we know about enterFrame right!! But we can use Timer as something to listener (addEventListener), as you know enterFrame is different but in real life how to work betwen two basicly the same.
Timer Class is new in FLASH CS 3!! to declare, we must type like this
var timer:Timer = new Timer(delay, repeatCount);
or in full
var timer:Timer = new Timer(500, 7);
Explanation
  • 500 is delay for 500 milliseconds
  • 7 is how many time the function will be run then stop.
The Timer class is part of the flash.utils package, and there is also a TimerEvent class in the flash.events package, so those need to be imported like this
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;

for example, we can see from next tutorial.. hehehe

Tidak ada komentar: