Forum Topic: AS3: Global buttons

(1,955 views • 16 replies)

This topic is 1 page long.

<< < > >>
None

ninjitsuwarrior

Reply To Post Reply & Quote

Posted at: 5/4/07 11:53 AM

ninjitsuwarrior FAB LEVEL 24

Sign-Up: 04/28/06

Posts: 1,088

AS3: Main, for updated people!

Here is a tutorial about global buttons, because everyone is complaining about that you can't add code's inside the button!

1. We start with a new Flash file with AS3:
2. Press F9 on the main stage to open the actions window.
3. Add this line to the actions window:

stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse);

This creates and listener (in as2 an clipevent) and it gives a mouse down function and the property mouse.

4. Under the previous line add this one:

function mouse(event:MouseEvent):void {
nextframe();
}

This triggers the mouse function and then it goes to the next frame.

5. Close the actions window.
6. Right click in the timeline on the second frame.
7. Select insert keyframe and draw something.
8. If you press ctrl enter and you press once you can see it going to the drawing!

Here are some things you can add here:

stage.addEventListener(MouseEvent.MOUSE_DOWN, mouse);

On the thick word you can add these functions:
CLICK if you click
DOUBLE_CLICK if you click twice
FOCUS_IN when you highlight it
FOCUS_OUT when you highlight another thing
KEY_DOWN when you press a key
KEY_FOCUS_CHANGE when you press another key
KEY_UP when you release a key
MOUSE_DOWN when you press the mouse
MOUSE_FOCUS_CHANGE when you focus on a other object
MOUSE_MOVE when you move the mouse
MOUSE_OUT when you move your mouse away
MOUSE_OVER when you roll over the object
MOUSE_UP when you release the mouse
MOUSE_WHEEL when you roll with the mouse wheel
ROLL_OUT when you move away
ROLL_OVER when you move over it
TAB_CHILDREN_CHANGE when the tab children flag change
TAB_ENABLED_CHANGE when the tab enabled flag change
TAB_INDEX_CHANGE when the tab index flag change

I hope it helps!
I still need to find out how the normal buttons work!


None

fuzz

Reply To Post Reply & Quote

Posted at: 5/4/07 11:55 AM

fuzz NEUTRAL LEVEL 21

Sign-Up: 06/02/04

Posts: 5,647

CLICK if you click
DOUBLE_CLICK if you click twice

Those would be pretty usefull for window simulators
(the click one is obvious, but double click.. was that possible in AS2?)


None

ninjitsuwarrior

Reply To Post Reply & Quote

Posted at: 5/4/07 11:56 AM

ninjitsuwarrior FAB LEVEL 24

Sign-Up: 04/28/06

Posts: 1,088

At 5/4/07 11:55 AM, fuzz wrote:
CLICK if you click
DOUBLE_CLICK if you click twice
Those would be pretty usefull for window simulators
(the click one is obvious, but double click.. was that possible in AS2?)

It was possible with an very large code!


None

Denvish

Reply To Post Reply & Quote

Posted at: 5/4/07 12:03 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

At 5/4/07 11:56 AM, ninjitsuwarrior wrote: It was possible with an very large code!

Not really that big

mclicks=0;
onMouseDown=function(){
mclicks++;
if(mclicks>1){ trace("Doubleclicked");}
sint=setTimeout(function() {mclicks=0;},200);
}

- - Flash - Music - Images - -

BBS Signature

None

jmtb02

Reply To Post Reply & Quote

Posted at: 5/4/07 12:04 PM

jmtb02 LIGHT LEVEL 29

Sign-Up: 03/01/04

Posts: 5,842

At 5/4/07 11:53 AM, ninjitsuwarrior wrote: CLICK if you click
DOUBLE_CLICK if you click twice

A few questions:

Is a double click actually counted as a click?

What determines the duration for which Flash will consider two clicks as a double click.

And hooray for scroll wheel!

Hi there!

BBS Signature

None

ninjitsuwarrior

Reply To Post Reply & Quote

Posted at: 5/4/07 12:12 PM

ninjitsuwarrior FAB LEVEL 24

Sign-Up: 04/28/06

Posts: 1,088

At 5/4/07 12:04 PM, jmtb02 wrote:
At 5/4/07 11:53 AM, ninjitsuwarrior wrote: CLICK if you click
DOUBLE_CLICK if you click twice
A few questions:

Is a double click actually counted as a click?

yes

What determines the duration for which Flash will consider two clicks as a double click.

don't know

And hooray for scroll wheel!

hooray!


None

trig1

Reply To Post Reply & Quote

Posted at: 5/4/07 12:14 PM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,108

At 5/4/07 12:04 PM, jmtb02 wrote: What determines the duration for which Flash will consider two clicks as a double click?

I assume the proportion thats set on your system - you can change it using the mouse bit of the control panel.

BBS Signature

None

ninjitsuwarrior

Reply To Post Reply & Quote

Posted at: 5/4/07 12:16 PM

ninjitsuwarrior FAB LEVEL 24

Sign-Up: 04/28/06

Posts: 1,088

Nice to know that!


None

Alphabit

Reply To Post Reply & Quote

Posted at: 5/4/07 12:57 PM

Alphabit NEUTRAL LEVEL 09

Sign-Up: 02/14/06

Posts: 4,066

At 5/4/07 12:04 PM, jmtb02 wrote: What determines the duration for which Flash will consider two clicks as a double click.

You need to set the DisplayObject's "doubleClickEnabled" poperty to true... He might have forgotten to mention that.


None

Jindo

Reply To Post Reply & Quote

Posted at: 5/4/07 01:40 PM

Jindo LIGHT LEVEL 22

Sign-Up: 08/30/05

Posts: 1,789

I dunno why, but sometimes, buttons I made in Flash 8 had to be double-clicked before they took you to the next frame.

But an actual double-clicking event, that's awesome :D!

:D


None

Eder

Reply To Post Reply & Quote

Posted at: 5/5/07 03:36 AM

Eder EVIL LEVEL 20

Sign-Up: 11/11/04

Posts: 72

The scroll whell was in AS2, there was a var called delta that had a value dependin on the times you scrolled.

But that's not the point, the point is Now how can we check when a specific key is down? like Key.isDown(Key.UP)

BBS Signature

None

trig1

Reply To Post Reply & Quote

Posted at: 5/5/07 04:55 AM

trig1 NEUTRAL LEVEL 15

Sign-Up: 10/04/05

Posts: 2,108

At 5/5/07 03:36 AM, Eder wrote: But that's not the point, the point is Now how can we check when a specific key is down? like Key.isDown(Key.UP)

They thought Key.isDown was to useful to put in AS3.

BBS Signature

None

Sid1120

Reply To Post Reply & Quote

Posted at: 5/5/07 06:21 AM

Sid1120 EVIL LEVEL 15

Sign-Up: 06/13/04

Posts: 81

You guys seem to work in a very weird fashion...
I'd recommend trying out FlexBuilder 2.0 for 30days to get how to REALLY script the language.

Buttons are usually made by:

var anything:DisplayObject = new DisplayObject(); // By DisplayObject, I mean just about anything, from a Shape to DisplayObject, passing by Sprite, MC & SimpleButton
anything.addEventListener(MouseEvent.CLICK, onClick01); // Adds the eventListener
addChild(anything); // This will actually put the new var on the stage... useful, huh?

private function onClick01(event:MouseEvent):void {
this.x += Math.random()*10 - 5 // You get the idea: you can place whatever you want here.
}

******************************
The usefull thing with AS3 is that you can add Events to ANYTHING very easily. Events, MouseEvents, KeyboardEvents, CustomEvents, FunnyEvents, CrappyEvents, NAME IT! It's so freaking easy that it's scary...

Too lazy for examples on how to create custom events

Adding eventListeners to the stage seem somewhat odd to me... they should be on a given child. ALWAYS.


None

Denvish

Reply To Post Reply & Quote

Posted at: 5/5/07 09:18 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

At 5/5/07 06:21 AM, Sid1120 wrote: You guys seem to work in a very weird fashion...
I'd recommend trying out FlexBuilder 2.0 for 30days to get how to REALLY script the language.

From what I've read about FlexBuilder, it's a good app, but it's more of a hardcore coder program, used for developing applications and web interfaces. What you have to bear in mind is that the vast majority of people who come to this forum aren't hardcore coders - they're mostly kids who want to make a game or a movie, for which Flash is the appropriate tool. I'm not criticising Flex or FlexBuilder, but it and Flash are two different beasts - and let's face it, NG is a Flash-based website.

Which is also the reason why people tend to default to 'Stage', the stage having always been the centrepoint of the Flash authoring environment, whether in animating or Actionscripting.

- - Flash - Music - Images - -

BBS Signature

None

Eder

Reply To Post Reply & Quote

Posted at: 5/5/07 02:59 PM

Eder EVIL LEVEL 20

Sign-Up: 11/11/04

Posts: 72

I'm still trying to get the hang of As3, and what I've seen is that those clic, mouseOver, scrollWheel, etc aren't functions. They are values that the listener modifies.

For example if you have a keyboard listener (i dont remember what was it called XD), with the KEY_DOWN var, and the spacebar is down, KEY_DOWN will be 32

BBS Signature

None

Zawmby

Reply To Post Reply & Quote

Posted at: 9/17/07 11:48 PM

Zawmby EVIL LEVEL 31

Sign-Up: 09/29/05

Posts: 18

Ok Im still having trouble with this, but I never took the time to learn actionscript as a programming language. Like it was stated before, Im not a coder I am just trying to create a button that when clicked will do the following commands:
gotoandplay(frame #)
gotoandstop(#)
play()
stop()
getURL()
etc.....
the farthest I have gotten is using the instance name of my button symbol "lamp"
lamp.addeventlistener(MouseEvent.MOUSE_U P,?)
I dont know what goes in the place of that question mark and I dont know what goes after to perform the action.


Elated

Jaye19

Reply To Post Reply & Quote

Posted at: 7/27/08 03:55 AM

Jaye19 LIGHT LEVEL 13

Sign-Up: 04/07/08

Posts: 606

Wow man this is a nice tut it really herd to AS3 it's far different from AS2 and last thing in you lines of code the nextframe(); thing is wrong you forgot the f to be capital

Godia meia of ultimia omega andio alpha areia javka

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 04:48 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!