[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


how to pass information using variables Actionscript 3.0

Posted on 2008-10-08
Medium Priority
Last Modified: 2013-11-11
I am working on making the transition from AS2 to AS3 and have run into a problem passing values held within variables.

In the attached code, I have succeeded in creating a dynamic menu which uses a movie clip button stored in the library (button_main) which is just a background graphic with a dynamic text field.

I am trying to target the individual menu items in the CLICK event function. I have created a variable called btnID:String (line 3 of code) to hold a name derived from the string passed in through an array during the button creation loop. I am able to get the correct value for btnID during the loop (see line 60).

I can't figure out how to get that value over to the btnClick function so I can target the individual buttons once they are created.

Note: If I pass the string value from the text field inside the button (see line 89) it works (the name assigned to the button in the for loop is transferred successfully) and I can target the clips as I want.

Can you tell me what I am doing wrong when using the variable btnID?

Thanks for your help.

import flash.geom.ColorTransform;
var buttonArray:Array = new Array("COOL","MOD", "LAME");
var btnID:String;
//create a movie clip shell so you can move as a unit
var menuClip:MovieClip = new MovieClip();
// var to hold number of buttons
var btnCount:Number = 4;
var btnPad:Number = 10;
var btnInset:Number = 10;
//initialize properties
//menuClip.x = 100;
menuClip.y = 25;
//add menuClip to stage display list
//creating instances of menu objects. 
///bg: Pull symbol from library
var menuBack:MovieClip = new graphic_menuBack();
//menuBack.width = 500;
//add menuBack to menuClip holder's display list
//for loop to create buttons for menu
for (var i:Number=0; i < buttonArray.length; i++) {
	//initialize our button instances
	var myButton:MovieClip = new button_main();
	//create local variable to hold name of button (from array) to pass to btnID;
	var btnName:String;
	//set hand cursor, block influence of text in button
	myButton.mouseChildren = false;
	//initialize a counter for total width of buttons
	var totalBtnWidth:Number;
	//initialize properties. Move x over each iteration of loop
	//btnInset is initial x placement; btnPad is the padding
	myButton.x = btnInset + (i * (myButton.width + btnPad));
	myButton.y = btnInset;
	//add buttons to display list
	//add button name to text field
	myButton.button_txt.text = buttonArray[i];
	btnName = buttonArray[i];
	trace("btnName is " + btnName);
	//btnID = buttonArray[i];
	btnID = btnName;
	trace("btnID is " + btnID);//this works
	//calculate total width for menu bg (menuBack)
	totalBtnWidth = (btnInset * 2) + (i * (myButton.width + btnPad)) + myButton.width;
	menuBack.width = totalBtnWidth;
	menuBack.height = myButton.height + (btnInset *2);
	//add mouse event listeners to buttons
	myButton.addEventListener(MouseEvent.CLICK, btnClick, false, 0, true);
	myButton.addEventListener(MouseEvent.ROLL_OVER, btnOver);
	myButton.addEventListener(MouseEvent.ROLL_OUT, btnOut);
	//center menuClip on stage
	menuClip.x = (stage.width / 2) - (totalBtnWidth/2);
	// Get access to the ColorTransform instance associated with buttonBack_mc.
	var colorInfo:ColorTransform = myButton.buttonBack_mc.transform.colorTransform;
//functions for button interaction
function btnClick(e:MouseEvent):void{
	// Set the color of the ColorTransform object.
    colorInfo.color = 0x5C35B9;
	//var btnTitle:String = e.target.btnID;
	// apply the color change to the display object
    e.target.buttonBack_mc.transform.colorTransform = colorInfo;
	trace("my name is " + e.target.button_txt.text);//this works
	trace("my name is " + e.target.btnID);//No value. Why can't I pass this variable out of the for loop
function btnOver(e:MouseEvent):void{
	// Set the color of the ColorTransform object.
    colorInfo.color = 0x5C35B9;
	// apply the change to the display object
    e.target.buttonBack_mc.transform.colorTransform = colorInfo;
function btnOut(e:MouseEvent):void{
	//e.target.buttonBack_mc.alpha= 1;
	// Set the color of the ColorTransform object.
    colorInfo.color = 0x003399;
	// apply the change to the display object
    e.target.buttonBack_mc.transform.colorTransform = colorInfo;

Open in new window

Question by:gtaffet08
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
  • 2
LVL 39

Assisted Solution

blue-genie earned 2000 total points
ID: 22684486
tell me if i'm surmising incorrectly.

out of context i see you've got a button_main class which is what you're creating instances of.
so in that class, create a set / get for the data you need.
call those methods instead.

was does  e.target.name return?

Author Comment

ID: 22684862

I have not written a custom class for the Movieclip that is acting as a button: button_main. (In the linkage info for that MovieClip symbol in the library, the base class is flash.display.MovieClip and the Class is button_main which is just the name of the symbol in the library at the moment.)

If I trace e.target.name when I click the 3 buttons I get these results:

my name is instance5
my name is instance8
my name is instance11

I will investigate the set/get methods and try to write a custom class for button_main. Thanks for your direction.

So my problem is that there is no way in AS3 to make that variable hold the ID info gathered from the array during the button creation loop and I need to write a method in the button's class that can hold the info instead, correct?

BTW: Is there a way I can send the fla to EE? When I tried to attach a zipped file it would not let me do it.

LVL 39

Assisted Solution

blue-genie earned 2000 total points
ID: 22685898
put a .txt extension after everything, i.e. filename.fla.txt.
I wouldn't say there's no way - but in terms of OOP good practise, the get /set way is the way to go.
better than those darn compile errors that something is null arggghh.

if u upload ur stuff i'll have a peruse over the weekend if nothing exciting comes up :-)

Accepted Solution

gtaffet08 earned 0 total points
ID: 22734206
Thanks very much. I am studying up on the get and set methods. I appreciate your direction.

Featured Post

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
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.

649 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