Solved

Dynamically Creating and Assigning a Handler for each Button

Posted on 2003-10-21
2
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…

717 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