Skip to main content
Participating Frequently
July 16, 2013
Question

assign code to buttons

  • July 16, 2013
  • 2 replies
  • 1236 views

Hi there,

I have another question

In action scripting 3.0 you can not use the 'get url' function.

so i found this code on the internet.

var myURL:String = "http://www.dnm.nl/index.html";

var myURLRequest:URLRequest = new URLRequest (myURL);

try

{

          navigateToURL(myURLRequest, '_blank');

}

catch (error:Error)

{

          trace ("Error when navigating to website: "+error);

}

but you can not assign code to a button, so i made 3 layers for 3 individual buttons

in frame 1 of thel layer i put the code in an keyframe.

but after testing the movie, the buttom does not work and it goes straight to the designated website

what is the best way to assign multiple buttons on a page to different webpages

so 3 buttons = 3 different webpages

which book do you reccommend

tia

Herman

This topic has been closed for replies.

2 replies

Ned Murphy
Legend
July 16, 2013

The difference between AS2 and AS3, aside from some function names and overall performance, is that you cannot assign code "on" a button in AS3 like you can in AS2, but you certainly can assign code to a button.  It gets a little more verbose to do so in AS3, but it easy enough to get used to doing. 

Pretty much like Amy just showed, you assign an event listener for the button and you assign an event handler function for that listener which gets called when the event is detected.  In Amy's case, she is going a step further to show that you could have that function working for all your buttons by checking which button dispatched the event.  But you could just as well have a separate function for each listener as long as you name them uniquely.

Participating Frequently
July 16, 2013

Thanx Ned  for the support so far

Participating Frequently
July 16, 2013

these buttons should go to assigned webpages

Amy Blankenship
Legend
July 16, 2013

myButton.addEventListener(MouseEvent.CLICK, goToPage);

function goToPage(e:MouseEvent):void {

     var url:String;

    switch (e.currentTarget) {

            case (myButton):

               url = "http://www.yourdomain.com/page1"

               break;

             //other cases for other buttons

    }

    //your url navigation logic from above, using the url variable populated in the case statement

}

Participating Frequently
July 16, 2013

Hi Amy,

Are there any book where this subject is written,

it's pretty new for me.

tia

Herman

Amy Blankenship
Legend
July 17, 2013