The Ultrashock Ultra Bundle
  • Home
  • Community
  • Forum
  • Flash
  • ActionScript
  • Thread
  •  
  • Previous topic
  • Next topic
Sign up to post

Flash
 ActionScript

  • felisan Author 
    • 31478 
    • 0 
    • 9 
    AS3: adding parameter to eventListener
    Isocase

    Last reply Aug 05 2009, 08:43 PM

    by Isocase

    Posted: May 29 2008, 05:08 AM

    by felisan

     

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

  9 REPLIES
 
Isocase
1  
Isocase

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
  •  
Armen
2  
Armen

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
  •  
Codemonkey
3  
Codemonkey

Both are good. Here’s yet another way. It’s not better, just… different smilie

[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
  •  
felisan Author 
4  
felisan

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
  •  
Nutrox
5  
Nutrox

[QUOTE=Codemonkey;739538]Both are good. Here’s yet another way. It’s not better, just… different smilie

[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. wink

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.

smilie

  • 30 May 2008 12:16 PM
  •  
Codemonkey
6  
Codemonkey

My my big grin

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
  •  
Nutrox
7  
Nutrox

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.

  • 30 May 2008 01:40 PM
  •  
asannov
8  
asannov
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
  •  
thatsasif
9  
thatsasif
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.
 
Topic actions
  •  Share on Facebook
  •  Share on Twitter
Topic Categories
  •  Show All Topics
  •  Development
    •  Server Side
    •  Client Side
  •  Creative Software
    •  Web
    •  Video
    •  3D
    •  Illustrator
    •  Photoshop Battles
    •  Photoshop
  •  Design
    •  Typography
    •  Resources & Insight
    •  Checkpoint
  •  Career
    •  Copyright Matters
    •  Advice & issues
    •  Job Seekers
    •  Job Offers
  •  Flash
    •  UltraMath
    •  OOP
    •  Third Party Tools
    •  Open Source alternatives
    •  Data Communication
    •  Components
    •  Flex
    •  AIR
    •  Flash Lite
    •  Flash Professional
    •  Flash Newbie
    •  ActionScript
    •  XML
  •  Lounge
    •  Polls
    •  Random Chat
    •  Showcase And Critique
    •  BombShock Award Nominations
  •  Community Essentials
    •  BombShock Award Winners
    •  Tutorials
    •  Interviews
    •  News
    •  Bitmap tutorials
Popular Topics
  • Sort by: 
  • Activity
  • Views
  • Comments
  • Likes
Advertise with us
  • Your advertisement here!
  • loading
Ultrashock
  • Creative Assets
  • Community
  • Blog
  1. Home
  2. Forum
+/-
Creative Assets
  • Categories
  • Contributors
  • How to buy
Make Money
  • Commission Rates
  • Referral Program
  • Contributor Program
Community
  • Activity Feed
  • Forum
  • Profiles
About
  • Quick Tour
  • Our History
  • Banners & Logos
Support
  • Contact Ultrashock
  • Advertise with us
  • Legal Information
  •  Keep up to date
  • Flash 775  Flash
  • Audio 6,481  Audio
  • Vector 2,130  Vectors
  • Image 12,338  Images
  • Creative Assets 21,724  Assets
  • Profiles 282,659  Members
  • Topics 93,762  Topics
  • Blog 4  Blog
  • Facebook 1,680  Facebook
  • Twitter 1,165  Twitter
  • Join our FREE monthly newsletter!
  • Archive
  • Invalid email address. Please try again.
Subscribe
  • ©2012 Ultrashock LLC - All rights reserved
  • Terms of Use
  • Privacy Policy
  • Switch to dark theme
  • RSS Feeds
  • Top

©2012 Ultrashock LLC - All rights reserved

Printed on Sat, February 04, 2012 - 18:59:49