Be a Supporter!

AS: While

  • 4,594 Views
  • 64 Replies
New Topic Respond to this Topic
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
AS: While 2005-07-22 12:47:40 Reply

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

jkhkhj
jkhkhj
  • Member since: Aug. 22, 2003
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to AS: While 2005-07-22 12:50:12 Reply

foodang

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: While 2005-07-22 12:53:33 Reply

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
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: While 2005-07-22 12:53:34 Reply

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

sorry?


- Matt, Rustyarcade.com

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: While 2005-07-22 12:55:07 Reply

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.

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: While 2005-07-22 12:55:17 Reply

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

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: While 2005-07-22 13:03:41 Reply

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
Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: While 2005-07-22 13:04:08 Reply

post this code on your first freame

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

gorman2001
gorman2001
  • Member since: Aug. 18, 2002
  • Offline.
Forum Stats
Member
Level 14
Blank Slate
Response to AS: While 2005-07-22 13:04:46 Reply

shit its an infinite loop :p


website :: hugostonge.com
my job :: we+are

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: While 2005-07-22 13:05:18 Reply

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.

Bkid001
Bkid001
  • Member since: May. 25, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: While 2005-07-22 13:31:31 Reply

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..

Khao
Khao
  • Member since: Sep. 20, 2003
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: While 2005-07-22 14:19:40 Reply

isnt while the same thing as if?

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: While 2005-07-22 14:31:40 Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: While 2005-07-22 14:55:23 Reply

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...

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: While 2005-07-22 15:14:53 Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: While 2005-07-22 15:28:28 Reply

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
liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: While 2005-07-22 15:34:18 Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: While 2005-07-22 15:36:32 Reply

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

while is importent

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: While 2005-07-22 15:36:46 Reply

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

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: While 2005-07-22 15:38:32 Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: While 2005-07-22 15:41:46 Reply

nonono

onEnterFrame triggers ONCE every 1/fps seconds

while triggers continusly untill the condition is reached

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: While 2005-07-22 15:43:35 Reply

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

Ok, I understand.


Sup, bitches :)

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: While 2005-07-22 15:51:00 Reply

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
ShotInTheHead
ShotInTheHead
  • Member since: Dec. 20, 2002
  • Offline.
Forum Stats
Member
Level 31
Blank Slate
Response to AS: While 2005-07-22 15:58:01 Reply

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.

ShotInTheHead
ShotInTheHead
  • Member since: Dec. 20, 2002
  • Offline.
Forum Stats
Member
Level 31
Blank Slate
Response to AS: While 2005-07-22 15:59:57 Reply

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 @.@

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: While 2005-07-22 16:10:08 Reply

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)

Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to AS: While 2005-07-22 16:14:33 Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: While 2005-07-22 16:17:39 Reply

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!

Khao
Khao
  • Member since: Sep. 20, 2003
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: While 2005-07-22 16:17:58 Reply

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

lolll I save a lot too, even sometimes I havent changed anything I save cause I think I changed something XD

but to stay in topic, basically, while is only a loop of "if" right?
would do the same to make
for(...){
if(...){
...
}
}
?

ShotInTheHead
ShotInTheHead
  • Member since: Dec. 20, 2002
  • Offline.
Forum Stats
Member
Level 31
Blank Slate
Response to AS: While 2005-07-22 16:21:56 Reply

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.