Highlighted

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

Explorer ,
Apr 22, 2019

Copy link to clipboard

Copied

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

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

TOPICS
Discussions

Views

372

Likes

Translate

Translate

Report

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

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

Explorer ,
Apr 22, 2019

Copy link to clipboard

Copied

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

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

TOPICS
Discussions

Views

373

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Apr 22, 2019 0
Advocate ,
Apr 23, 2019

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Apr 23, 2019 2