| Ultrashock Forums
• AS3: adding parameter to eventListener |
|||
![]() |
||||
| Search this Thread | Thread Tools | Display Modes |
|
|
|||||||||||||||||||||||||
![]() |
Ultrashock Member Comments:
|
2008-05-29
#2 |
||
|
|
2008-05-29
#3 |
||
|
You can add property to the boxes like: blueBox._color=0x0000FF; and after just change the color based on event target _color property like: Code:
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"});
}
|
|
|
2008-05-29
#4 |
||
|
Both are good. Here's yet another way. It's not better, just... different ![]() ActionScript Code:
|
|
|
2008-05-30
#5 |
||
|
Last edited by felisan : 2008-08-28 at 10:56.
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 |
|
17 Creative Assets
|
2008-05-30
#6 |
||
|
Both are good. Here's yet another way. It's not better, just... different
![]() ActionScript Code:
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.
|
|
|
2008-05-30
#7 |
||
|
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. and (b) the objects you are listening to will never be garbage collected even if you declare the listener as a weak reference.
|
|
17 Creative Assets
|
2008-05-30
#8 |
||
|
I'm pretty sure weak references don't work with closures. I remember running a few tests and posting the results in a thread around here somewhere, but I can't find it at the moment. I will have another look for it this evening when I have a bit more free time.
|
|
|
2009-08-02
#9 |
||
|
Last edited by asannov : 2009-08-17 at 06:49.
Code:
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"});
}
|
|
Creative Assets
|
2009-08-03
#10 |
||
|
Code:
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"});
}
|
|
|
2009-08-04
#11 |
||
|
@thatsasif isn't your example almost the same as the example Codemonkey posted at #4 and Nutrox argumented against at #6 ? thanks |
|
|
2009-08-04
#12 |
||
|
@thatsasif That code you posted just looks filthy!!!! ![]() I agree with Nutrox in not using inline/anonymous functions but if you feel you want to go that route then go ahead. |
|
|
2009-08-05
#13 |
||
|
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. ![]() ActionScript Code:
|
|
17 Creative Assets
|
2009-08-05
#14 |
||
|
That would be a better way to do things I believe, I can't see any obvious problems with it. The functions are still anonymous but you aren't losing reference to them, they look more like regular callbacks now.
|
|
|
2009-08-05
#15 |
||
|
What about the below? Code:
blueBox.addEventListener(MouseEvent.CLICK, boxClickHandler);
redBox.addEventListener(MouseEvent.CLICK, boxClickHandler);
yellowBox.addEventListener(MouseEvent.CLICK, boxClickHandler);
function boxClickHandler(event:MouseEvent):void {
switch (event.target) {
case blueBox:
changeColor(0x0000FF);
break;
case redBox:
changeColor(0xFF0000);
break;
case yellowBox:
changeColor(0xFFFF00);
break;
}
}
function changeColor(color:Number):void {
Tweener.addTween(this.bigBox, {_color:color, time:1, transition:"linear"});
}
|
|
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|


14 comments
| 18438 views




17 Creative Assets


Linear Mode
Side note you can use a switch statement instead of else if's.