Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Apr 22, 2019 Apr 22, 2019

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();

874
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Apr 23, 2019 Apr 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

...
Translate
Advocate ,
Apr 23, 2019 Apr 23, 2019
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines