Log in

View Full Version : Simple button navigation in actionscript 3



evan
03-24-2008, 02:54 PM
In Version two the code looked like this:

stop();
home_btnone.onRelease = function(){gotoAndStop("home");}
home_btntwo.onRelease = function(){gotoAndStop("about");}
home_btnthree.onRelease = function(){gotoAndStop("contact");}

-this works.

On another thread I saw this example given given to describe trigger mouse event using actionscript 3 -but to display text.

It looks like this:

function eventResponse(evt:MouseEvent):void {
output_txt.text = “Button was clicked!”;
}
button_btn.addEventListener(MouseEvent.MOUSE_UP,eventResponse);

I tried to do this with it:

function eventResponse(evt:MouseEvent):void {

gotoAndStop("contact");}
contact_btn.addEventListener(MouseEvent.MOUSE_UP,eventResponse);

-I get no errors but it just keeps playing without responding to my button.

-Also How do I set this up with more than one button?

-some help greatly appreciated(the tutorials I find seem to cover AS 2-bummer) :confused:.

evan
03-24-2008, 03:58 PM
THIS WORKS -yes - I answered my own question -I hope this aids someone else -putting NUMBERS at the end of the function names did it.

stop();


function eventResponse1(evt:MouseEvent):void {
gotoAndStop("one");}
btn_one.addEventListener(MouseEvent.MOUSE_UP,eventResponse1);

function eventResponse2(evt:MouseEvent):void {
gotoAndStop("two");}
btn_two.addEventListener(MouseEvent.MOUSE_UP,eventResponse2);

function eventResponse3(evt:MouseEvent):void {
gotoAndStop("three");}
btn_three.addEventListener(MouseEvent.MOUSE_UP,eventResponse3);

ccesca
10-19-2009, 01:49 AM
Thanks Evan - just wanted to thank you for posting your answer because it did help me : )

evan
10-22-2009, 03:06 PM
yeah that was an old post when I was just starting out Glad it could help But I have one further suggestion, The "numbered" functions were just changing the names otherwise the error would be a duplicate function error(though I didn't know it at the time).

Also, I like to structure my listeners nice and neatly first and then write my functions, the code is cleaner.


btn_one.addEventListener(MouseEvent.MOUSE_UP,eventResponse1);
btn_two.addEventListener(MouseEvent.MOUSE_UP,eventResponse2);
btn_three.addEventListener(MouseEvent.MOUSE_UP,eventResponse3);

function eventResponse1(evt:MouseEvent):void
{
gotoAndStop("one");
}


function eventResponse2(evt:MouseEvent):void
{
gotoAndStop("two");
}


function eventResponse3(evt:MouseEvent):void
{
gotoAndStop("three");
}

you can also of course use
btn_one.addEventListener(MouseEvent.CLICK,eventResponse1);in order to have the button repond to being clicked rather than a MouseUp if you want.