Be a Supporter!

AS: For... in loops

  • 5,212 Views
  • 32 Replies
New Topic Respond to this Topic
InglorAntiPostLimit
InglorAntiPostLimit
  • Member since: Nov. 23, 2002
  • Offline.
Forum Stats
Member
Level 03
Blank Slate
AS: For... in loops Jun. 30th, 2005 @ 12:09 PM Reply

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

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: For... in loops Jun. 30th, 2005 @ 12:13 PM Reply

Nice post again,"InglorAntiPostLimit". ; )


BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: For... in loops Jun. 30th, 2005 @ 12:15 PM Reply

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

Glaiel-Gamer
Glaiel-Gamer
  • Member since: Dec. 28, 2004
  • Offline.
Forum Stats
Member
Level 28
Game Developer
Response to AS: For... in loops Jun. 30th, 2005 @ 01:27 PM Reply

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.

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: For... in loops Jun. 30th, 2005 @ 01:29 PM Reply

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

BleeBlap
BleeBlap
  • Member since: Mar. 8, 2005
  • Offline.
Forum Stats
Member
Level 24
Blank Slate
Response to AS: For... in loops Jun. 30th, 2005 @ 02:44 PM Reply

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.

Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: For... in loops Jul. 1st, 2005 @ 03:47 AM Reply

AS: Basic Loops

linked to the basic loop thread for reference

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: For... in loops Jul. 23rd, 2005 @ 12:49 PM Reply

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));


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: For... in loops Jul. 23rd, 2005 @ 01:08 PM Reply

SHOOT ME DELTA

godamn it

it's an operator not a method

myClip instanceof MovieClip

what I really ment is:

_root[i] instanceof MovieClip

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: For... in loops Jul. 23rd, 2005 @ 01:15 PM Reply

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


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: For... in loops Jul. 23rd, 2005 @ 01:23 PM Reply

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

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: For... in loops Jul. 31st, 2005 @ 09:34 AM Reply

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

Rammer
Rammer
  • Member since: Jun. 8, 2003
  • Offline.
Forum Stats
Member
Level 33
Programmer
Response to AS: For... in loops Sep. 15th, 2005 @ 06:54 PM Reply

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

Fire
Fire
  • Member since: Oct. 29, 2003
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to AS: For... in loops Dec. 19th, 2005 @ 10:15 AM Reply

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

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

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: For... in loops Dec. 19th, 2005 @ 10:22 AM Reply

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");
}
}


Sup, bitches :)

BBS Signature
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: For... in loops Dec. 19th, 2005 @ 10:23 AM Reply

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

Rookie209
Rookie209
  • Member since: Feb. 20, 2006
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to AS: For... in loops Mar. 5th, 2006 @ 03:20 PM Reply

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

liam
liam
  • Member since: Dec. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to AS: For... in loops Mar. 5th, 2006 @ 03:24 PM Reply

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

Not with that description, no.


Sup, bitches :)

BBS Signature
Rookie209
Rookie209
  • Member since: Feb. 20, 2006
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to AS: For... in loops Mar. 5th, 2006 @ 03:38 PM Reply

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);
}

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to AS: For... in loops Feb. 8th, 2007 @ 12:58 PM Reply

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


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: For... in loops Feb. 8th, 2007 @ 01:08 PM Reply

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!


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: For... in loops Feb. 8th, 2007 @ 01:35 PM Reply

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
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: For... in loops Feb. 8th, 2007 @ 01:37 PM Reply

How does that go together? >:P


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: For... in loops Feb. 8th, 2007 @ 01:38 PM Reply

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
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: For... in loops Feb. 8th, 2007 @ 01:40 PM Reply

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


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: For... in loops Feb. 8th, 2007 @ 01:41 PM Reply

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
GustTheASGuy
GustTheASGuy
  • Member since: Nov. 2, 2005
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: For... in loops Feb. 8th, 2007 @ 02:27 PM Reply

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"

BBS Signature
Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: For... in loops Feb. 8th, 2007 @ 02:30 PM Reply

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


BBS Signature
Paranoia
Paranoia
  • Member since: Apr. 22, 2005
  • Offline.
Forum Stats
Member
Level 35
Game Developer
Response to AS: For... in loops Feb. 8th, 2007 @ 03:01 PM Reply

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
fwe
fwe
  • Member since: Jul. 24, 2003
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: For... in loops Feb. 8th, 2007 @ 03:10 PM Reply

2005 much?


wtfbbqhax