addEventListener 의 이벤트핸들러로 변수 전달하기

Pass variable with addEventListener

addEventListener 의 이벤트핸들러로 변수 전달하기

 

Knorcedger

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

item.addEventListener(ContextMenuEvent.MENU_ITEM_S ELECT, me);

and…

private function me(event:ContextMenuEvent):void {
and i try something like that, but it says that it expected 2 variables

item.addEventListener(ContextMenuEvent.MENU_ITEM_S ELECT, me(fire));

and…

private function me(event:ContextMenuEvent, fire:Number):void {
so, i try this one

item.addEventListener(ContextMenuEvent.MENU_ITEM_S ELECT, 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?

이번에는 addEventListener 의 이벤트핸들러로 변수를 전달할수 있을까 하는 의문이 들었다.
역시나 나와 똑같은 고민을 하는 친구는 이세상에 없을리가 없다.
나처럼 여러가지로 시도해보고 모두 안된다는걸 알았을 것이다;
과연 명쾌한 해답은 있을까?

 


  

Dazzer
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

이벤트핸들러는 함수가 아니라 참조일뿐이라고 잘난척하고있다. 그걸 누가 모르나? -_-;
완벽한 방법이라며 걸어둔 링크를 따라가보면, 커스텀이벤트를 따로 만들고 그 이벤트에는 파라메터를 포함하면서 이벤트만 전달하는 방식이다.

이방법은 크게 와닿지가 않는다.

iandrio
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:

item.id =”fire”;
item.addEventListener(ContextMenuEvent.MENU_ITEM_S ELECT, me);

function me(myEvent:MouseEvent)
{
var dO:DisplayObject = DisplayObject(myEvent.target);
mov:MovieClip = dO as MovieClip;
trace(mov.id);// fire
}

색다른 방법이다. 코드를 보면 객체안에 자식인자를 두고 그곳에 전달하고자 하는 변수를 넣어둔다.
그리고 이벤트핸들러에서는 그렇게 저장된 변수를 꺼내 쓰는 방식이다.
내가 처한 상황을 어느정도 해결할 수 있을것 같지만, 궁극적인 해결책은 아닌듯.

apu808
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….

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_OV ER,functionString,false, 0, true);
}

function stateRollover(evt:MouseEvent, msg:String) {
dtTopText.text=msg;
}

역시 이런 꼼수스러운 방법이 항상 내 스타일인 것 같다.
Function 으로 정의된 변수(functionString)에 함수를 넣는데 그 함수는 이벤트와 전달하고자 하는 변수를 넘겨받는 또다른 함수(stateRollover)를 호출한다.
그리고 그렇게 정의된 함수(functionString)를 이벤트핸들러로 사용한다.
I love this code!

View Full Version : [AS3] Pass variable with addEventListener

 

One response to “addEventListener 의 이벤트핸들러로 변수 전달하기”

  1. 857

    맨 마지막 방법 안되는거네요..

Leave a Reply