View Full Version : Grouping objects or movieclips into one variable
you_rock
05-11-2005, 02:04 PM
Can I assign more than one MC to a variable, such as:
//my one variable:
mytargets=mc1 and mc2 and mc3;//three movieclips into one variable
//make calls to one variable to group functions and actions
mytargets.onRollOver= function(){
mycursor.gotoAndPlay("over");
};
mytargets.onRollOut= function(){
mycursor.gotoAndPlay("up");
};
senocular
05-11-2005, 02:16 PM
no. Probably about the best thing you can do is put them into an array and loop through that array performing the same actions on each.
Either that or put them all in the same movie clip - that would work for things like movement and transparency etc, but not rollover events and things of that nature
no - you can do a series of mcs all equal to the same function like this:
mc1.onRollOver = mc2.onRollOver = mc3.onRollOver = function(){
mycursor.gotoAndPlay("over");
};
That will work.
you_rock
05-11-2005, 02:22 PM
Bummer :(
What about creating some sort of prototype object function that somehow included the different MCs?
you_rock
05-11-2005, 02:24 PM
Ah beau you posted while I was typing.
Would I be able to do it like this?
firstgroup=mc1=mc2;
second group=mc1=mc2=mc3;
firstgroup.onRelease=function(){
etc...
senocular
05-11-2005, 02:47 PM
its still not grouping. You cant assign to all one variable. Each will have to be used. beau's example is just reducing redundancy by consecutive assignment through variable return value.
Like I said, the closest thing to one variable would be assigning them to an array, but youd still have to do some looping for each variable in that array. You can use a walk method to ease the pain, though.
// array_walk calls a function 'func' for each item in array '_array'
function array_walk(_array, func){
var i = _array.length;
var args = arguments.slice(2);
while (i--){
func.apply(_array[i], args);
}
}
// ******************
// define your targets
mytargets = [mc1 , mc2, mc3];
// define a function to be used on each target
// 'this' in the function will represent the
// target in the array as the function is called
// for it in array_walk
function assignEvents(){
this.onRollOver= function(){
mycursor.gotoAndPlay("over");
};
this.onRollOut= function(){
mycursor.gotoAndPlay("up");
};
}
// use array_walk to assign the events to each target
array_walk(mytargets, assignEvents);
you_rock
05-13-2005, 05:17 PM
Very interesting. I will have to explore that. Thanks!
sleekdigital
05-13-2005, 05:41 PM
Well this is the AS 2.0 forum , so we should be suggesting to create a movieclip subclass or a class that wraps a movieclip... shouldn't we?
http://www.actionscript.org/forums/showthread.php3?t=73199
jsebrech
05-13-2005, 06:42 PM
Or, create a class:
class ClipGroup {
private var items: Array;
public function ClipGroup(newitems: Array) {
items = newitems;
}
public function set onRollOver (func) {
for (var i in items) {
items[i].onRollOver = func;
};
}
}
And use it like this:
var group = new ClipGroup([mc1, mc2, mc3]);
group.onRollOver = function(){
mycursor.gotoAndPlay("over");
};
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.