Home  |  About  | Last |  Submit  |  Contact
AllQuests.com



Previous Question:  [FLEX2 AS3]Need help getting data objects onto the stage  ActionScript 3.0Next Question:  Classes: (not so) Private Variables?  ActionScript 3.0
Question [AS3] Pass variable with addEventListener ( kirupaForum ActionScript 3.0 )
Updated: 2008-05-20 19:17:45 (17)
[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

Answers: [AS3] Pass variable with addEventListener ( kirupaForum ActionScript 3.0 )
[AS3] Pass variable with addEventListener

It looks like the first attempt that you posted should work, can you post the specific error that is thrown?

Krilnon

[AS3] Pass variable with addEventListener

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

Knorcedger

[AS3] Pass variable with addEventListener

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

Dazzer

[AS3] Pass variable with addEventListener

Quote:
Originally Posted by Dazzer
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
So, to answer your question, you need to create a custom Event.
^ but thats spot on

senocular

[AS3] Pass variable with addEventListener

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

Knorcedger

[AS3] Pass variable with addEventListener

- bows to the great senocular, the great wise one -

Dazzer

[AS3] Pass variable with addEventListener

Hi there i tried onw of the methods in this post and i did it, the problem is to return that value..


var xmlLoader:URLLoader = new URLLoader();
xmlLoader.addEventListener(Event.COMPLETE, DATAXML);
xmlLoader.load(new URLRequest("galeria.xml"));

function DATAXML(e:Event):void {
var xmlData:XML=new XML(e.target.data);
}

function DATAXML2(id:Number):Function {
return function(e:Event) : void {
var xmlData:XML=new XML(e.target.data);
globais.vrb.nome = xmlData.pic[id].image[0];
trace (globais.vrb.nome); <-------- HERE THE OUTPUT IT'S OK
}
}

xmlLoader.addEventListener(Event.COMPLETE, DATAXML2(globais.vrb.num_i));

trace (globais.vrb.nome); <---- HERE THE OUTPUT IS UNDEFINED

"globais.vrb.[other]" <--- is my global object to make a global variable..

Say something i'm stuck..

Or give me another way to make this..

Fhavio

[AS3] Pass variable with addEventListener

Just a heads up, I got around having to 'recreate the EventListener wheel just to pass an additional parameter' by referencing 'e.target.name'.

Since my MCs are all named, I could then strip out the # from the MC name, and grab the description from the global array positioned at that # (from within the EventHandler code).

Works good!

bhjodokast

[AS3] Pass variable with addEventListener

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

cheinan

[AS3] Pass variable with addEventListener

Quote:
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);
}
This works like a champ, just used it myself

jinxology

[AS3] Pass variable with addEventListener

I just want to say how helpful this information has been. I have been working on a flash-based forum and trying to figure out how to expand and collapse an indeterminate number of individual posts. Knowing that it's possible to pass a string from an eventListener to a function, has saved me a lot of time and several headaches. The method allowed me to accomplish the task with just 13 lines of code. Not knowing this, it would have taken me at least 20x as much.

Code:
public function clickTest(msg:String):Function {
        return function():void {
            if(forumMessageSlide.getChildAt(stringToNum(msg)).height>=200) {
                forumMessageSlide.getChildAt(stringToNum(msg)).height=22;
            }else{
                forumMessageSlide.getChildAt(stringToNum(msg)).height=218;
            }
            var t3:int = forumMessageSlide.numChildren;
            for(i=0; i<t3-1; i++) {
                forumMessageSlide.getChildAt(i+1).y = forumMessageSlide.getChildAt(i).y + forumMessageSlide.getChildAt(i).height + 5;
            }
        }
    }
My work in progress can be seen here: http://www.battletech-live.net

charlesshoults

[AS3] Pass variable with addEventListener

Quote:
Originally Posted by PBandJ
Alternatively you can void out the function reference and pass parameters to a function from within the event listener like this:

backButton.addEventListener("mouseOver", showTip("backTip"));

function showTip(tipName:String):Function {
return function():void
{
switch (tipName)
{
case "backTip":
backTip.alpha = 1;
break;
case "forwardTip":
forwardTip.alpha = 1;
break;
case "playTip":
playTip.alpha = 1;
break;
case "pauseTip":
pauseTip.alpha = 1;
break;
}
}
}

Enjoy.
Awesome, it works, but I'm having problem removing the eventlistener after this...

My eventlistener looks like this:
imageContainer1_mc.addEventListener(MouseEvent.CLI CK, changeThumbPicture(number, XML_itemType[number]));

But when I click another button I want to add a new eventlistener with new a number passed to the function changeThumbPicture, but doesn't seem to work, it adds a new eventlistener only so I have two, what can I do?

Urme

[AS3] Pass variable with addEventListener

you can pass variables to your listener if you want:

Code:
for(var i:uint = 0; i < images.length; i++) {
     this["image"+i].addEventListener(MouseEvent.CLICK, imageClick(i));
}
function imageClick(id:uint):Function{
     return function(m:MouseEvent) : void {
          trace("you clicked ", id);
     }
}

sebrofm

[AS3] Pass variable with addEventListener

Alternatively you can void out the function reference and pass parameters to a function from within the event listener like this:

backButton.addEventListener("mouseOver", showTip("backTip"));

function showTip(tipName:String):Function {
return function():void
{
switch (tipName)
{
case "backTip":
backTip.alpha = 1;
break;
case "forwardTip":
forwardTip.alpha = 1;
break;
case "playTip":
playTip.alpha = 1;
break;
case "pauseTip":
pauseTip.alpha = 1;
break;
}
}
}

Enjoy.

PBandJ

[AS3] Pass variable with addEventListener

Custom event is the better way to go, but if you are feeling lazy, just a reference to e.currentTarget should work.

Daganev

[AS3] Pass variable with addEventListener

var i:Number;
i=1;
var Id:Object = new Object();
item.id = 1;
item.addEventListener(ContextMenuEvent.MENU_ITEM_S ELECT, me);

private function me(event:ContextMenuEvent):void {
//trace "1";
trace(e.target.Id)
}

greg999

[AS3] Pass variable with addEventListener

Quote:
Originally Posted by senocular
What is?
Having to create a custom even just to be able to pass variables to a function called via an event handler.

Do the people at Adobe even stop to think about what people are attempting to do with Flash? Do they not have any commons sense whatsoever?

The logical way of thinking is "I have a function. I need to pass varaible to it." How the function is being called should be completely irrelevant. That is something that should be handled on the back end. Saying "other programming languages handle it this way" is absolutely not a valid answer.

mrbelv

Previous Question:  [FLEX2 AS3]Need help getting data objects onto the stage  kirupaForum  ActionScript 3.0Next Question:  Classes: (not so) Private Variables?  kirupaForum  ActionScript 3.0

- Source: [AS3] Pass variable with addEventListener kirupaForum ActionScript 3.0
- Previous Question: [FLEX2 AS3]Need help getting data objects onto the stage kirupaForum ActionScript 3.0
- Next Question: Classes: (not so) Private Variables? kirupaForum ActionScript 3.0