PDA

View Full Version : event.currentTarget Help


Ingenieurs2
05-16-2008, 03:03 PM
Hello. I hope somebody can help.

I have 70 movieclips, all different, that all do the same thing on a mouse over. I was hoping I could use the currentTarget trick in a function to make them all use the same functions, but sadly it seems they don't want to.

Imagine you have 10 different movieclips, all at different points on the stage, all called different things. Can I make them all use the same function, all I want them to do is alpha to 10% on mouse over.

Or am I going to have to write 70 functions and 140 event listeners? :eek:

Sorry for my manic post, I'm stressing. :)

A.

Ingenieurs2
05-16-2008, 03:22 PM
Ignore me, unless anyone really feels like posting a way to do it to help others. I got the event.currentTarget to work, so now all I need is 70 eventListeners and 2 functions. :)

I can cope with that. :)

Slowburn
05-16-2008, 03:40 PM
Use for..loops to iterate over your children to add listeners easily.

// instance Names for all your clips ( reference )
var tenMovieClips:Array = ["one","two","three","four","etc..."];
// number of clips
var numOfMovieClips:int = tenMovieClips.length;
// iterate through all clips in the array and add listeners to all of them
// this can alos be done to remove them
for( var i:int = 0; i < numOfMovieClips; i++ )
{
var target:MovieClip = getChildByName( tenMovieClips[i] ) as MovieClip;
target.addEventListener( MouseEvent.ROLL_OVER, doRollOver, false, 0, true );
}

function doRollOver( event:MouseEvent ):void
{
// get the clip we have rolled over
var target:MovieClip = event.target;
// actions for your rollover ( whatever they are )
target.alpha = .5;
}

cokefour
05-18-2008, 04:58 AM
I tried your array method but I keep getting this error

"ReferenceError: Error #1069: Property niknaksPanel_mc not found on String and there is no default value.
at edbSubNav_fla::MainTimeline/edbSubNav_fla::frame60()"

for the last movie clip in the array. If I delete the reference it just errors the last one remaining
here's the code

var myButtonArray:Array = new Array["hshPanel_mc","castlesPanel_mc","therapyPanel_mc",
"relaxPanel_mc","welfarePanel_mc",
"exactPanel_mc","midasPanel_mc","envelopePanel_mc",
"harmonyPanel_mc","niknaksPanel_mc"];

var numOfMovieClips:int = myButtonArray.length;
for (var i:int = 0; i < numOfMovieClips; i++)
{
var target:MovieClip = getChildByName(myButtonArray[i]) as MovieClip;
target.addEventListener(MouseEvent.ROLL_OVER, onOver);
target.addEventListener(MouseEvent.ROLL_OUT, onOut);
target.addEventListener(MouseEvent.CLICK, onClick);

function onOver(event:MouseEvent):void {
var twnYouScX:Tween;
var twnYouScY:Tween;
twnYouScX = new Tween(event.target, "scaleX",Elastic.easeOut, event.target.scaleX, (event.target.scaleX * 1.2), 2, true);
twnYouScY = new Tween(event.target, "scaleY",Elastic.easeOut, event.target.scaleY, (event.target.scaleY * 1.2), 2, true);
event.target.filters = [ new DropShadowFilter() ];
}
function onOut(event:MouseEvent):void {
var twnYouScX:Tween;
var twnYouScY:Tween;
twnYouScX = new Tween(event.target, "scaleX",Elastic.easeOut, event.target.scaleX, (event.target.scaleX /1.2), 2, true);
twnYouScY = new Tween(event.target, "scaleY",Elastic.easeOut, event.target.scaleY, (event.target.scaleY /1.2), 2, true);
event.target.filters = [];
}
var subLoader:Loader = new Loader;
var subURLRequest:URLRequest;
addChild(subLoader);
subLoader.x = -150;
subLoader.y = 75;
function onClick(event:MouseEvent):void {

switch (event.target) {
case hshPanel_mc :
subLoader.unload();
subURLRequest= new URLRequest("homeSweetHome.swf");
break;

case castlesPanel_mc :
subLoader.unload();
subURLRequest= new URLRequest("castles.swf");
break;

case therapyPanel_mc :
subLoader.unload();
subURLRequest= new URLRequest("therapy.swf");
break;
}


subLoader.load(subURLRequest);
event.target.filters = [];
}


}
stop();

Mazoonist
05-18-2008, 05:23 AM
var circles:Array = [c1, c2, c3, c4, c5]; //instance names of the clips on the stage

for(var i:int = 0; i < circles.length; i++) {
circles[i].addEventListener(MouseEvent.ROLL_OVER, over);
circles[i].addEventListener(MouseEvent.ROLL_OUT, out);
}

function over(event:MouseEvent):void {
event.currentTarget.alpha = 0.25;
}
function out(event:MouseEvent):void {
event.currentTarget.alpha = 1;
}

cokefour
05-18-2008, 11:19 AM
tried it still gets error without the quotes, the movieclips are instanced on stage with those names

Mazoonist
05-18-2008, 03:07 PM
If you only give your code, then I have to create from scratch every symbol mentioned in your script just to test and run it. I'd be glad to help if you'd just attach your fla file.

cokefour
05-18-2008, 05:56 PM
Sorry, I've just started using the forum didn't realise I could upload files

cokefour
05-18-2008, 07:23 PM
var myArray:Array = New Array[ ], got rid of the Array after the equals sign.

ONWARDS!!

Thanks for looking at it though

Mazoonist
05-18-2008, 07:38 PM
That's great! Because I looked at it, and I didn't even catch that.