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

Variable Function Names

New Here ,
Feb 25, 2013 Feb 25, 2013

What I'm trying to do is add event listeners for each position up to the total of compPositions. This way, each one will have a dymanic function name. Right now, they all launch function "clicked1" but I can't figure out how to make it add numbers to the function name dynamically. Is there a different method I should be using to accomplish this goal?

Right now, compPosition = 5 so it should create 5 event listners, but I want them each to have their own function they exucute.

          var i;

          for (i = 1; i < compPositions +1; i++)

          {

                    currentPos = "pos" + i;

                    this["pos" + i].addEventListener(MouseEvent.CLICK, clicked1)

          }

Edit:

Actually, after thinking about it, I think this is a better approach, but I'm not sure how to pass over a variable using a click event.

            var i;

            for (i = 1; i < compPositions +1; i++)

            {

                  var currentPos; //now it's private to this for loop

                  currentPos = "pos" + i;

                  this["pos" + i].addEventListener(MouseEvent.CLICK, posClicked)

              }

In this version, I just need to pass over the currentPos variable to the function, but I'm not sure if that's possible.

Thanks,

Corey

TOPICS
ActionScript
437
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

Engaged , Feb 25, 2013 Feb 25, 2013

Before solving, I would caution why you need them to have a unique function/unique function name. You should be able to have them all hit the same function and do different logic within depending on the target.

That aside, you could do something like this:

for (i = 1; i < compPositions +1; i++)

{

          currentPos = "pos" + i;

          this["pos" + i].addEventListener(MouseEvent.CLICK, this["clicked" + i])

}

However, you would have to manually define each function. Alternatively, you can use closur

...
Translate
Engaged ,
Feb 25, 2013 Feb 25, 2013

Before solving, I would caution why you need them to have a unique function/unique function name. You should be able to have them all hit the same function and do different logic within depending on the target.

That aside, you could do something like this:

for (i = 1; i < compPositions +1; i++)

{

          currentPos = "pos" + i;

          this["pos" + i].addEventListener(MouseEvent.CLICK, this["clicked" + i])

}

However, you would have to manually define each function. Alternatively, you can use closures:

for (i = 1; i < compPositions +1; i++)

{

  currentPos = "pos" + i;

  this["pos" + i].addEventListener(MouseEvent.CLICK, getFunction(i))

}

function getFunction(i:int):Function

{

          return function(e:MouseEvent):void { trace("Clicked " + i); };

}

EDIT:

Using the closure method you could also pass the current pos if you needed to:

for (i = 1; i < compPositions +1; i++)

{

  currentPos = "pos" + i;

  this["pos" + i].addEventListener(MouseEvent.CLICK, getFunction(i, currentPos))

}

function getFunction(i:int, currentPos:String):Function

{

          return function(e:MouseEvent):void { trace("Clicked " + i + " currentPos = " + currentPos); };

}

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
New Here ,
Feb 25, 2013 Feb 25, 2013
LATEST

I see exactly what you're talking about now. The closures are a much better method. Thanks! 🙂

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