00:00
00:00

AS3: Global buttons

5,976 Views | 20 Replies
New Topic

AS3: Global buttons 2007-05-04 11:53:08


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!

Response to AS3: Global buttons 2007-05-04 11:55:13


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?)

Response to AS3: Global buttons 2007-05-04 11:56:55


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!

Response to AS3: Global buttons 2007-05-04 12:03:40


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

Response to AS3: Global buttons 2007-05-04 12:04:45


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

Response to AS3: Global buttons 2007-05-04 12:12:38


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!

Response to AS3: Global buttons 2007-05-04 12:14:21


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.

Response to AS3: Global buttons 2007-05-04 12:16:31


Nice to know that!

Response to AS3: Global buttons 2007-05-04 12:57:11


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.


Bla

Response to AS3: Global buttons 2007-05-04 13:40:56


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!

Response to AS3: Global buttons 2007-05-05 03:36:44


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

Response to AS3: Global buttons 2007-05-05 04:55:04


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.

Response to AS3: Global buttons 2007-05-05 06:21:48


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.

Response to AS3: Global buttons 2007-05-05 09:18:58


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

Response to AS3: Global buttons 2007-05-05 14:59:37


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

Response to AS3: Global buttons 2007-09-17 23:48:33


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.

Response to AS3: Global buttons 2008-07-27 03:55:11


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


JAYE 19 | Flash | Advertise Here |

BBS Signature

Response to AS3: Global buttons 2010-02-05 12:57:00


At 9/17/07 11:48 PM, Zawmby wrote:
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.

I'm really new to this language too, but looking at the example that would be the function.

You'd need a seprate block of code for the function, something like
function questionmarklol(event:MouseEvent):void {
what you want to happen here
}

Response to AS3: Global buttons 2010-02-05 13:07:32


At 9/17/07 11:48 PM, Zawmby wrote: getURL()

navigateToURL (new URLRequest ("http://www.sitename.come"))


AS2||AS3||Motox

Thanks to hdxmike for the sig :]

BBS Signature

Response to AS3: Global buttons 2010-02-06 00:10:10


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.
Adding eventListeners to the stage seem somewhat odd to me... they should be on a given child. ALWAYS.

eventListeners shouldn't always be on a child, for example, adding keyboard listeners to the stage is common practice and makes the most sense for global keyboard control. Also, how is FlexBuilder "really" scripting the language, any more than Flash? It's the same language.


BBS Signature

Response to AS3: Global buttons 2010-02-06 00:22:46


Why are you all replying to posts from three years ago? :P