Skip to main content
Inspiring
October 2, 2008
Answered

syntax question

  • October 2, 2008
  • 3 replies
  • 452 views
hello --

i'm wondering if their is a way to compress this code for more efficiency. stepping up to AS3 soon!

This topic has been closed for replies.
Correct answer Rothrock
I probably wouldn't do it as a function, but rather...well there are probably a lot of things I would do differently there. But here is most likely what I would do.

for(var i=1;i<=6;i++){
var curItem:MovieClip=this["rack"+i]
curItem.onRollOver=rackOver;
curItem.onRollOut=rackOut;
curItem.attachMovie("spreadOver","spreadOver",10,{_x:(i-1)*34,_y:-2,_height:2,_width:30,_visible:false});
curItem.ID=i;
}

function rackOver(){
this.spreadOver._visible=true;
}
function rackOut(){
this.spreadOver._visible=false;
}

Also if you do need to have the multiple if type of stuff, then I would use a switch statement:

switch (item){
case "rack1":
// do something;
break;
case "rack2:
// do something else;
break;
// and so on.
}

3 replies

Inspiring
October 2, 2008
You know I don't know. If there are children clips inside of an attached movie their properties, events, etc won't be available until one frame has advanced.

Also in your example there would be a lot of code that needed to execute each time it was attached to place it. So I thought that was a bit "messy" and complex. I imagine there is a little memory overhead for having all six attached all the time. If these are little buttons or some such it probably isn't important. If these are giant 800 x 600 bitmaps that might be an issue. So ir really depends upon what you are doing!

Generally I don't like to attach and remove a lot of clips. But it all depends upon what is in your clips and what you are trying to do.
Inspiring
October 2, 2008
cool. thanks so much! as always, more than one way to skin a cat.
RothrockCorrect answer
Inspiring
October 2, 2008
I probably wouldn't do it as a function, but rather...well there are probably a lot of things I would do differently there. But here is most likely what I would do.

for(var i=1;i<=6;i++){
var curItem:MovieClip=this["rack"+i]
curItem.onRollOver=rackOver;
curItem.onRollOut=rackOut;
curItem.attachMovie("spreadOver","spreadOver",10,{_x:(i-1)*34,_y:-2,_height:2,_width:30,_visible:false});
curItem.ID=i;
}

function rackOver(){
this.spreadOver._visible=true;
}
function rackOut(){
this.spreadOver._visible=false;
}

Also if you do need to have the multiple if type of stuff, then I would use a switch statement:

switch (item){
case "rack1":
// do something;
break;
case "rack2:
// do something else;
break;
// and so on.
}
Inspiring
October 2, 2008
are there any performance issues when you attach and remove an mc versus attaching an mc, hiding it then showing it after the event?

thanks > mark