Noah R
Noah R

Reputation: 5477

What is the ActionScript 3.0 Code to Make a Simple Button?

What is the actionscript 3.0 code to make a very simple button that brings the user to the next frame? If I remember correctly in ActionScript 2.0 it was: instance_name.onPress = function(){ gotoAndStop(2) } Or something like that. However, this isn't the case for ActionScript 3.0. So could someone please let me know? Thanks!

Upvotes: 1

Views: 5696

Answers (4)

Siddharth Mirajkar
Siddharth Mirajkar

Reputation: 1

private var button:Sprite = new Sprite();

      public function ButtonInteractivity() 
        button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
        addChild(button);
      }

      private function mouseDownHandler(event:MouseEvent):void {
        your code!!
      }

Upvotes: 0

Adam Harte
Adam Harte

Reputation: 10510

ActionScript 3 uses an event based system, so to be notified when the user clicks a DisplayObject, you need to listen for the click MouseEvent.

myDisplayObject.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(event:MouseEvent):void {
  event.target.gotoAndStop(2);
}

Upvotes: 3

longstaff
longstaff

Reputation: 2051

the answers here have the corret function for the functionality, but have a consideration user experience too, you might want to assign these values:

myDisplayObject.buttonMode = true //use the "hand" cursor on mouseover
myDisplayObject.mouseChildren = false //stops the children of the button firing the event, helpful especially when having text lables etc.

Upvotes: 1

jhurtado
jhurtado

Reputation: 8747

function eventResponse(evt:MouseEvent):void {gotoAndStop(2);}

yourButton.addEventListener(MouseEvent.MOUSE_UP,eventResponse);

Upvotes: 0

Related Questions