Solved

Dynamically Creating and Assigning a Handler for each Button

Posted on 2003-10-21
2
137 Views
Last Modified: 2010-04-03
Hi,

Well, here's my problem:

I am trying to dynamically create a list of movieclips with button in each. I want to assign each button a specific event handler for the onRelease, for example. The following code was produced:

      clip.readMore.btn.onRelease = function () {
            trace ("Clicked on Button#" + i);
      };

...where essentially "btn'' is the button. Now the problem is, whenever I click any button, I get the same value, which is the max value for i.

What I think is the problem is that Flash stores the code of the function as is without evaluating the symbol or variable i. Then, when the button is called, it fetches the value of i and uses it.

I want such a way that I can "inject" the value of the current i, knowing the this piece of code runs in a for-loop.

Thanks in advance
0
Comment
Question by:amotaz
2 Comments
 
LVL 27

Accepted Solution

by:
negatyve earned 100 total points
ID: 9595538
you have to save the i variable as an button instance property. for example:

for(var i = 0; i < 20; i++){
      clip.readMore["btn" + i].ID = i;
      clip.readMore["btn" + i].onRelease = function ()
      {
            trace ("Clicked on Button#" + this.ID);
      };
};
0
 

Author Comment

by:amotaz
ID: 9661683
negatyve,

Thank you. Although that's not exactly what I wanted, but it worked.

Setting the button's .ID worked.

Thanks again.

I have another question that I have posted already. Please take a look at it.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
YouTube Link 10 347
How to display a webpage in a flash 10 345
Adobe Flash Free Transform Tool 2 121
What process can I use to achieve this? 5 108
There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now