Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

assign code to buttons

New Here ,
Jul 16, 2013 Jul 16, 2013

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

TOPICS
ActionScript
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 16, 2013 Jul 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

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2013 Jul 16, 2013

Hi Amy,

Are there any book where this subject is written,

it's pretty new for me.

tia

Herman

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Jul 17, 2013 Jul 17, 2013
LATEST
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 16, 2013 Jul 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2013 Jul 16, 2013

Thanx Ned  for the support so farschermflash.jpg

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 16, 2013 Jul 16, 2013

these buttons should go to assigned webpages

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 16, 2013 Jul 16, 2013

Here's something I used to post often but haven't run into needing to for awhile:

Explaining AS3 Button Code
--------------------------

Let's say you create a button symbol.  Since it is a button, it is already a self animating object that will react to mouse interactions, but only visually at this stage.  The first thing you need to do to make it useful code-wise is to assign it a unique instance name.  So you drag a copy of it out to the stage from the library, and while it's still selected, you enter that unique instance name for it in the Properties panel... let's say you name it "btn1"


In AS3, to make a button work with code, you need to add an event listener and event handler function for it.  You might need to add a few (for different events, like rollover, rollout, clicking it, but for now we'll just say you want to be able to click it to get a web page to open.  In the timeline that holds that button, in a separate actions layer that you create, in a frame numbered the same as where that button exists, you would add the event listener:


btn1.addEventListener(MouseEvent.CLICK, btn1Click);

The name of the unique function for processing the clicking of that button is specified at the end of the event listener assignment, so now you just have to write that function out:


function btn1Click(evt:MouseEvent):void {

   var url:String = "http://www.awebsite.com/awebpage.html";

   var req:URLRequest = new URLRequest(url);

   navigateToURL(req);

}


Here's some of what's involved there:


evt:MouseEvent - the event listeners throws an argument automatically which the function must be set up to receive.  In this case I have given that argument a variable name of evt, though I could have chosen anything.


:void - this defines the class of the value that the function will return.  In this case, the function does not return anything, so "void" is used.  If it did return a value, you would see a line containing "return xyz"; in the function (where xyz is not literal, it simply represents the variable or value being returned)


The normal code to open a web page contains three distinct elements, the url, the request, and the command to get the page, so I have shown them as three separate lines.  Many people will combine them into one line.


In AS3, in strict mode, it is necessary to identify the types/classes of the variables being created, which is why you see :String, :MouseEvent, etc... showing up everywhere.


I know that's probably clear as mud in the explanation, but hopefully it will shed some light on what you're working with.


Now, to create another button with a unique function for it, you could just drag another copy of it from the library, give it a unique name, say btn2, copy/paste the code from btn1 and replace "btn1" with "btn2" in that copied code, adding a new url for the page that button will open.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines