Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: [AS3] Pass variable with addEventListener

  1. #1

    [AS3] Pass variable with addEventListener

    I want to pass a variable to a function called by an addEventListener, but when i try to do it, it doesnt pass the event itself to the function. For example, i have

    Code:
    item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, me);
    
    and...
    
    private function me(event:ContextMenuEvent):void {
    and i try something like that, but it says that it expected 2 variables

    Code:
    item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, me(fire));
    
    and...
    
    private function me(event:ContextMenuEvent, fire:Number):void {
    so, i try this one

    Code:
    item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, me(ContextMenuEvent, fire));
    
    and...
    
    private function me(event:ContextMenuEvent, fire:Number):void {
    but is says "Type Coercion failed: cannot convert flash.events::ContextMenuEvent$ to flash.events.ContextMenuEvent."

    I dont know what else to try. Can somebody please help me?

    Thanx

  2. #2
    It looks like the first attempt that you posted should work, can you post the specific error that is thrown?
    “Who were you, Krilnon, and how did you know so much about AS4?”
    The historian sighed as she gazed up at the sky and saw… not stars. A story.

  3. #3
    The first example works, but how will i pass the variable "fire" to the function "me"?

    I tried both the 2nd and the 3rd examples but it doesnt work

  4. #4
    addEventListener doesn't allow you to do so.

    When you put 'me' into the addEventListener call, me.type == function.

    me is a function.
    me() is not a function. it is a reference. Understand?

    So you can't do that, since it is expecting a Function as its parameter.

    So, to answer your question, you need to create a custom Event. And the perfect article is here

    http://www.darronschall.com/weblog/archives/000191.cfm

  5. #5
    Quote Originally Posted by Dazzer View Post
    me() is not a function. it is a reference.
    Its actually a function call from which its return value is used in the addEventListener call. Just 'me' is the function reference.

    Quote Originally Posted by Dazzer View Post
    So, to answer your question, you need to create a custom Event.
    ^ but thats spot on

  6. #6
    Tnx for ur help boys. I know i can always rely on u

  7. #7
    - bows to the great senocular, the great wise one -

  8. #8
    Reason number 523 to hate Flash and AS3. Taking something that should be simple and logical and making it 100 times more complicated than it needs to be.

  9. #9
    Quote Originally Posted by mrbelv View Post
    Reason number 523 to hate Flash and AS3. Taking something that should be simple and logical and making it 100 times more complicated than it needs to be.
    Aww, its not that hard..

    Code:
    /* AS3
    */
    package come.events {
    
    import flash.events.Event;
    
    /**
     *	Event subclass description.
     *
     *	@langversion ActionScript 3.0
     *	@playerversion Flash 9.0
     *
     *	@author Some Person
     *	@since  27.09.2008
     *	@example Basic Useage
     *	 <listing version="3.0">
     *	//some class, dispatch your event with the paramater
     *	dispatchEvent(new MyCustomEvent(MyCustomEvent.MY_EVENT,true,false,5));
     *	
     *	//someOther class, listen for that event and access the contained parameter
     *	addEventListener(MyCustomEvent.MY_EVENT, handleMyEvent)
     *	
     *	//type the eventType param to match your custom event
     *	private function handleMyEvent(event:MyCustomEvent): void {
     *		trace(event.id)
     *		//traces 5
     *	}
     *	
     *	</listing>
     */
    public class MyCustomEvent extends Event {
    	
    	//--------------------------------------
    	// CLASS CONSTANTS
    	//--------------------------------------
    	
    	public static const MY_EVENT:String = "myEvent";
    	
    	//--------------------------------------
    	// PUBLIC VARIABLES
    	//--------------------------------------
    	public var id:int
    	//--------------------------------------
    	//  CONSTRUCTOR
    	//--------------------------------------
    	
    	/**
    	 *	@constructor
    	 */
    		public var id:int;
    		
    		/**
    		*   Your event documentation description  
    		*	@langversion ActionScript 3.0
    		*	@playerversion	Flash 9
    		*/
    		public function MyCustomEvent(type:String, bubbles:Boolean, cancelable:Boolean, id:int):void {
    			super(type, bubbles, cancelable);
    			this.id = id;
    		}
    		/**
    		*	@langversion ActionScript 3.0
    		*	@playerversion	Flash 9
    		*	Creates a copy of an Event object and sets the value of each property to match that of the original.
    		*	@return Event A new Event object with property values that match those of the original.
    		*/
    		override public function clone():Event {
    			return new MyCustomEvent(this.type, this.bubbles, this.cancelable, this.id);
    		}
    		/**
    		*	@langversion ActionScript 3.0
    		*	@playerversion	Flash 9
    		*	Returns a string that contains all the properties of the Event Object
    		*	[YourEvent type=value bubbles=value cancelable=value eventPhase=value id=value]
    		*	@return String A string that contains all the properties of the Event Object.
    		*/
    	   override	 public function toString():String {
    			return formatToString("myCustomEvent","type","bubbles","cancelable","eventPhase","id");
    		}
    	}
    }

  10. #10
    Yeah... that's just absolutely asinine.

    Thanks Adobe.

  11. #11
    Quote Originally Posted by mrbelv View Post
    Yeah... that's just absolutely asinine.

    Thanks Adobe.
    What is?

  12. #12

    Pallete adding function to event listener???

    To provide some background for this question, you could send argument data to a standard function like this?

    Code:
    function showMsg(msg:String):void {
        trace(msg);
    }
    showMsg("Claire");
    However, in an event listener, only one argument is allowed in the listener function: the argument responsible for receiving the event data. Using a standard mouse click listener as an example...

    Code:
    stage.addEventListener(MouseEvent.CLICK, showMsg, false, 0, true);
    function showMsg(evt:MouseEvent):void {
        trace("hello");
    }
    ...the kind of question asked is, can you do something like this:

    Code:
    stage.addEventListener(MouseEvent.CLICK, showMsg, "hello",
                           false, 0, true);
    function showMsg(evt:MouseEvent, msg:String):void {
        trace(msg);
    }
    The answer is, not out of the box. The existing AS3 events do not provide for this capability. However, to answer Jim's question, the best way to pass arguments with an event is to create your own event class by extending AS3's Event class.

    You can try something like this::::

    Code:
    stage.addEventListener(MouseEvent.CLICK,
                  function(evt:MouseEvent){doIt(evt, "Shift key was down:")},
                  false, 0, true);
    
    function doIt(evt:MouseEvent, msg:String) {
        trace(msg, evt.shiftKey);
        trace("");
        trace(evt);
    }

  13. #13
    Quote Originally Posted by Knorcedger View Post
    The first example works, but how will i pass the variable "fire" to the function "me"?

    I tried both the 2nd and the 3rd examples but it doesnt work
    You could add a property to your item and then read it from within your me function:

    Code:
    item.id ="fire";
    item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, me);
    
    function me(myEvent:MouseEvent)
    {
    
    var dO:DisplayObject = DisplayObject(myEvent.target); mov:MovieClip = dO as MovieClip; trace(mov.id);// fire
    }

  14. #14

    one step further....

    What Im trying to do is create objects each with an eventListener with a function that passes the state name to the function. What it is doing instead, is picking up the variable "stateName" and pass the last value which was in "stateName".
    Please help....

    Code:
    for (var key:String in childArray){
      var stateName = childArray[key].@name;
      var stateMCObject:DisplayObject = this.getChildByName("mc"+stateName)
      var stateBTNObject:DisplayObject = this.getChildByName("btn"+stateName)
      stateBTNObject.x=stateMCObject.x;
      stateBTNObject.y=stateMCObject.y;
      var functionString:Function = function(evt:MouseEvent){stateRollover(evt, stateName)};
      stateBTNObject.addEventListener(MouseEvent.ROLL_OVER,functionString,false, 0, true);
    }
     
    function stateRollover(evt:MouseEvent, msg:String) {
     dtTopText.text=msg;
    }

  15. #15

    do you ever figure this out?

    Quote Originally Posted by mrbelv View Post
    Reason number 523 to hate Flash and AS3. Taking something that should be simple and logical and making it 100 times more complicated than it needs to be.
    Just curious to know if you ever figured this out? I've just come across this issue (Pass variable with addEventListener) on my own AS 3.0 learning curve. My first thoughts were similar to your own.
    Last edited by conspirisi; January 23rd, 2009 at 02:24 PM.

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012