Forum Topic: AS: While

(3,106 views • 64 replies)

This topic is 3 pages long. [ 1 | 2 | 3 ]

<< < > >>
None

Rustygames

Reply To Post Reply & Quote

Posted at: 7/22/05 12:47 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

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


None

jkhkhj

Reply To Post Reply & Quote

Posted at: 7/22/05 12:50 PM

jkhkhj NEUTRAL LEVEL 01

Sign-Up: 08/22/03

Posts: 192

foodang


None

Denvish

Reply To Post Reply & Quote

Posted at: 7/22/05 12:53 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Rustygames

Reply To Post Reply & Quote

Posted at: 7/22/05 12:53 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

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

sorry?

- Matt, Rustyarcade.com


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 7/22/05 12:55 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

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.


None

Rustygames

Reply To Post Reply & Quote

Posted at: 7/22/05 12:55 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 7/22/05 01:03 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 7/22/05 01:04 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

post this code on your first freame

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


None

gorman2001

Reply To Post Reply & Quote

Posted at: 7/22/05 01:04 PM

gorman2001 NEUTRAL LEVEL 14

Sign-Up: 08/18/02

Posts: 1,935

shit its an infinite loop :p


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 7/22/05 01:05 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

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.


None

Bkid001

Reply To Post Reply & Quote

Posted at: 7/22/05 01:31 PM

Bkid001 NEUTRAL LEVEL 10

Sign-Up: 05/25/04

Posts: 274

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


None

Khao

Reply To Post Reply & Quote

Posted at: 7/22/05 02:19 PM

Khao EVIL LEVEL 19

Sign-Up: 09/20/03

Posts: 2,765

isnt while the same thing as if?


None

Denvish

Reply To Post Reply & Quote

Posted at: 7/22/05 02:31 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 02:55 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 7/22/05 03:14 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 03:28 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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

None

liaaaam

Reply To Post Reply & Quote

Posted at: 7/22/05 03:34 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,529

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


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 03:36 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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

while is importent


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 7/22/05 03:36 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

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


None

liaaaam

Reply To Post Reply & Quote

Posted at: 7/22/05 03:38 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,529

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


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/22/05 03:41 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

nonono

onEnterFrame triggers ONCE every 1/fps seconds

while triggers continusly untill the condition is reached


None

liaaaam

Reply To Post Reply & Quote

Posted at: 7/22/05 03:43 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,529

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

Ok, I understand.


None

Denvish

Reply To Post Reply & Quote

Posted at: 7/22/05 03:51 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

ShotInTheHead

Reply To Post Reply & Quote

Posted at: 7/22/05 03:58 PM

ShotInTheHead LIGHT LEVEL 31

Sign-Up: 12/20/02

Posts: 3,443

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.


None

ShotInTheHead

Reply To Post Reply & Quote

Posted at: 7/22/05 03:59 PM

ShotInTheHead LIGHT LEVEL 31

Sign-Up: 12/20/02

Posts: 3,443

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


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 7/22/05 04:10 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,041

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)


None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 7/22/05 04:14 PM

Afro-Ninja EVIL LEVEL 37

Sign-Up: 03/02/02

Posts: 13,464

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

None

T-H

Reply To Post Reply & Quote

Posted at: 7/22/05 04:17 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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!


None

Khao

Reply To Post Reply & Quote

Posted at: 7/22/05 04:17 PM

Khao EVIL LEVEL 19

Sign-Up: 09/20/03

Posts: 2,765

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(...){
...
}
}
?


None

ShotInTheHead

Reply To Post Reply & Quote

Posted at: 7/22/05 04:21 PM

ShotInTheHead LIGHT LEVEL 31

Sign-Up: 12/20/02

Posts: 3,443

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.


All times are Eastern Standard Time (GMT -5) | Current Time: 10:54 PM

<< Back

This topic is 3 pages long. [ 1 | 2 | 3 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!