Solved

how to pass information using variables Actionscript 3.0

Posted on 2008-10-08
4
1,490 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
HTML 5 or Flash 7 475
IE 11 Flash Player issue 7 228
camtasia alternative that can record 3 hours video plus audio 24 94
Flash ActionScript 6 21
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
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…
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 use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now