Solved

Flash - loop variables

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
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.

856 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