Solved

Flash - loop variables

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

632 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