AS: MAIN - all your actionscript questions answered
While Loops
Introduction
I hope noone has already done one of these (I dont think so) but I think it is necessary for me to go through this as it is an invaluable technique used in actionscript and is very powerful in the right hands!
The while loop
The while loop basically runs a peice of code (the statements) over and over until the condition becomes false
We write it like this
while( condition ) {
statement(s) ;
}
Here is an example of how to use a while loop
onClipEvent (enterFrame) {
// Makes a new object called point
var point:Object = new Object();
point.x = this.feet._x;
point.y = this.feet._y;
this.localToGlobal(point);
// Converts the local co-ordinates of the "feet" mc into global co-ordinates ready to use
//for the shapeflag hitTest
while (_root.ground.hitTest(point.x, point.y, true)) {
// While the mc "ground" hits the point at which the mc "feet" lie at the mc containing
// feet will go up (as well as the point otherwise the loop would never end
this._y -= 1;
grav_y = 0;
jumping = false;
point.y -= 1;
}
}
This is a nice simple example which is used in this
IMPORTANT
You MUST make the statement/s eventualy cancel out the condition otherwise it will keep running and eventualy crash it.
For example DO NOT do...
While (a = true) {
this._x += 10
}
but you could do
While (this._x < 100) {
this._x += 10
}
see what I mean?
I hope this helped the less experienced actionscripters out there and though it is really basic it is very useful when creating flash games. Any questions/ comments please post
AS: MAIN - all your actionscript questions answered