Skip to main content
Known Participant
April 22, 2019
Answered

AS3: Array of buttons and mouse click listeners inside a for loop

  • April 22, 2019
  • 1 reply
  • 1003 views

I'm working on a simple Animate/Flash interface/Swf panel that will trigger JSFL scripts that are listed in an external xml file, so I can keep them visible instead of under the /Commands menu.

I'm having a problem declaring the mouse listeners inside the for loop, where the var has already finished counting when I click, so it traces the max of i on all buttons (4).

I think I need to declare the current i inside the button as it runs through the loop, (which will be replaced by the jsfl name/url items from the xml)

I don't think I can simply pass a single int to a function on click, since I want the xml to have sub folders/ groups of items.

This is a simplified code of just the button creation that shows the problem:

(Apologies, It has been a while since I wrote AS3)

import flash.display.*

import flash.events.*;

import fl.controls.*;

import flash.events.*

import flash.net.*;

var buttonSetsArray: Array = new Array();

function init() {

    for (var i: int = 0; i < 4; i++) {

        var myButton: Button = new Button();

        myButton.label = String(i);

        myButton.width = 20;

        myButton.move(22 * i, 5);

        addChild(myButton);

        buttonSetsArray = myButton;

        buttonSetsArray.addEventListener(MouseEvent.CLICK, function (evt: MouseEvent) {

            trace(i);

          //this is where I want to pass several xml items to a function, just trying to get the i for now.

        });

    }

}

init();

This topic is closed to new replies. Start a new post to keep the conversation going.
Correct answer kdmemory

Hi Lachlan

it's possible that I do not fully get where your problem is for you.

But the "problem declaring the mouse listeners inside the for loop" isn't really there, is it? I omitted the var buttonSetsArray (not really needed) and was able to trace i and evt.target.label to distinguish the buttons on click.

import flash.display.*

import flash.events.*;

import fl.controls.*;

import flash.events.*

import flash.net.*;

//var buttonSetsArray: Array = new Array();

function init() {

    for (var i: int = 0; i < 4; i++) {

        var myButton: Button = new Button();

        myButton.label = String(i);

        myButton.width = 20;

        myButton.move(22 * i, 5);

        addChild(myButton);

        //buttonSetsArray = myButton;

        myButton.addEventListener(MouseEvent.CLICK, function (evt: MouseEvent) {

            trace(i);

            trace("button label: " + evt.target.label);

            //this is where I want to pass several xml items to a function, just trying to get the i for now. 

        });

    }

}

init();

Maybe this helps

Klaus

1 reply

kdmemory
kdmemoryCorrect answer
Inspiring
April 23, 2019

Hi Lachlan

it's possible that I do not fully get where your problem is for you.

But the "problem declaring the mouse listeners inside the for loop" isn't really there, is it? I omitted the var buttonSetsArray (not really needed) and was able to trace i and evt.target.label to distinguish the buttons on click.

import flash.display.*

import flash.events.*;

import fl.controls.*;

import flash.events.*

import flash.net.*;

//var buttonSetsArray: Array = new Array();

function init() {

    for (var i: int = 0; i < 4; i++) {

        var myButton: Button = new Button();

        myButton.label = String(i);

        myButton.width = 20;

        myButton.move(22 * i, 5);

        addChild(myButton);

        //buttonSetsArray = myButton;

        myButton.addEventListener(MouseEvent.CLICK, function (evt: MouseEvent) {

            trace(i);

            trace("button label: " + evt.target.label);

            //this is where I want to pass several xml items to a function, just trying to get the i for now. 

        });

    }

}

init();

Maybe this helps

Klaus