Forum Topic: Apply Actionscript to all Movieclip

(159 views • 14 replies)

This topic is 1 page long.

<< < > >>
Shouting

evaneley

Reply To Post Reply & Quote

Posted at: 6/21/09 02:13 PM

evaneley DARK LEVEL 02

Sign-Up: 05/06/07

Posts: 4

I was wondering how you could apply some actionscript to every movieclip on the stage, other than manually copying and pasting, preferably with some actionscript.


None

Archon68

Reply To Post Reply & Quote

Posted at: 6/21/09 02:26 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,201

Just expand from this:

var myMovieClips:Array = new Array("duck", "cow", "dog", "cat");

function myFunction(){
	trace("Function Executed");
	//Your Code
}

for(var i=0; i<myMovieClips.length; i++){
	_root[myMovieClips[i]].onRelease = function(){
		myFunction();
	}
}

I code in AS2, in case I forgot to mention it.

BBS Signature

None

evaneley

Reply To Post Reply & Quote

Posted at: 6/21/09 02:36 PM

evaneley DARK LEVEL 02

Sign-Up: 05/06/07

Posts: 4

Um, I don't really know what to do with that, I suck with arrays and other actionscript stuff. All I want to do is apply one line of code to every movieclip I have on stage, which is a lot, and it would take to long to apply one line of code to them all manually. (I just want to apply this.swapDepths(_y)) So could I do something like,

var myMovieClips:Array = new Array("All Movieclips on Stage:);

function myFunction(){
this.swapDepths(_y);
}

for(var i=0; i<myMovieClips.length; i++){
_root[myMovieClips[i]].onRelease = function(){
myFunction();
}
}

Sorry, I really don't get it.


None

Archon68

Reply To Post Reply & Quote

Posted at: 6/21/09 02:46 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,201

Why would you want to make it swap depths with its own _y position?

To add more items to my array, just add a comma, followed by a string with the name of your movieClip.

I code in AS2, in case I forgot to mention it.

BBS Signature

None

Archon68

Reply To Post Reply & Quote

Posted at: 6/21/09 02:48 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,201

At 6/21/09 02:46 PM, Archon68 wrote: Why would you want to make it swap depths with its own _y position?

To add more items to my array, just add a comma, followed by a string with the name of your movieClip.

Code, BTW:

var myMovieClips:Array = new Array("duck", "cow", "dog", "cat");

for(var i=0; i<myMovieClips.length; i++){
	_root[myMovieClips[i]].onRelease = function(){
		this.swapDepths(_y);
	}
}

I code in AS2, in case I forgot to mention it.

BBS Signature

None

evaneley

Reply To Post Reply & Quote

Posted at: 6/21/09 02:49 PM

evaneley DARK LEVEL 02

Sign-Up: 05/06/07

Posts: 4

It's so that objects can appear behind and infront of other objects, it gives it the depth of its current _y position. But it doesn't work when I use that code.


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 6/21/09 02:59 PM

Yambanshee DARK LEVEL 10

Sign-Up: 10/05/08

Posts: 1,449

easy:

for(i in _root){
i.onEnterFame = function () {
//CODE
}
}

thats ALL you need

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

UnknownFury

Reply To Post Reply & Quote

Posted at: 6/21/09 03:04 PM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,031

At 6/21/09 02:59 PM, Yambanshee wrote: for(i in _root){
i.onEnterFame = function () {
//CODE
}
}
for (var i in _root) 
{
	if (typeof(_root[i]) == "movieclip") 
		_root[i].swapDepths(_root[i]._y);
}

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


None

Archon68

Reply To Post Reply & Quote

Posted at: 6/21/09 03:09 PM

Archon68 LIGHT LEVEL 22

Sign-Up: 09/09/07

Posts: 3,201

At 6/21/09 02:49 PM, evaneley wrote: It's so that objects can appear behind and infront of other objects, it gives it the depth of its current _y position. But it doesn't work when I use that code.

Did you change "duck" "cow" "dog" and "cat" to your own Movie Clips?

Here's a .fla file so you can see it working.

And to Yambanshee, wouldn't that apply the actions to ALL the movieclips in the _root layer? I'm guessing he doesn't want it applied to everything. If he has a movieclip for the background, or for a mute button, etc. then it'll apply the function to those as well.

I code in AS2, in case I forgot to mention it.

BBS Signature

Winking

milchreis

Reply To Post Reply & Quote

Posted at: 6/21/09 03:27 PM

milchreis DARK LEVEL 16

Sign-Up: 01/11/08

Posts: 405

check out "prototype".

"Even if it's just cynicism that remained to push us ahead, we'll carry on!"


None

evaneley

Reply To Post Reply & Quote

Posted at: 6/21/09 04:35 PM

evaneley DARK LEVEL 02

Sign-Up: 05/06/07

Posts: 4

Hey thanks for all the help, it works, but all i need now is a code so that the depth of one movieclip is always above everything.
Should I just set its depth to 999999999999 or something?


None

milchreis

Reply To Post Reply & Quote

Posted at: 6/21/09 04:41 PM

milchreis DARK LEVEL 16

Sign-Up: 01/11/08

Posts: 405

At 6/21/09 04:35 PM, evaneley wrote: Hey thanks for all the help, it works, but all i need now is a code so that the depth of one movieclip is always above everything.
Should I just set its depth to 999999999999 or something?

check out "getNextHighestDepth()"

"Even if it's just cynicism that remained to push us ahead, we'll carry on!"


None

asgrunt

Reply To Post Reply & Quote

Posted at: 6/22/09 04:02 AM

asgrunt NEUTRAL LEVEL 01

Sign-Up: 05/26/09

Posts: 164

prototype is nice but be aware that _root is a mc, too. The prototype may affect it which makes no sense. So to loop through everything in _root maybe a better solution. Also, as far as I understand the question, it is enough to change the depth of the objects on _root (and not mcs who are nested in other mcs - prototype would change them, too)


Kissing

milchreis

Reply To Post Reply & Quote

Posted at: 6/22/09 05:25 AM

milchreis DARK LEVEL 16

Sign-Up: 01/11/08

Posts: 405

At 6/22/09 04:02 AM, asgrunt wrote: prototype is nice but be aware that _root is a mc, too. The prototype may affect it which makes no sense. So to loop through everything in _root maybe a better solution. Also, as far as I understand the question, it is enough to change the depth of the objects on _root (and not mcs who are nested in other mcs - prototype would change them, too)

duuh, are you afraid to make this last step yourself or what?
use a flag var.
Be aware that this may help somebody who does not want to change the depth only and who searches for a way to place code on every mc. (I know that this is not probable, yet not impossible)

"Even if it's just cynicism that remained to push us ahead, we'll carry on!"


Resigned

Hoeloe

Reply To Post Reply & Quote

Posted at: 6/22/09 07:02 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 4,983

At 6/21/09 03:09 PM, Archon68 wrote:
At 6/21/09 02:49 PM, evaneley wrote: It's so that objects can appear behind and infront of other objects, it gives it the depth of its current _y position. But it doesn't work when I use that code.
Did you change "duck" "cow" "dog" and "cat" to your own Movie Clips?

Here's a .fla file so you can see it working.

And to Yambanshee, wouldn't that apply the actions to ALL the movieclips in the _root layer? I'm guessing he doesn't want it applied to everything. If he has a movieclip for the background, or for a mute button, etc. then it'll apply the function to those as well.

It's still easy to cut any out by just adding in:

&& i._name != "movieclipname"){

to the function.

Swapping depths with the _y position is also the best way to allow objects to move on a 4 way directional plane and interact with the depths of other objects, i.e. appear behind or in front of an object depending on where they are on the screen.

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

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 02:07 PM

<< Back

This topic is 1 page long.

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