00:00
00:00
Newgrounds Background Image Theme

Notking 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: For... in loops

7,272 Views | 32 Replies
New Topic Respond to this Topic

AS: For... in loops 2005-06-30 12:09:03


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

Response to AS: For... in loops 2005-06-30 12:13:33


Nice post again,"InglorAntiPostLimit". ; )


BBS Signature

Response to AS: For... in loops 2005-06-30 12:15:42


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

Response to AS: For... in loops 2005-06-30 13:27:09


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.

Response to AS: For... in loops 2005-06-30 13:29:22


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

Response to AS: For... in loops 2005-06-30 14:44:57


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.

Response to AS: For... in loops 2005-07-01 03:47:29


AS: Basic Loops

linked to the basic loop thread for reference

Response to AS: For... in loops 2005-07-23 12:49:40


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

Response to AS: For... in loops 2005-07-23 13:08:06


SHOOT ME DELTA

godamn it

it's an operator not a method

myClip instanceof MovieClip

what I really ment is:

_root[i] instanceof MovieClip

Response to AS: For... in loops 2005-07-23 13:15:58


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

Response to AS: For... in loops 2005-07-23 13:23:09


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

Response to AS: For... in loops 2005-07-31 09:34:44


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

Response to AS: For... in loops 2005-09-15 18:54:59


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

Response to AS: For... in loops 2005-12-19 10:15:49


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

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

Response to AS: For... in loops 2005-12-19 10:22:18


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

Response to AS: For... in loops 2005-12-19 10:23:02


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

Response to AS: For... in loops 2006-03-05 15:20:56


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

Response to AS: For... in loops 2006-03-05 15:24:49


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

Not with that description, no.


Sup, bitches :)

BBS Signature

Response to AS: For... in loops 2006-03-05 15:38:31


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

Response to AS: For... in loops 2007-02-08 12:58:34


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


BBS Signature

Response to AS: For... in loops 2007-02-08 13:08:22


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

Response to AS: For... in loops 2007-02-08 13:35:20


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

Response to AS: For... in loops 2007-02-08 13:37:19


How does that go together? >:P


BBS Signature

Response to AS: For... in loops 2007-02-08 13:38:24


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

Response to AS: For... in loops 2007-02-08 13:40:18


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


BBS Signature

Response to AS: For... in loops 2007-02-08 13:41:56


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

Response to AS: For... in loops 2007-02-08 14:27:10


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

Response to AS: For... in loops 2007-02-08 14:30:59


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

Response to AS: For... in loops 2007-02-08 15:01:56


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

Response to AS: For... in loops 2007-02-08 15:10:54


2005 much?


wtfbbqhax