Skip to main content
Participating Frequently
June 16, 2009
Question

HELP: How to: AS on button

  • June 16, 2009
  • 1 reply
  • 642 views

greetings to all!

i'm a very nood in actionScript. actually i am still is a noob in flash. i just follow instructions to tutorials available on the net to do projects. i'm working now by the way. these pojects i'm working on is for some simple presentation in the office. i am using Adobe Flash CS4.

i'm trying to put an actionScript on a single button. but if i press F9 (shortcut for the action window), the action window says that "Current selection cannot have actions applied to it". How's that?

and also can somebody help me finish my presentation. i've already finished the first part (this is for the thumbs as button: ive made it marqueeing horizontally). the second part is to display a larger image in the center of the stage whenever I click a button/thumb. and then when I click the image, it goes to the next image, and so on.

i will be uplaoding the .fla for the second part of my project and hopefully someone can provide a proper script for it be able to play normally. there is already script in the project but it doesn't work because the script is for ActionScript 2.0 still. i just copied the script on a tutorial provided by Nathaniel on the other website. I think providing link on other websites is prohibited in here so i wont put a link.

thank you so much to all.

PS. this is kind of urgent and if somebody can finish this simple scripts right now, that would be so much appreciated. again, to all, thanks!

ooops the forum says .fla is not a valid attachment. i'll just upload it somewhere so you guys can have a look. i'll just post here a .swf of it.

here's hte link for the .fla: http://www.yousendit.com/download/cmcwUGhVdGo5NVhIRGc9PQ

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
June 16, 2009

In AS3 you cannot put code on symbols like you can when using AS2.  So in AS3 you have to assign instance names to your buttons/movieclips and put all the code in the timeline in the same frames where the buttons are.

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 for it and an event handler function.  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 was already defined at the end of the event listener assignment, so now you just have to write that function out:


function btn1Click(evt:MouseEvent):void {

   // your actions for the button click

}

Participating Frequently
June 16, 2009

what if, say, I want the button to display an image on the CLICK event. or adjust the alpha of the button whenever I ROLLOVER on it? can you provide me code for that? i'm really a noob here, hope you understand.

and also, how do you declare the stop(); and gotoAndPlay();?

say, I want to stop the movie on a ceratin frame? or play a certain scene when the movie/timeline reaches a certain frame?

Ned Murphy
Legend
June 16, 2009

I won't be solving all of your coding issues for you, but I will offer some answers to your questions.  To manage clicking a button to make an image appear, you could have that image in a movieclip in an invisible state and tell it to be visible in the event handler function.

To have a button change its alpha when you rollover it, then you would create a listener handler pair for that...

btnName.addEventListener(MouseEvent.ROLL_OVER, overHandler);

btnName.addEventListener(MouseEvent.ROLL_OUT, outHandler);

function overHandler(evt:MouseEvent):void {

     btnName.alpha = 0.5;

}

function outHandler(evt:MouseEvent):void {

     btnName.alpha = 1;

}

Use the stop(); command when you want a timeline to stop.  Look up gotoAndPlay(...) in the help documents.  It is explained there.