Forum Topic: As3: Buttons Explained

(10,179 views • 25 replies)

This topic is 1 page long.

<< < > >>
None

Flynny

Reply To Post Reply & Quote

Posted at: 7/2/07 08:09 PM

Flynny EVIL LEVEL 16

Sign-Up: 05/04/03

Posts: 226

This tutorial will show you how to use a button for navigation with AS3.

So first we must create a button. You can click Insert>New Symbol>Button and then draw your button inside of this new symbol. Or you can draw a button, highlight it and click Modify>Convert To Symbol>Button.

After you have created your button, you must give it an Instance Name.
I will give my button the instance name 'intro_btn' (Without the quotation marks)

Now open the actions panel for the frame that your button is on (Press F9). Paste the following script into your actions panel. (I have commented each part to give you a better
understanding of how it works):

// START OF SCRIPT

stop();

// The stop function stops the movie timeline on the current frame.

function onIntroClick(evt:MouseEvent):void {
gotoAndPlay(2);
}

// We have created a function. This is triggered when the End User clicks the button.
// This is followed by an Event, which is a Mouse Event.
// And because we aren't returning anything, we write void.
// Between our braces we tell the player what we want it to do. In this case we want to goto
// and play a frame. My intro is on frame 2, so i have typed 2.

intro_btn.addEventListener(MouseEvent.CLICK, onIntroClick);

// We have added an Event Listener, so that when we Click the intro_btn, the onIntroClick
// function is triggered.

// END OF SCRIPT

And that's a guide to creating buttons using Actionscript 3. To make multiple buttons just copy the script, but change the instance name and the function name for each button you use.

BBS Signature

None

clixels

Reply To Post Reply & Quote

Posted at: 7/2/07 08:11 PM

clixels LIGHT LEVEL 12

Sign-Up: 05/23/07

Posts: 295

gosh, I'm just gonna stick with as2, as3 is way to hard to understand. They say its supposed to be simpler, but it really isn't.

Your mission is to fail this mission, do you accept?


None

authorblues

Reply To Post Reply & Quote

Posted at: 7/2/07 08:12 PM

authorblues FAB LEVEL 12

Sign-Up: 06/21/05

Posts: 6,360

At 7/2/07 08:11 PM, clixels wrote: gosh, I'm just gonna stick with as2, as3 is way to hard to understand.
They say its supposed to be simpler, but it really isn't.

who said anything about simpler? its more powerful, but not simpler.

GENERATION 1-i: The first time you see this, copy it into your sig on any forum. Square it, and then add i to the generation.

BBS Signature

None

Kuoke

Reply To Post Reply & Quote

Posted at: 7/2/07 08:14 PM

Kuoke DARK LEVEL 23

Sign-Up: 05/25/06

Posts: 2,507

Indeed, Shame that I finally got used to AS2 just before AS3 came out >_<.

BBS Signature

None

Flynny

Reply To Post Reply & Quote

Posted at: 7/2/07 08:16 PM

Flynny EVIL LEVEL 16

Sign-Up: 05/04/03

Posts: 226

At 7/2/07 08:11 PM, clixels wrote: as3 is way to hard to understand...

Its just as easy/hard to understand as AS2. Your just not used to it yet

BBS Signature

None

clixels

Reply To Post Reply & Quote

Posted at: 8/2/07 12:54 AM

clixels LIGHT LEVEL 12

Sign-Up: 05/23/07

Posts: 295

how would you make multiple buttons, I tried something and I don't have the example, but could you put how to to it.

Your mission is to fail this mission, do you accept?


None

clixels

Reply To Post Reply & Quote

Posted at: 8/2/07 01:14 AM

clixels LIGHT LEVEL 12

Sign-Up: 05/23/07

Posts: 295

Never mind, I just figured it out.

sorry for the double post

Your mission is to fail this mission, do you accept?


None

UnknownFear

Reply To Post Reply & Quote

Posted at: 8/11/07 05:42 PM

UnknownFear FAB LEVEL 34

Sign-Up: 02/27/04

Posts: 6,913

At 7/2/07 08:11 PM, clixels wrote: gosh, I'm just gonna stick with as2, as3 is way to hard to understand. They say its supposed to be simpler, but it really isn't.

I feel the same way. To goin from

On(Release){
gotoAndPlay(2);
}

to

function onIntroClick(evt:MouseEvent):void {
gotoAndPlay(2);
}

intro_btn.addEventListener(MouseEvent.CL ICK, onIntroClick);

I am sorry, but the fuck is that more simpler that the AS2 button script? I find AS3 VERY confusing

Blog
[ T | F | G/MSN ]
--Ubuntu 9.10 "Karmic Koala" User--


None

Cjross313

Reply To Post Reply & Quote

Posted at: 4/24/08 08:55 PM

Cjross313 DARK LEVEL 07

Sign-Up: 11/21/07

Posts: 11

It's not hard, it's just more to copy and paste. =P

Anyways, you could also use the URLrequest with it to go to a website, but I'm still working on that...

Insanity is a true sign of genious...


None

rhahnemann

Reply To Post Reply & Quote

Posted at: 5/8/08 02:15 PM

rhahnemann NEUTRAL LEVEL 01

Sign-Up: 05/08/08

Posts: 1

The code works well for me the first time I use the button - but if I want to use it again, which I will, nothing happens??????


None

Hk3

Reply To Post Reply & Quote

Posted at: 6/27/08 10:36 PM

Hk3 FAB LEVEL 05

Sign-Up: 06/21/08

Posts: 168

At 8/11/07 05:42 PM, UnknownFear wrote:
At 7/2/07 08:11 PM, clixels wrote: gosh, I'm just gonna stick with as2, as3 is way to hard to understand. They say its supposed to be simpler, but it really isn't.
I feel the same way. To goin from

On(Release){
gotoAndPlay(2);
}

to

function onIntroClick(evt:MouseEvent):void {
gotoAndPlay(2);
}

intro_btn.addEventListener(MouseEvent.CL ICK, onIntroClick);

I am sorry, but the fuck is that more simpler that the AS2 button script? I find AS3 VERY confusing

It might not look simpler but it makes a lot more sense coding wise, since a button is 100% a listener and this makes all the code go on the timeline which streamlines it for me by about 600000%.

BBS Signature

None

kid-dude

Reply To Post Reply & Quote

Posted at: 6/27/08 10:39 PM

kid-dude DARK LEVEL 06

Sign-Up: 05/12/05

Posts: 507

I have to agree. AS3 is a lot similiar to modern object orientated prog langs than AS2. Its just something we have to deal with. For the immediate future I will be keeping to AS2 till I'm confident.

szafranko wrote:
im willing to put much credit for somone who can create a dorito's character to my likeing....
Linkage: Cracked | Geekologie | xkcd | smbc


None

OzMafioso

Reply To Post Reply & Quote

Posted at: 12/7/08 02:58 PM

OzMafioso NEUTRAL LEVEL 17

Sign-Up: 10/16/04

Posts: 145

I appreciate this :) I've been using AS2 since I've started and this is my first dive into AS3... and I've gotta be honest, it does scare me a little, lol. But thanks to contributers such as yourself, It shouldn't be too bad of an experience :) Thanks for this!


Questioning

ONYX-qotsa

Reply To Post Reply & Quote

Posted at: 2/19/09 04:55 AM

ONYX-qotsa LIGHT LEVEL 02

Sign-Up: 11/29/05

Posts: 1

What happens if you want two buttons on stage, or more, say, one to go forward and one to go back. Do they both have to refer to the onIntroClick command? IF so how would it distinguish which button is pressed?
I mean, if more than one button is on the stage, one goes to frame 2

function onIntroClick(evt:MouseEvent):void {
gotoAndPlay(2);
}

and one goes to frame 4

function onIntroClick(evt:MouseEvent):void {
gotoAndPlay(4);
}

Is this what you do? because surely that wouldnt work.


None

Toniko-x

Reply To Post Reply & Quote

Posted at: 2/19/09 06:08 AM

Toniko-x NEUTRAL LEVEL 05

Sign-Up: 06/16/03

Posts: 91

Personally, I really hate how the Flash CS3 button dealio works.

It's powerful, it's better, but it isn't for everyone.

That's because I don't fiddle much at all with the actionscript, but it just becomes more less user friendly for those who just want to showcase their animations online.


None

Xeptic

Reply To Post Reply & Quote

Posted at: 2/19/09 06:25 AM

Xeptic LIGHT LEVEL 30

Sign-Up: 05/12/05

Posts: 2,181

At 2/19/09 04:55 AM, ONYX-qotsa wrote: Is this what you do? because surely that wouldnt work.

You could have two buttons with seperate EventListeners, like so:

button1.addEventListener(MouseEvent.CLICK, button1Clicked);
button2.addEventListener(MouseEvent.CLICK, button2Clicked);
//and add the two different functions to your code:
function button1Clicked (event:MouseEvent):void {
  do something;
}

function button2Clicked (event:MouseEvent):void {
  do something else;
}

Or you could add a general mouse click listener like so:

addEventListener(MouseEvent.Click, onClick);
//with the function:
function onClick(event:MouseEvent):void {
  if (event.target == button1) {
    do this;
  }
  if (event.target == button2) {
    do that;
  }
}
BBS Signature

None

Deadclever23

Reply To Post Reply & Quote

Posted at: 2/19/09 06:30 AM

Deadclever23 FAB LEVEL 12

Sign-Up: 11/27/06

Posts: 1,034

At 4/24/08 08:55 PM, Cjross313 wrote: It's not hard, it's just more to copy and paste. =P

Lol, can you not write this.
It's 4 lines of code.
It takes barely a minute.

IndyDev - Reviews, News and Tutorials. the hottest new Indy blog of it's kind. (Shameless self promotion).

BBS Signature

None

Saza

Reply To Post Reply & Quote

Posted at: 2/19/09 07:09 AM

Saza LIGHT LEVEL 15

Sign-Up: 10/01/05

Posts: 1,903

At 8/11/07 05:42 PM, UnknownFear wrote:
At 7/2/07 08:11 PM, clixels wrote: gosh, I'm just gonna stick with as2, as3 is way to hard to understand. They say its supposed to be simpler, but it really isn't.
I feel the same way. To goin from

On(Release){
gotoAndPlay(2);
}

to

function onIntroClick(evt:MouseEvent):void {
gotoAndPlay(2);
}

intro_btn.addEventListener(MouseEvent.CL ICK, onIntroClick);

I am sorry, but the fuck is that more simpler that the AS2 button script? I find AS3 VERY confusing

Yeah but it's easier to write:

function onIntroClick(evt:MouseEvent):void {
gotoAndPlay(2);
}

intro_btn.addEventListener(MouseEvent.CL ICK, onIntroClick);
intro_btn2.addEventListener(MouseEvent.CL ICK, onIntroClick);

than

On(Release){
gotoAndPlay(2);
 }

On(Release){
gotoAndPlay(2);
 }

It's less code and you can re use functions. Sure you can do that in AS2 but whats the point for gotoAndPlay?

As3 is alot more organised in my opinion.


None

thestrikerz933

Reply To Post Reply & Quote

Posted at: 3/5/09 11:59 PM

thestrikerz933 DARK LEVEL 08

Sign-Up: 12/23/07

Posts: 5

when i try. i get this error:

Description: 1120: Access of undefined property intro_btn.
source: intro_btn.addEventListener(MouseEvent.CL ICK, onIntroClick);

What am i doing wrong?

BBS Signature

None

Johnny

Reply To Post Reply & Quote

Posted at: 3/6/09 12:04 AM

Johnny DARK LEVEL 21

Sign-Up: 04/17/04

Posts: 4,429

At 3/5/09 11:59 PM, thestrikerz933 wrote: when i try. i get this error:

Description: 1120: Access of undefined property intro_btn.
source: intro_btn.addEventListener(MouseEvent.CL ICK, onIntroClick);

What am i doing wrong?

It means that you either:

A) Don't have a button/clip named 'intro_btn' or
B) You have one named that, but you're not referencing correctly (IE, your scope is wrong)

All sites currently down. Deal with it. <3

BBS Signature

None

thestrikerz933

Reply To Post Reply & Quote

Posted at: 3/6/09 12:16 AM

thestrikerz933 DARK LEVEL 08

Sign-Up: 12/23/07

Posts: 5

I think it is probably my scope. How would i fix that?

Sorry im a noob at Flash

BBS Signature

None

Johnny

Reply To Post Reply & Quote

Posted at: 3/6/09 11:45 AM

Johnny DARK LEVEL 21

Sign-Up: 04/17/04

Posts: 4,429

At 3/6/09 12:16 AM, thestrikerz933 wrote: I think it is probably my scope. How would i fix that?

Sorry im a noob at Flash

You need to reference it in levels. For example, if its a movieClip, inside a movieClip, that's on the root timeline, you'd need to do..

_root.mc1.mc2.whateverYouWant to reference it.

So find out where it's located and reference it correctly. =)

All sites currently down. Deal with it. <3

BBS Signature

Crying

DarkFlock

Reply To Post Reply & Quote

Posted at: 3/6/09 01:22 PM

DarkFlock NEUTRAL LEVEL 03

Sign-Up: 03/05/09

Posts: 8

Wow.. i didn't even understand a thing you typed in, and what is As3? sorry i'm new here and i really wanted to make like a movie. are these the buttons that are like play now and scene selection and stuff? oh and is adobe flash work for making animations? i have anime studio 5 and i have no idea how to use it..... please help anybody?

hi guyz


None

DarkFlock

Reply To Post Reply & Quote

Posted at: 3/6/09 01:25 PM

DarkFlock NEUTRAL LEVEL 03

Sign-Up: 03/05/09

Posts: 8

At 3/6/09 12:16 AM, thestrikerz933 wrote: I think it is probably my scope. How would i fix that?

Sorry im a noob at Flash

i bet i'm more of a noob than you are, i don't even know what program to use!

hi guyz


None

Halosheep

Reply To Post Reply & Quote

Posted at: 8/4/09 05:00 PM

Halosheep NEUTRAL LEVEL 05

Sign-Up: 04/09/09

Posts: 333

At 7/2/07 08:16 PM, Flynny wrote:
At 7/2/07 08:11 PM, clixels wrote: as3 is way to hard to understand...
Its just as easy/hard to understand as AS2. Your just not used to it yet

I really hope this is true. AS2 just need on(press){gotoAndPlay(2)};
Hopefully it gets easier
:3

"If everyone in the world did things 75% right all the time, the world would be much better than it is right now, so i demand you change this C to an A+!" - Calvin and Hobbes


Questioning

camannawi

Reply To Post Reply & Quote

Posted at: 9/20/09 09:55 PM

camannawi NEUTRAL LEVEL 01

Sign-Up: 09/20/09

Posts: 1

i just make three buttons and put the script

stop()

function click1(evt:MouseEvent):void {
gotoAndPlay(5);
}

function click2(evt:MouseEvent):void {
gotoAndPlay(2);
}

function click3(evt:MouseEvent):void {
gotoAndPlay(10);
}

bconocenos.addEventListener(MouseEvent.C LICK, click1);
binicio.addEventListener(MouseEvent.CLIC K, click2);
bproductos.addEventListener(MouseEvent.C LICK, click3);

but when i click on a button twice it send me to the next frame. example:

i click on ''binicio" and send me to frame 2, -- thats what i want, but if i press it again, it send me to frame 5, it shouldn't do anything, because i'm already there.. i dont think this is normal jaja, but i dont know how to fix it.
hope someone can help.


All times are Eastern Standard Time (GMT -5) | Current Time: 08:41 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!