Actionscript 3 Question - multiple event listeners...

Soldato
Joined
19 Oct 2002
Posts
3,480
Hi Guys,

Here is an example of the way i have coded something so when you hover over a smal blob graphic, it reduces in opacity by 10%:

Code:
function blobFade(evt:Event):void
{
      mBlob.alpha = mBlob.alpha - .1;
}

mBlob.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
but how would i do this, if the (for example) entire sceen was full of these blobs? is there a way to attach the

Code:
.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
to all blob movie clips on the screen?

they will all be instances of the same class (the same blob library item) if that makes it easier?

is this even possible or would i need:

Code:
mBlob01.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
mBlob02.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
mBlob03.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
mBlob04.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
etc...
(there will potentially be 100s of these blobs...)
 
i can't really get the code in AS3.0 for you as i'm still in the bronze-age of AS2.0.. but the function, void does that not cause an error with a small v? anyhow, the function, it needs the object passed through it so it can deal with all blobs.

i.e. something like..
Code:
function blobFade(evtObj:Object):Void
{
      evtObj._alpha = evtObj._alpha - .1;
}
mBlob01.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
mBlob02.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
mBlob03.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
mBlob04.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
if mBlob04 triggered the event then flash see's
evtObj._alpha = evtObj._alpha - .1;
as
mBlob04._alpha = mBlob04._alpha - .1;

next job it's making those addEventListeners in a for loop.. i need to have a clean up first as the dog just had a seizure, knocked my tea over and poor thing don't know what's going on for sometime after :(
 
ahh do you mean adding them to an array and just moving through it with a for loop?

you would still have to add all the names to an array then...

you would need:

Code:
var blobArray = new Array(mBlob01, mBlob02, mBlob03, mBlob04, ... );
fair anough, but my question really comes back to what if there are many, say hundreds of these blobs? and you want them all to be capable of the same deduction in alpha on mouse_out? (or roll_out, whatever the differnce is?)
 
like this..
Code:
var blobQty:Number = 100;
for (i=0; i<blobQty; i++) {
	var baseVar:MovieClip = this.createEmptyMovieClip("mBlob"+i, this.getNextHighestDepth());
	trace(baseVar);
	with(baseVar){
		//quick example of setting a position for the blobs
		_x= 10*i;
		}
	//attachMovie mBlob into baseVar, requires an mc in the library with the export name mBlob
	var blobVar:MovieClip = baseVar.attachMovie("mBlob", "mBlob", baseVar.getNextHighestDepth());
	//set the event listner
	baseVar.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
	//or you can use
	//blobVar.addEventListener(MouseEvent.MOUSE_OUT, blobFade);
}

function blobFade(evtObj:Object):Void
{
      evtObj._alpha = evtObj._alpha - .1;
}
i'm out of time again :o but i'll be back tonight.

edit:.. rushed my butt off and my mates stuck in traffic. i've really got to get back into flash because i'm starting to forget what i learnt :(

here's something i cut quick from a project that may help. make a new fla and stick this into flame one and compile... it's an early version of a menu i was making, a working example in AS2.0 of binding events to mc's created dynamicaly, in this case from an array. the commented onEnterFrame's are gumph (testing things), ignore them.
Code:
var tempArray:Array = new Array();
var j:Number = 0;
tempArray[j++] = ["About", "Sub About"];
tempArray[j++] = ["News", "Sub News"];
tempArray[j++] = ["Files", "Sub Files"];
tempArray[j++] = ["Links", "Sub Links"];
tempArray[j++] = ["Contact", "Sub Contact"];
tempArray[j++] = ["Contact", "Sub 22"];
tempArray[j++] = ["Contact", "Sub 32"];
tempArray[j++] = ["Contact", "Sub 42"];
tempArray[j++] = ["Contact", "Sub 12"];
tempArray[j++] = ["Contact", "Sub Contact"];
//trace(tempArray);
import flash.filters.*;
var gf:GlowFilter = new GlowFilter(0x000000, 100, 2, 2, 1000, 1, false, false);
var ds:DropShadowFilter = new DropShadowFilter(1, 45, 0x000000, 5, 2, 2, 0.8, 3, false, false);
var css:TextField.StyleSheet = new TextField.StyleSheet();
css.setStyle(".btn", {fontFamily:'Tahoma,Arial,Helvetica,sans-serif', color:'#FFFFFF', fontSize:'11px', textAlign:'center', fontWeight:'bold'});
this.createEmptyMovieClip("buttonBase", 1);
with (buttonBase) {
	_x = 64;
	_y = 64;
}
var btnX:Number = 96;
var btnY:Number = 20;
var i:Number;
var k:Number;
for (i=0; i<tempArray.length; i++) {
	var baseVar = buttonBase.createEmptyMovieClip("button"+i, buttonBase.getNextHighestDepth());
	addFunctionsTo(baseVar);
}
function addFunctionsTo(evtO:Object):Void {
		with (evtO) {
		beginFill(0x003366, 25);
		lineStyle(0.25, 0x003366, 0);
		moveTo(0, 0);
		lineTo(btnX, 0);
		lineTo(btnX, btnY);
		lineTo(0, btnY);
		lineTo(0, 0);
		endFill();
		_x = 0;
		_y = ((evtO._height+0)*i);
	}
	evtO.createTextField("tf", baseVar.getNextHighestDepth(), 0, 0, btnX, btnY);
	with (evtO) {
		tf.selectable = false;
		tf.html = true;
		tf.styleSheet = css;
		tf.htmlText = "<span class=\"btn\">"+tempArray[i][0]+"</span>";
		tf.filters = [gf, ds];
		var itemNum:Number = i;
	}
	evtO.onRelease = function():Void  {
		trace(tempArray[itemNum][1]);
	};
	evtO.onRollOver = function():Void  {
		//this.onEnterFrame = function():Void  {
		ds.distance = 3;
		ds.alpha = 25;
		evtO.tf._y = -1;
		evtO.tf.filters = [gf, ds];
		//delete this.onEnterFrame;
		//};
	};
	evtO.onRollOut = function():Void  {
		//this.onEnterFrame = function():Void  {
		ds.distance = 1;
		ds.alpha = 5;
		evtO.tf._y = 0;
		evtO.tf.filters = [gf, ds];
		//delete this.onEnterFrame;
		//};
	};
}
and now i really have to go..
 
Last edited:
Back
Top Bottom