Question

I have a few different question and i have decided to put them in one. So the first question : If i have one and the same code(for example):

buttonsA.buton1a.addEventListener(MouseEvent.MOUSE_DOWN , buton1a_function);
buttonsA.buton2a.addEventListener(MouseEvent.MOUSE_DOWN , buton2a_function);
buttonsA.buton3a.addEventListener(MouseEvent.MOUSE_DOWN , buton3a_function);
buttonsA.buton4a.addEventListener(MouseEvent.MOUSE_DOWN , buton4a_function);
buttonsA.buton5a.addEventListener(MouseEvent.MOUSE_DOWN , buton5a_function);
buttonsA.buton6a.addEventListener(MouseEvent.MOUSE_DOWN , buton6a_function);
buttonsA.buton7a.addEventListener(MouseEvent.MOUSE_DOWN , buton7a_function);
buttonsA.buton8a.addEventListener(MouseEvent.MOUSE_DOWN , buton8a_function);
buttonsA.buton9a.addEventListener(MouseEvent.MOUSE_DOWN , buton9a_function);
buttonsA.buton10a.addEventListener(MouseEvent.MOUSE_DOWN , buton10a_function);

and i want to put it in several places(in different conditions) can i put them in a function a call a function instead a copying a large amout of text (I thought about 'include' from a different file but i want to keep all the information in one file).

The second question is about arrays : In my situation i have an array and i .push() a different numbers in it.But it could be "1,51,11,2,13' or "1,2,3,4,5" so every time place of numbers (and numbers themselves) are different. How can i say to AS3 in function to remove(.splice) exactly the number 5 or 6 (in spite of their place in the array).

The third question is again about the "code" that is upper in the question. Can i maybe with the loop for to make all these 10 addEventListen with a fewer code (i think it should be something like that:)

    for(var i:int = 1; i <= 100; i++){
   //buttonsA.buton'i'a.addEventListener(MouseEvent.MOUSE_DOWN , buton'i'a_function);
    }

Long story short maybe i didn't have to put so much question and maybe my thoughts are not correct, but i think that my questions are easy but i can't solve them. Any decisions and replies are welcomed :) Thanks.

Was it helpful?

Solution

First Question:

Not sure I understand your first question. I'll take a stab at it and say you're wanting the functionality of the button mouse down to be enabled during different contexts of your application state, but you don't want to repeat all the event listeners all the time?

if so, you should make a subclass for all your button to inherit from. It could look something like this:

public class ButtonSubClass extends Sprite {  //or simple button, or whatever
    public function ButtonSubClass():void {
        this.addEventListener(MouseEvent.MOUSE_DOWN,downHandler,false,0,true);
    }

    private function downHandler(e:MouseEvent):void {
        //do something common to all your buttons here
    }
}

Then, have all your buttons inherit from this class.

Second Question:

function removeFromArray(elementVal:*):void {
    var index:int = array.indexOf(elementVal);  //get the index of the first element whose value is the passed parameter;
    if(index >= 0){
        array.splice(index,1); //remove that element/index from the array.
    }
}

Third Question:

If ButtonA is a sprite whose only children are all the buttons you want the listener for, then you can do this:

var i:int = buttonA.numChildren;
while(i--){
    buttonsA.getChildAt(i).addEventListener(MouseEvent.MOUSE_DOWN , button_function);
}

function button_function(e:Event):void {
    switch(e.currentTarget){
        case button1a:
            //do something with button 1a
            break;

        case button2a
            //do something with button 2a
            break;
    }
}

OR, more sloppily and less efficient and not recommended, this:

for(var i:int=1;i<=10;i++){
    this["buton" + i.toString() + "a"].addEventListener(MouseEvent.MOUSE_DOWN, this["buton" + i.toString() + "a_function"]); 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top