开发者

Actionscript3: add and remove EventListeners (with dynamic name and dynamic variables)

开发者 https://www.devze.com 2022-12-17 12:46 出处:网络
I am making a boardgame in flash Action Script 3. Each position on the board is a buttons like this: button_1_1, button_1_2 etc. Whenever a character is selected you want to move it so the script has

I am making a boardgame in flash Action Script 3. Each position on the board is a buttons like this: button_1_1, button_1_2 etc. Whenever a character is selected you want to move it so the script has to add event listeners for positions around the selected unit


// This function adds or deletes an event listener
function listentoButton (isTrue:int, position_x:int, position_y:int):void {
    var myFunction:Function = new Function;
    myFunction = function ():void {userClickedPosition(position_x, position_y)};
    if (isTrue == 1) {
        this["button_position_"+(position_x)+"_"+(position_y)].addEventListener(MouseEvent.CLICK, myFunction);
    } else {
        this["button_position_"+(position_x)+"_"+(position_y)].removeEventListener(MouseEvent.CLICK, myFunction);
    }
}

In the rest of the code I have:

  • function userClickedPosition(position_x:int, position_y:int)

    it selects or deselect a unit

  • function selectUnit(position_x:int, position_y:int):

    it uses the listentoButton(1) function to add 8 listeners (the positions around the clicked unit)

  • function deselectUnit(position_x:int, position_y:int):

    it uses the listentoButton(0) function to delete 8 listeners (the positions around the clicked unit)

My question: adding eventlisteners is no problem 开发者_如何学Gobut removing them dont seem to work? What did I do wrong?


When you go to remove the event, you are using a new instance of myFunction, not the same one you added it with. You either need to declare the function like you would any other function, and use the event args to examine the button's position like. I Think you want the stageX and stageY properties: http://www.adobe.com/livedocs/flex/3/langref/flash/events/MouseEvent.html

// This function adds or deletes an event listener
function listentoButton (isTrue:int, position_x:int, position_y:int):void {
    if (isTrue == 1) {
        this["button_position_"+(position_x)+"_"+(position_y)].addEventListener(MouseEvent.CLICK, myFunction);
    } else {
        this["button_position_"+(position_x)+"_"+(position_y)].removeEventListener(MouseEvent.CLICK, myFunction);
    }
}

function myFunction(eventArg:MouseEvent):void {
//use MouseEvent
};

Or you can create a little MyFunctionParameters class to hold the coordinate information and create a new instance of that class, add it to a collection indexed by the x and y coordinates, and later when you go to remove the event, you would lookup the MySpaceParameters instance in the collection, based on x and y coordinates, then use that to remove function.

class MyFunctionParameters
{
public x:int;
public y:int;
    function myFunction(eventArg:MouseEvent):void {
        userClickedPosition(x,y);
    };
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号