Solved

Flash - loop variables

Posted on 2011-03-24
2
214 Views
Last Modified: 2012-05-11
Hey guys, I'm trying to put the following in a loop.
var ButtonOne:MovieClip = BouncingBallBtn;
var ButtonTwo:MovieClip = BallControlsBtn;
var ButtonThree:MovieClip = DiceBtn;
var ButtonFour:MovieClip = SlideShowBtn;
var ButtonFive:MovieClip = SlideShowControlBtn;
var ButtonSix:MovieClip = QuizBtn;

Open in new window


is it possible to actually put it in a loop?
I'm trying it like this, but I get an error everytime.

var buttons:Array = ["ButtonOne","ButtonTwo","ButtonThree","ButtonFour","ButtonFive","ButtonSix"];
var names:Array = ["BouncingBall", "BallControls", "Dice", "SlideShow", "SlideShowControl", "Quiz"];

for(var a:Number=0;a<6;a++){
	var appName:MovieClip = this[names[a] + "Btn"];
	this[buttons[a]]:MovieClip = appName;
}

Open in new window

0
Comment
Question by:ErnstJacobs
[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 14

Expert Comment

by:tomaugerdotcom
ID: 35215230
I think your approach is problematic. You can't really assign a movieclip (or button) to another movieclip. Movieclips are not variables that you can assign to.

If all you're trying to do is find an easy way to access your buttons, then your "names" array should be sufficient

// do something with BallControls
var ballMC:MovieClip = MovieClip(this[names[a] + "Btn"]);

Open in new window


If you're trying to do something to all your buttons (like add event listeners), then just iterate through the list

for (var a:int = 0, l:int = names.length; a < l; ++a){
  var appName:MovieClip = MovieClip(this.getChildByName(names[a] + "Btn"));
  this.addEventListener(Event.CLICK, buttonClicked);
}

Open in new window


If you're trying to do something more complex, please let us know.

T
0
 
LVL 3

Accepted Solution

by:
James Tomasino earned 500 total points
ID: 35232942
Just drop the quotes alltogether. You can define the array elements as references to the clips directly. There's no need for getChildByName as long as the clips exist.
var names:Array = [BouncingBallBtn, BallControlsBtn, DiceBtn, SlideShowBtn, SlideShowControlBtn, QuizBtn];

for (var i:int = 0; i < names.length; ++i)
{
    var btn:SimpleButton = names[i] as SimpleButton;
    btn.addEventListener (Event.CLICK, buttonClicked);
}

Open in new window

As a side note, if you use flash.utils.Dictionary, you can then create a Dictionary object with those same buttons as the keys. In your click handler, you can lookup the values of your dictionary to switch and decide how to handle the event.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

735 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