hi everybody.
i’d like to know how to pass a parameter to my function through an eventListener.
to illustrate, I’ve made this example:
http://campjohn.dk/AS3/addingParameterToEventListener/addingParameterToEventListener.swf
by clicking the boxes on the left, the bigBox centrered changes color.
nice!
right now, I have to use a if else-loop:
//imports
import caurina.transitions.Tweener;
this.blueBox.addEventListener(MouseEvent.CLICK, changeColor);
this.redBox.addEventListener(MouseEvent.CLICK, changeColor);
this.yellowBox.addEventListener(MouseEvent.CLICK, changeColor);
function changeColor(e:Event):void {
trace("CLICKED: e.target.name = "+e.target.name);
if(e.target.name == "blueBox"){
Tweener.addTween(this.bigBox, {_color:0x0000FF, time:1, transition:"linear"});
trace("changing color to blue");
} else if(e.target.name == "redBox") {
Tweener.addTween(this.bigBox, {_color:0xFF0000, time:1, transition:"linear"});
trace("changing color to blue");
} else if(e.target.name == "yellowBox") {
Tweener.addTween(this.bigBox, {_color:0xFFFF00, time:1, transition:"linear"});
trace("changing color to blue");
} else {
trace("this shouldn't be possible");
}
}
can I somehow change the example above so it passes the color to use to the function as a parameter instead?
thanks
felisan
I don’t think you can but you only using three colors. What is wrong with keeping it as is and why do you need/want to pass it in as a param?
Side note you can use a switch statement instead of else if’s.
[as]blueBox.addEventListener(MouseEvent.CLICK, changeColor);
redBox.addEventListener(MouseEvent.CLICK, changeColor);
yellowBox.addEventListener(MouseEvent.CLICK, changeColor);
function changeColor( e:Event ):void
{
switch( e.target.name )
{
case “blueBox”:
trace( “blueBox” );
Tweener.addTween(this.bigBox, {_color:0x0000FF, time:1, transition:“linear”});
break;
case “redBox”:
Tweener.addTween(this.bigBox, {_color:0xFF0000, time:1, transition:“linear”});
break;
case “yellowBox”:
Tweener.addTween(this.bigBox, {_color:0xFFFF00, time:1, transition:“linear”});
break;
}
}[/as]
- 29 May 2008 01:08 PM
-
You can add property to the boxes like: blueBox._color=0x0000FF;
and after just change the color based on event target _color property like:
blueBox._color= 0x0000FF;
redBox._color=0xFF0000;
yellowBox._color=0xFFFF00;
blueBox.addEventListener(MouseEvent.CLICK, changeColor);
redBox.addEventListener(MouseEvent.CLICK, changeColor);
yellowBox.addEventListener(MouseEvent.CLICK, changeColor);
function changeColor( e:Event ):void
{
Tweener.addTween(this.bigBox, {_color:e.target._color, time:1, transition:"linear"});
}
should work.
- 29 May 2008 01:18 PM
-
Both are good. Here’s yet another way. It’s not better, just… different ![]()
[as]
blueBox.addEventListener(MouseEvent.CLICK, function(e:Event) { changeColor(0x0000FF); });
redBox.addEventListener(MouseEvent.CLICK, function(e:Event) { changeColor(0xFF0000); });
yellowBox.addEventListener(MouseEvent.CLICK, function(e:Event) { changeColor(0xFFFF00); });
function changeColor(color:Number):void {
Tweener.addTween(this.bigBox, {_color:color, time:1, transition:“linear”});
}
[/as]
- 30 May 2008 03:46 AM
-
Author
thanks for helping, all 3 of you.
this will surely come in handy many times in the future.
by the way, I’ve blogged the issue here:
http://www.campjohn.dk/wp/?p=114
- 30 May 2008 06:19 AM
-
[QUOTE=Codemonkey;739538]Both are good. Here’s yet another way. It’s not better, just… different ![]()
[as]
blueBox.addEventListener(MouseEvent.CLICK, function(e:Event) { changeColor(0x0000FF); });
redBox.addEventListener(MouseEvent.CLICK, function(e:Event) { changeColor(0xFF0000); });
yellowBox.addEventListener(MouseEvent.CLICK, function(e:Event) { changeColor(0xFFFF00); });
function changeColor(color:Number):void {
Tweener.addTween(this.bigBox, {_color:color, time:1, transition:“linear”});
}
[/as]
Sorry mate, I have to pick you up on that one. ![]()
You should never use inline/anonymous functions for event listeners because (a) you can never remove those listeners again, and (b) the objects you are listening to will never be garbage collected even if you declare the listener as a weak reference.
Armen’s solution is the best one to use here, especially if the objects are extending a class with a “color” property defined.
- 30 May 2008 12:16 PM
-
My my ![]()
You’re right, but…
It depends on the context in which you define and use closures. In this specific case you might argue you would never want to remove those listeners and not even need the objects-being-listened-to garbage collected, though you wouldn’t decide on something like that lightly.
It takes some experience to know when you can or shouldn’t use closures, but closures are a topic on their own… not that I’m saying this is a particular wise approach, it isn’t really from an application architectural point of view, but like I said… it’s not better, just different. Personally, I would go for Armen’s solution.
[QUOTE=Nutrox;739592]and (b) the objects you are listening to will never be garbage collected even if you declare the listener as a weak reference.
How is that? If you declare the listener as weak, the listener is garbage collected the first sweep, as the listener has no strong references to it. How would that cause the broadcasting object to never be garbage collected?
- 30 May 2008 01:10 PM
-
var boxes:Array = [blueBox, redBox, yellowBox, blackBox, whiteBox, ... bla bla bla];
var colors:Array = [0x0000FF, 0xFF0000, 0xFFFF00, 0x000000, 0xFFFFFF, ... bla bla bla];
for (var i:Number = 0; i<boxes.length; i++){
boxes[i].addEventListener(MouseEvent.CLICK, changeColor);
}
function changeColor(e:MouseEvent):void{
Tweener.addTween(this.bigBox, {_color:colors[boxes.indexOf(e.currentTarget)], time:1, transition:"linear"});
}
just signed up to post this, but i think this is the best way if there are more than three boxes and colors
- 02 August 2009 10:51 AM
-
blueBox.addEventListener(MouseEvent.CLICK,function(e:MouseEvent){changeColor(e, 0x0000FF)},false, 0, true);
redBox.addEventListener(MouseEvent.CLICK,function(e:MouseEvent){changeColor(e, 0xFF0000)},false, 0, true);
yellowBox.addEventListener(MouseEvent.CLICK,function(e:MouseEvent){changeColor(e, 0xFFFF00)},false, 0, true);
function changeColor(e:MouseEvent, color:uint) {
Tweener.addTween(e.target, {_color:color, time:1, transition:"linear"});
}
- 04 August 2009 04:39 AM
-
- Log in or join for free to make a comment.


