Solved

Flash - loop variables

Posted on 2011-03-24
2
204 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 what frame rate is, how to control it and what effect it has on the video.

760 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

20 Experts available now in Live!

Get 1:1 Help Now