• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1521
  • Last Modified:

how to pass information using variables Actionscript 3.0

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

  • 2
  • 2
3 Solutions
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?
gtaffet08Author Commented:

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.

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 :-)
gtaffet08Author Commented:
Thanks very much. I am studying up on the get and set methods. I appreciate your direction.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now