Solved

Flash - loop variables

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
"Black Box" Testing of Control System Software 2 127
VB.net and sql server 4 67
backup program with robocopy 6 92
Java pass by reference 3 74
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 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.

752 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