Forum Topic: AS: For... in loops

(3,922 views • 32 replies)

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

<< < > >>
None

InglorAntiPostLimit

Reply To Post Reply & Quote

Posted at: 6/30/05 12:09 PM

InglorAntiPostLimit NEUTRAL LEVEL 03

Sign-Up: 11/23/02

Posts: 71

Sorry, couldn't resist, had to use AntiPostLimit account, it's ok since it's the original purpose of the anti post limit accounts (like schorhrs)

The one thread to rule them all: AS: Main

This is a useful little thing, again this isn't a long tutorial, it's very basic and it covers a VERY USEFUL OFTEN OVERLOOKED type of looping in flash, the for... in loop

Uses
There are many uses for for in loops:

1)looping over all movieclips in a container
2)looping over all strings on data.
3)looping over all the objects ;)

I will cover:

the instanceof method
for in loops

The actual AS

first of all I'll start with the instance of method, it basically checks if an object is an instance of a class, more advanced talk about classes and objects at AS: OOP.

for example
s.instanceof(String);
will only return true if 's' is a string
_root.mc1.instanceof(MovieClip);
will only return true if 'mc1' is a movieclip

simple eh?
more about instance of

The actual loop

the syntax of a for in loop is:

for(reference in container){
//do actions
}

now the reference part is the one hard to concieve, reference isn't an actual object, it is a reference to all objects. what flash really does is place "links" to all of the objects on the Stage in an array and reference is an array index. more about arrays

the container is the object where the check will be held, for example if _root is the container it will loop over every single thing in your movie, this is why instance of movieclip is importnet

let's look at examples

var counter:Number=0;
for(i in _root){
if(_root[i].instanceof(MovieClip)){
counter++;
}
}
trace(counter);

this counts the number of movieclips in the movie.

remmember container[reference] is always the currently pointed object, in our case _root[i].

very useful, ask any questions

the googling

easier loops


None

Toast

Reply To Post Reply & Quote

Posted at: 6/30/05 12:13 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

Nice post again,"InglorAntiPostLimit". ; )


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/30/05 12:15 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

very good example I forgot to post ;)

function setFPS(fps:Number):Void{
for(i in _root){
if(_root[i].instanceof(MovieClip)){
_root[i].stop();
}
}
_root.stop();
id=_root.setInterval(function():Void{
_root.nextFrame();
for(i in _root){
if(_root[i].instanceof(MovieClip)){
_root[i].nextFrame;();
}
}
},1000/fps);
return id;
}

this is a very simple dynamic FPS setter using a for in loop


None

Glaiel-Gamer

Reply To Post Reply & Quote

Posted at: 6/30/05 01:27 PM

Glaiel-Gamer NEUTRAL LEVEL 27

Sign-Up: 12/28/04

Posts: 8,038

Inglor, I'm on a crappy dial-up computer, and this is the only post I'll make here today, but I'm guessing this could be used to make a pause function in a game? Wait, nvm, I know how I could do this. Good code anyway, I've been wondering how to do this.


None

Inglor

Reply To Post Reply & Quote

Posted at: 6/30/05 01:29 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

yea, you could use it for a pause function, there are better ways though :P


None

BleeBlap

Reply To Post Reply & Quote

Posted at: 6/30/05 02:44 PM

BleeBlap LIGHT LEVEL 24

Sign-Up: 03/08/05

Posts: 944

Thanks for covering this type of loop. It was the only one that I wasn't very confident with when I made the AS thread about the others. Your explanation makes a lot more sense than the AS reference dictionary. I understand how to do this stuff now. And yes, the AS thread will rule them all.


None

Inglor

Reply To Post Reply & Quote

Posted at: 7/1/05 03:47 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

AS: Basic Loops

linked to the basic loop thread for reference


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 7/23/05 12:49 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

never knew about the instanceof, thx for that... one thing tho

:: for example
:: s.instanceof(String);
:: will only return true if 's' is a string
:: _root.mc1.instanceof(MovieClip);
:: will only return true if 'mc1' is a movieclip

here your using the syntaqx of object.instanceof(class)
yet, this doesnt work, and according to the link you gave and flashes documentation it should be

object instanceof class

which does work for example

var c:Vector = new Vector(); // im using a class Vector i made to test the instanceof with custom classes
trace(c instanceof Vector);
var s:String = new String();
trace(s instanceof String);

output
true
true

wheras with using object.instanceof(class)
you get the error

Expected a field name after '.' operator.
trace(s.instanceof(String));

My social worker says im special!

BBS Signature

None

Inglor

Reply To Post Reply & Quote

Posted at: 7/23/05 01:08 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

SHOOT ME DELTA

godamn it

it's an operator not a method

myClip instanceof MovieClip

what I really ment is:

_root[i] instanceof MovieClip


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 7/23/05 01:15 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,542

lol, dont you think youre over exagerating a bit with the SHOOT ME

My social worker says im special!

BBS Signature

None

Inglor

Reply To Post Reply & Quote

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

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

At 7/23/05 01:15 PM, dELta_Luca wrote: reading your threads makes me want to touch myself, donkey's arouse me.

Thanks I guess... :S

(yeah, I'm recycling the AS varialbes jokes)

sarcasm dude :P


None

Rustygames

Reply To Post Reply & Quote

Posted at: 7/31/05 09:34 AM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,662

At 7/23/05 01:23 PM, Inglor wrote:
At 7/23/05 01:15 PM, dELta_Luca wrote: reading your threads makes me want to touch myself, donkey's arouse me.
Thanks I guess... :S

(yeah, I'm recycling the AS varialbes jokes)

sarcasm dude :P

Heh heh I get it

- Matt, Rustyarcade.com


None

Rammer

Reply To Post Reply & Quote

Posted at: 9/15/05 06:54 PM

Rammer DARK LEVEL 32

Sign-Up: 06/08/03

Posts: 4,331

At 7/31/05 09:34 AM, Ninja-Chicken wrote:
At 7/23/05 01:23 PM, Inglor wrote:
At 7/23/05 01:15 PM, dELta_Luca wrote: reading your threads makes me want to touch myself, donkey's arouse me.
Thanks I guess... :S

(yeah, I'm recycling the AS varialbes jokes)

sarcasm dude :P
Heh heh I get it

O_o i dont...enlighten me, please

snyggys


None

Fire

Reply To Post Reply & Quote

Posted at: 12/19/05 10:15 AM

Fire DARK LEVEL 15

Sign-Up: 10/29/03

Posts: 2,936

So, you can make a list of all movieclips on stage with:

for(i in _root) {
trace(_root[i]);
}


None

liaaaam

Reply To Post Reply & Quote

Posted at: 12/19/05 10:22 AM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,529

At 12/19/05 10:15 AM, GameCubeFreak2004 wrote: So, you can make a list of all movieclips on stage with:

for(i in _root) {
trace(_root[i]);
}

No, that will list EVERYTHING.. not just MovieClips.

for(i in _root){
if(_root[i] instanceof MovieClip){
trace(_root[i]._name+" {");
trace("_x: "+_root[i]._x);
trace("_y: "+_root[i]._y);
trace("_xscale: "+_root[i]._xscale);
trace("_yscale: "+_root[i]._yscale);
trace("_width: "+_root[i]._width);
trace("_height: "+_root[i]._height);
trace("_alpha: "+_root[i]._alpha+"\n\n");
}
}


None

Inglor

Reply To Post Reply & Quote

Posted at: 12/19/05 10:23 AM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

joke's origin : http://www.newground../topic.php?id=297928

At 12/19/05 10:15 AM, GameCubeFreak2004 wrote: So, you can make a list of all movieclips on stage with:

yes, you can


None

Rookie209

Reply To Post Reply & Quote

Posted at: 3/5/06 03:20 PM

Rookie209 EVIL LEVEL 06

Sign-Up: 02/20/06

Posts: 32

I have a game where enemies run across the screen. I'm having problems putting them into a loop. Can someone help.


None

liaaaam

Reply To Post Reply & Quote

Posted at: 3/5/06 03:24 PM

liaaaam NEUTRAL LEVEL 22

Sign-Up: 12/11/04

Posts: 14,529

At 3/5/06 03:20 PM, Rookie209 wrote: Can someone help.

Not with that description, no.


None

Rookie209

Reply To Post Reply & Quote

Posted at: 3/5/06 03:38 PM

Rookie209 EVIL LEVEL 06

Sign-Up: 02/20/06

Posts: 32

At 3/5/06 03:24 PM, -liam- wrote:
At 3/5/06 03:20 PM, Rookie209 wrote: Can someone help.
Not with that description, no.

Ok, so far, I have a game where I have a movieclip scroll across the screen towards a tower. If its clicked it dies. I'm having problems putting it into a loop where, overtime a set number of copies of that movieclip will scroll across the screen.

Here is the as I have in the movieclip:

onClipEvent (enterFrame) {
if (_root.Tower._x > _x) {
_x += 5;
}
}
onClipEvent(load){
stop();
}
on(press){
gotoAndStop(2);
}


None

Hoeloe

Reply To Post Reply & Quote

Posted at: 2/8/07 12:58 PM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,006

If i understood what this is about correctly, could this be used to pause a game in flash?

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 2/8/07 01:08 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

It could be used to loop through all movieclips and their children. It's not a very clever idea of pausing a game, however, that was said when Glaiel was an even bigger noob than now!

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Paranoia

Reply To Post Reply & Quote

Posted at: 2/8/07 01:35 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,696

At 2/8/07 01:08 PM, OldGust wrote: It could be used to loop through all movieclips and their children. It's not a very clever idea of pausing a game, however, that was said when Glaiel was an even bigger noob than now!

It is clever, just not practical :P

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 2/8/07 01:37 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

How does that go together? >:P

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Paranoia

Reply To Post Reply & Quote

Posted at: 2/8/07 01:38 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,696

At 2/8/07 01:37 PM, OldGust wrote: How does that go together? >:P

Well frankly I'm impressed that Glaiel could even spell 'pause' at this point :P

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 2/8/07 01:40 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

But how can something that is not practical be clever? There is no other purpose for it, no?

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Paranoia

Reply To Post Reply & Quote

Posted at: 2/8/07 01:41 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,696

At 2/8/07 01:40 PM, OldGust wrote: But how can something that is not practical be clever? There is no other purpose for it, no?

If I worked out a way to convert 2000 apples into a banana, that would be clever. Unless there was a serious apple surplus, though, there wouldn't really be any practicality to it.

BBS Signature

None

GustTheASGuy

Reply To Post Reply & Quote

Posted at: 2/8/07 02:27 PM

GustTheASGuy LIGHT LEVEL 08

Sign-Up: 11/02/05

Posts: 11,358

At 2/8/07 01:41 PM, Paranoia wrote: If I worked out a way to convert 2000 apples into a banana, that would be clever.

Oh, well, yeah. But then it would not be a clever thing to research normally. :P

Unless there was a serious apple surplus, though, there wouldn't really be any practicality to it.

Haha, I doubt it would work out even in our society!

"Daddy, daddy, gemme a fucken banana or I'll fucken bbqpwn your sissy ass! >:C"

#ngprogramming at irc.freenode.net
haXe | Keel imperative | Spyro! | Thru you


None

Toast

Reply To Post Reply & Quote

Posted at: 2/8/07 02:30 PM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,914

At 7/23/05 01:23 PM, Inglor wrote:
At 7/23/05 01:15 PM, dELta_Luca wrote: reading your threads makes me want to touch myself, donkey's arouse me.

I hope you use protection.

PS: it's donkeys, not donkey's :P americanub


None

Paranoia

Reply To Post Reply & Quote

Posted at: 2/8/07 03:01 PM

Paranoia DARK LEVEL 34

Sign-Up: 04/22/05

Posts: 9,696

At 2/8/07 02:30 PM, Toast wrote: I hope you use protection.

PS: it's donkeys, not donkey's :P americanub

O: Everyone laugh at Toast some more :)

BBS Signature

None

fwe

Reply To Post Reply & Quote

Posted at: 2/8/07 03:10 PM

fwe DARK LEVEL 08

Sign-Up: 07/24/03

Posts: 3,361

2005 much?

wtfbbqhax


All times are Eastern Standard Time (GMT -5) | Current Time: 08:04 PM

<< Back

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

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