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
Solved

Dynamically Creating and Assigning a Handler for each Button

Posted on 2003-10-21
2
141 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
While working over numerous projects I often had the requirement for doing a screen capture in AS3.0. Unfortunately I found no "ready made" solutions in google search that suited my requirements. But I did come across some great resources which help…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

840 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