Groups | Blog | Home
all groups > flash actionscript > june 2006 >

flash actionscript : onPress vs onMouseDown?


David Stiller
6/2/2006 12:12:21 PM
flasheister,

[quoted text, click to view]

Start thinking of events in terms of the classes they belong to. There
are at least two onMouseDown events in ActionScript: one belongs to the
Mouse class and another belongs to the MovieClip class. Presumably, you're
using the MovieClip.onMouseDown event. So what does this event refer to?
It refers to the mouse being clicked. Not being clicked *on* the movie
clip, necessarily -- just *being clicked*. So if several movie clips have a
function assigned to their onMouseDown event, then yes, they'll all fire at
once.

[quoted text, click to view]

That's because MovieClip.onPress (note, there's also a Button.onPress)
only fires when the mouse is clicked *on* the object in question.


David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."

David Stiller
6/2/2006 2:34:00 PM
flasheister,

[quoted text, click to view]

Sure.

[quoted text, click to view]

Here's a blog entry on that.

http://www.quip.net/blog/2006/flash/event-handlers-listeners

In summary: some objects are written to expect event handlers and some
are written to expect listeners. You use handlers for the one kind and
listeners for the other. ;) The quickest way I can think of to tell which
is which is to learch the term "addListener" in the ActionScript 2.0
Language Reference. You'll get an awful lot of search results to sift
through, but you'll eventually see all the classes that expect this sort of
event handling.

[quoted text, click to view]

That's just the thing, flasheister, you're not "just attaching them to
the Stage." When you use those events as stand-alone actions, you're
actually assigning them to a movie clip -- because the main timeline *is* a
movie clip. Since that's what you're doing anyway, you may as well use the
full syntax. Easier to understand, and you'll be able to "see" what you're
doing clearer. My opinion, anyway.


David
stiller (at) quip (dot) net
Dev essays: http://www.quip.net/blog/
"Luck is the residue of good design."

flasheister
6/2/2006 3:58:57 PM
I was making multiple dynamic movieclips and trying to assing listeners and
onMouseDown functions but couldnt get it to work...it fired all of them at once.

So I used the onPress instead and everything works fine.

What is the diff between these two technically? When should I use one vs the
other?


2m
6/2/2006 4:22:20 PM
It is quite simple and not even illogical.

onPress is fires, when you "press" the clip it is assicioated with - when he
pointer hovers over the clip other wise you wouldnt press it.

on MouseDown fires whenever the mouse(-button) is down(ed), regardless where
your pointer is positioned.
flasheister
6/2/2006 5:54:26 PM
that makes sense....so even if I use the movieclip.onmousedown that would still
fire if I clicked anywhere on the screen right?

if i understand correctly if i want an event to happen only when I click a
particular MC then onPress is the way to go.
flasheister
6/2/2006 6:11:20 PM
Just one more question on that topic...if I use the onPress, i just assign it
to the MC like this.onPress = function () etc...so i can do something when it
is pressed.

Should or in what situation should I use listeners rather that the just
assigning an onPress event directly to the MC Seems to just assinging events
like onPress, onRollOver, etc. is easiest to just assign them when I attach
them to the stage.
AddThis Social Bookmark Button