Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting variables with a button AS3

Status
Not open for further replies.

overyde

Programmer
May 27, 2003
226
ZA
Hi,
I'm prety handy with AS2 but AS3 has come and pulled the carpet from under my feet. I need to know how to do the following. In AS2 you would set this script on the button:
Code:
on (release){
    variable = "variable amount";
    gotoAndStop("labeled frame");
}
I've pretty much got the gotoAndStop part right in AS3 with functions but do I need to make a new function everytime I want to change the variable. i.e.
Code:
ins_btn1.addEventListener(MouseEvent.CLICK, land_on_frame);
function land_on_frame(event:MouseEvent):void
{
	gotoAndStop("labeled frame");
        variable = "variable amount set in function";
}
I'm looking for something like:
Code:
ins_btn1.addEventListener(MouseEvent.CLICK, land_on_frame);
ins_btn2.addEventListener(MouseEvent.CLICK, land_on_frame);
function land_on_frame(event:MouseEvent):void
{
	gotoAndStop("labeled frame");
        variable = "variable amount set by button";
}


Reality is built on a foundation of dreams.
 
Make both "ins_btn1" and "ins_btn2" as instances of a Class (extends Sprite/MovieClip/Button - depending on what you are after). Then you can set up a private variable in the Class and give a value to it when the Class is instantiated. To access the variable from mouse click, do:
Code:
function land_on_frame(event:MouseEvent):void
{
   trace(event.target.myVariable);
}
This is a standard AS3 way. If you need more concrete example, let me know.

Kenneth Kawamoto
 
I'm kinda getting what you mean but not 100% sure. If you could give me a more concrete example that'd be great ;)

Reality is built on a foundation of dreams.
 
I strongly recommend to use an external Class as the Document Class and do not put a single code in the Timeline, but for the sake of this example I use the Timeline without proper Class structure :)

1. Create a new FLA document and save.

2. Create a new ActionScript file and save it as "MyButton.as" in the same directory where the FLA file above sits.

3. In "MyButton.as" type in the following:
Code:
package {
	import flash.display.SimpleButton;
	
	public class MyButton extends SimpleButton {
		public var myVariable:int;
		
		public function MyButton():void {
			super();
		}
	}  
}

4. Create a Button Symbol in the Library, and draw something so that you can click!

5. Go to Properties of the above Button. Set Name to "MyButton", enable "Export for ActionScript". Make sure Class is "MyButton", and Base class is "flash.display.SimpleButton".

6. Drag a couple of "MyButton" from the Library to the Stage, name them "button0" and "button1".

7. Type in the following in the Timeline:
Code:
button0.myVariable = 23;
button1.myVariable = -666;

button0.addEventListener(MouseEvent.CLICK, buttonClick);
button1.addEventListener(MouseEvent.CLICK, buttonClick);

function buttonClick(e:MouseEvent):void {
	trace(e.target.myVariable);
}

That's it. You should get the value you set when you click on the buttons.

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top