Solved

how to pass information using variables Actionscript 3.0

Posted on 2008-10-08
4
1,502 Views
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
addChild(menuClip);
 
//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
menuClip.addChild(menuBack);
 
 
//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.buttonMode=true;
	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
	menuClip.addChild(myButton);
	
	//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;
	//trace(totalBtnWidth);
	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

0
Comment
Question by:gtaffet08
  • 2
  • 2
4 Comments
 
LVL 39

Assisted Solution

by:blue-genie
blue-genie earned 500 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?
0
 

Author Comment

by:gtaffet08
ID: 22684862
Hello,

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.

0
 
LVL 39

Assisted Solution

by:blue-genie
blue-genie earned 500 total points
ID: 22685898
yup.
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 :-)
0
 

Accepted Solution

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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
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…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

860 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