Results 1 to 15 of 30
-
February 21st, 2007, 05:18 AM #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
and i try something like that, but it says that it expected 2 variablesCode:item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, me); and... private function me(event:ContextMenuEvent):void {
so, i try this oneCode:item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, me(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."Code:item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, me(ContextMenuEvent, fire)); and... private function me(event:ContextMenuEvent, fire:Number):void {
I dont know what else to try. Can somebody please help me?
Thanx
-
February 21st, 2007, 07:01 AM #2
It looks like the first attempt that you posted should work, can you post the specific error that is thrown?
AS4 expert.
-
February 21st, 2007, 07:29 AM #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
-
February 21st, 2007, 08:51 AM #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.cfmhttp://darylteo.com/blog
-
February 21st, 2007, 10:10 AM #5
-
February 21st, 2007, 10:28 AM #6
Tnx for ur help boys. I know i can always rely on u
-
February 21st, 2007, 10:28 AM #7
- bows to the great senocular, the great wise one -
http://darylteo.com/blog
-
September 26th, 2008, 03:49 PM #844Registered User
postsReason 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.
-
September 26th, 2008, 04:51 PM #9
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"); } } }
-
November 19th, 2008, 04:51 PM #1044Registered User
postsYeah... that's just absolutely asinine.
Thanks Adobe.
-
November 19th, 2008, 05:32 PM #11
-
November 19th, 2008, 11:10 PM #12
adding function to event listener???
To provide some background for this question, you could send argument data to a standard function like this?
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:function showMsg(msg:String):void { trace(msg); } showMsg("Claire");
...the kind of question asked is, can you do something like this:Code:stage.addEventListener(MouseEvent.CLICK, showMsg, false, 0, true); function showMsg(evt:MouseEvent):void { trace("hello"); }
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.Code:stage.addEventListener(MouseEvent.CLICK, showMsg, "hello", false, 0, true); function showMsg(evt:MouseEvent, msg:String):void { trace(msg); }
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); }
-
December 30th, 2008, 10:39 AM #131Registered User
postsYou 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}
-
January 15th, 2009, 03:48 PM #141Registered User
postsone 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; }
-
January 23rd, 2009, 01:21 PM #151Registered User
postsdo you ever figure this out?
Last edited by conspirisi; January 23rd, 2009 at 01:24 PM.

Reply With Quote




Bookmarks