00:00
00:00
Newgrounds Background Image Theme

Kiler91 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: While 2005-07-22 12:47:40


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


- Matt, Rustyarcade.com

Response to AS: While 2005-07-22 12:50:12


foodang

Response to AS: While 2005-07-22 12:53:33


My tip: ALWAYS save your .fla before you test a while loop. I've lost count of the number of times I've crashed Flash by mistakenly using an infinite loop.


- - Flash - Music - Images - -

BBS Signature

Response to AS: While 2005-07-22 12:53:34


At 7/22/05 12:50 PM, The_BananaCock wrote: foodang

sorry?


- Matt, Rustyarcade.com

Response to AS: While 2005-07-22 12:55:07


At 7/22/05 12:53 PM, Denvish wrote: My tip: ALWAYS save your .fla before you test a while loop. I've lost count of the number of times I've crashed Flash by mistakenly using an infinite loop.

If it freezes eventually it asks if you want to abort the script.

Response to AS: While 2005-07-22 12:55:17


At 7/22/05 12:53 PM, Denvish wrote: My tip: ALWAYS save your .fla before you test a while loop. I've lost count of the number of times I've crashed Flash by mistakenly using an infinite loop.

He he yeah...
Normally it just goes blank for a while (he the irony) and then says "you must abort the script" or something so im lucky I suppose. I always save before testing the move (paranoid
<_<
>_>)


- Matt, Rustyarcade.com

Response to AS: While 2005-07-22 13:03:41


At 7/22/05 12:55 PM, Glaiel_Gamer wrote:
At 7/22/05 12:53 PM, Denvish wrote: My tip: ALWAYS save your .fla before you test a while loop. I've lost count of the number of times I've crashed Flash by mistakenly using an infinite loop.
If it freezes eventually it asks if you want to abort the script.

For me, it does that for other CPU-killing code, but never for while loops. I've left Flash for 30+ minutes before in the hopes that it will recover and let me save, but no dice.


- - Flash - Music - Images - -

BBS Signature

Response to AS: While 2005-07-22 13:04:08


post this code on your first freame

while(true){
trace("I AM AN IDIOT")
}

Response to AS: While 2005-07-22 13:04:46


shit its an infinite loop :p


website :: hugostonge.com

my job :: we+are

Response to AS: While 2005-07-22 13:05:18


At 7/22/05 01:03 PM, Denvish wrote: For me, it does that for other CPU-killing code, but never for while loops. I've left Flash for 30+ minutes before in the hopes that it will recover and let me save, but no dice.

lolz that's messed up.

Response to AS: While 2005-07-22 13:31:31


At 7/22/05 12:47 PM, Ninja-Chicken wrote: This is a nice simple example which is used in this

do you think I could get the fla of that? I'm not tryin to steal nothin, I'm just better at taking things apart to see how they work, rather than just reading a bunch of stuff....not saying that I won't read it, I already ran through most of your first post and read that stuff, I just learn better when it's hands on...I can give you contact info once you reply..

Response to AS: While 2005-07-22 14:31:40


At 7/22/05 02:19 PM, -KhAo- wrote: isnt while the same thing as if?

No, if only checks if a condition is true or false ONCE.
while will continue to loop & check the condition until it is either true or false. That's why it's vital to make sure that your while loop contains statements which will make your condition either true or false.


- - Flash - Music - Images - -

BBS Signature

Response to AS: While 2005-07-22 14:55:23


At 7/22/05 02:31 PM, Denvish wrote: while will continue to loop & check the condition until it is either true or false.

the condition is always either true or false... while checks untill the condition is true...

Response to AS: While 2005-07-22 15:14:53


At 7/22/05 02:55 PM, Inglor wrote:
At 7/22/05 02:31 PM, Denvish wrote: while will continue to loop & check the condition until it is either true or false.
the condition is always either true or false... while checks untill the condition is true...

well, I was thinking of:

while (!condition){statements} -checks until condition is true
while (condition){statements} -checks until condition is false


- - Flash - Music - Images - -

BBS Signature

Response to AS: While 2005-07-22 15:28:28


At 7/22/05 03:14 PM, Denvish wrote: while (!condition){statements} -checks until condition is true
while (condition){statements} -checks until condition is false

it's called a while because it's a while loop... some languages have do untill loops, this is not one of them... :P

oh well, I get your point

sorry for being a smartass <3

Response to AS: While 2005-07-22 15:34:18


At 7/22/05 12:53 PM, Denvish wrote: My tip: ALWAYS save your .fla before you test a while loop. I've lost count of the number of times I've crashed Flash by mistakenly using an infinite loop.

Yeah, while always seems to crash Flash for me, which is why I tend to not use it.. you can do the same thing with if, really O_o


Sup, bitches :)

BBS Signature

Response to AS: While 2005-07-22 15:36:32


no you can't... unless you do function recursion instead... which I doubt anyone really uses :P

while is importent

Response to AS: While 2005-07-22 15:36:46


you'd need 250 if statements to do the same as a while statement.

Response to AS: While 2005-07-22 15:38:32


I don't get it, if I put an if statement onEnterFrame.. it doesn't do the same as a while statement?


Sup, bitches :)

BBS Signature

Response to AS: While 2005-07-22 15:41:46


nonono

onEnterFrame triggers ONCE every 1/fps seconds

while triggers continusly untill the condition is reached

Response to AS: While 2005-07-22 15:43:35


At 7/22/05 03:41 PM, Inglor wrote: while triggers continusly untill the condition is reached

Ok, I understand.


Sup, bitches :)

BBS Signature

Response to AS: While 2005-07-22 15:51:00


At 7/22/05 03:36 PM, Glaiel_Gamer wrote: you'd need 250 if statements to do the same as a while statement.

Or a for and an if =)

At 7/22/05 03:36 PM, Inglor wrote: no you can't... unless you do function recursion instead... which I doubt anyone really uses :P
while is importent

It's nothing that can't be done with for loops. I very rarely use while loops, for exactly the same reason as -liam-, I hate losing 30+ minutes of work because Flash is too stupid to recognise and halt an infinite loop (or I'm too stupid to realise I wrote one ;)


- - Flash - Music - Images - -

BBS Signature

Response to AS: While 2005-07-22 15:58:01


At 7/22/05 01:04 PM, Glaiel_Gamer wrote: post this code on your first freame

while(true){
trace("I AM AN IDIOT")
}

HAHAHAHAHAHAHAHAHAHAHA!!!
same can be done with
if ( ; ; ) {
trace("I AM A DUMBFUCK")
}
or even
while(_root._currentframe!=undefined) {
trace("LOLOLOLOLOLOL")
}

theres almost endless possibilities and you jsut pray flash lets you stop it. sometimes ti does. othertimes you owned your comp to the point of no return.

Moral of the story,
SAVE A LOT.

Response to AS: While 2005-07-22 15:59:57


At 7/22/05 03:14 PM, Denvish wrote:
while (!condition){statements} -checks until condition is true

when you think about it, its a double negative there. when its !true that means its false. which means its checking if its false. so a false then returns a true @.@

Response to AS: While 2005-07-22 16:10:08


At 7/22/05 03:51 PM, Denvish wrote: It's nothing that can't be done with for loops. I very rarely use while loops, for exactly the same reason as -liam-, I hate losing 30+ minutes of work because Flash is too stupid to recognise and halt an infinite loop (or I'm too stupid to realise I wrote one ;)

I use them all the time, they can be really useful. Check out my spiral generator (bbs search for it) VERY useful. And flash recognized an infinite loop on my computer. (Maby cause it's a mac and macs are smarter than windows)

Response to AS: While 2005-07-22 16:14:33


I save constantly. And I mean CONSTANTLY.

new symbol? save
modified one line of code? save
moved something? save

ctrl +s is almost broken on my keyboard


BBS Signature

Response to AS: While 2005-07-22 16:17:39


At 7/22/05 04:14 PM, Afro_Ninja wrote: I save constantly. And I mean CONSTANTLY.

new symbol? save
modified one line of code? save
moved something? save

ctrl +s is almost broken on my keyboard

Same here because whenever I dont God decides he wants vengence on my sins and crashes it!

Response to AS: While 2005-07-22 16:21:56


At 7/22/05 04:14 PM, Afro_Ninja wrote:
ctrl +s is almost broken on my keyboard

i thought i was the only one.

Response to AS: While 2005-07-22 16:22:55


At 7/22/05 04:17 PM, -KhAo- wrote: but to stay in topic, basically, while is only a loop of "if" right?
would do the same to make
for(...){
if(...){
...
}
}
?

Yes, but that's a for more than an if. But that's the way I do it, I rarely have a need for while loops. There are rare occasions where they're necessary, but generally I'll go out of my way to avoid them.


- - Flash - Music - Images - -

BBS Signature

Response to AS: While 2005-07-22 16:29:55


heh, I save constantly too, but while loops never crash me.