Groups | Blog | Home
all groups > flash actionscript > august 2004 >

flash actionscript : Movie Clip Button Basic ActionScript Question


karltraum
8/31/2004 6:01:39 PM
I'm trying to make a button that on mouseover will get larger (slowly, through
tweening), hold its larger state while the mouse is over the button, and then
slowly shrink back to normal when the mouse is moved out. I also want the
button to remain enlarged (and other things to happen) when its clicked; and
when other buttons are clicked, I want it to act as if there were a mouseout,
and slowly shrink.

I am an actionscript newbie (altho' I used to program C/C++, so I'm OK on
basic syntax). I thought a movieclip would be the most flexible way to handle
above-- I could be wrong. But I can't even get the first effect (the swelling,
holding, shrinking) to work properly. The clip swells, holds, but when I
mouseout, the button goes back to its "_up" state -- no slow shrinking. I can't
figure it out. Please see attached .FLA file. Thanks very much for any help!
And if you have any advice about how to manage the other things I need done to
the button, that would be most welcome!

I think that a large part of the problem is that the _over state disappears as
soon as I roll out -- so it can't play the rest of the script -- and I also
think that there may be a way to do this with "onRollOver" and "onRollOut"
handlers -- but I can't seem to figure out how to get this work -- and help
much appreciated.
jsebok
8/31/2004 6:45:46 PM
karltraum
9/1/2004 2:01:14 AM
That looks promising, from the SWF -- but the FLA refuses to open/be opened in
FLASH MX. Any clue as to what might be going wrong? It simply says "Failed
to open document" and then the file path.

Any help much appreciated -- I'm under time pressure -- Thanks
jsebok
9/1/2004 2:47:13 AM
Maybe it won't open since I use MX 2004 Pro. I wouldn't think that would cause
a problem but maybe it does.

Anyway, all I did was create a movie clip with the 1st frame as the starting
size and the 6th frame as the enlarged size. Motion or shape tween as needed
and attach the following code to the mc on the stage:

onClipEvent (enterFrame)
{
if (this.hitTest(_root._xmouse, _root._ymouse, true))
{
this.nextFrame();
}
else
{
this.prevFrame();
}
}

on (release)
{
//code
}

There are probably better or at least other ways of doing this but it might
get you by for now.
rlc5611
9/1/2004 3:25:14 AM
Yes MX 2004 files cannot be opened in MX. If you want to make a 2004 file MX
compatible, you need to use "save as", select the MX format and then ignore the
onslaught of predictions of doom and gloom in the ensuing Flash warning
messages. If you do this often, these warnings can be turned off in the
preferences.
karltraum
9/1/2004 3:34:04 AM
I'm afraid I'm getting really lost in the sauce here -- how do I attach the
above code, and where?

But also, is the script you sent me frame-based -- i.e on _whose_ enterFrame?

In any case, my great idea was to simply attach event handlers to the movie
clip instance (is there a way to attach handlers to the movieclip itself --
i.e. so it's a true object? -- I can only seem to attach them to instances) and
negotiate the MC timelline thusly, while ALSO relying on the clip being tracked
as a button to automagically route proper events to the "_up", "_down", etc
frames.

Attached is my sample -- in addition to helping me with your helpful code, can
you tell me why mine doesn't work? It's button 1 that should do the magic stuff.

Thanks very much!


jsebok
9/1/2004 5:45:42 AM
karltraum
9/1/2004 4:55:23 PM
Thanks -- the works fine. But a couple of questions --

1 -- your script keeps polling the mouse position and then advancing or moving
backwards the frame pointer in the "button" movie clip. So when it hits the
beginning or end of the movie clip's timeline it simply stops, right? And so
that's why it goes backwards ("deflates"), because at any point where the mouse
isn't over the bounding box it will try to movie the script pointer back, but
cannot anymore when it hits the beginning of the movie clip's timeline. Is
that correct?

If I had many such buttons, though, wouldn't that tax the system: each one
polling? Is there a way to do this where you have a movie clip acting as a
button that would respond to particular events, so that you could just "play"
particular parts of the timeline based on what event received?

2. Related to that, I notice that you use the movie clip simply as a movie
clip, not using it as a button object. Will that work just as well? I know
that there's a way to use a MC as a button and put in frames labelled "_up",
"_down", etc and these will automatically be called on the proper button
states. I thought this was the "proper" way to do it. Will a movie clip used
as a movie clip handle all situations where a button would be used?

3. Related to above: When I _did_ use the movie clip as a button, I couldn't
get the timeline to resond to my commands, though -- or better put, I was
unable to move the pointer to different parts of the timeline as I saw fit in
response to, for instance, the "_over" command (once the mouse button moved out
of the bounding box, my "_over" script stopped running, and it just reverted to
"_up" state -- i.e. there was no way to "deflate" the button.

3. All actions are related to instances of the MC on the stage. Is there a
way to attach actions to the root object -- so that multiple instances will all
have the same actions? (i.e. methods of the object -- as opposed to methods of
the instantiation of the object -- I don't know if that's clear: it just seemed
to me that if I created a movie clip "mySwellButton" I could have methods
assigned to it to do certain things _before_ I put it on any stage. Or no.

Thanks for you help and your example --

By the way -- is it worth upgrading to MX2004 from MX? -- Is there a big
difference?
jsebok
9/2/2004 3:29:47 AM
Sorry to take so long to respond but the day has been rather hectic getting
ready to leave town for a few. I'll try and answer your questions but I am
fairly new to Flash development so keep that in mind. Also, If anyone else
reads this and sees an error in what I am saying, PLEASE correct me. Just be
gentle, hehe.

1. The 1st and last frame act as stops in that there is no 'next frame' but
the script keeps trying to 'get there'. Sorta like walking in place when you
hit a brick wall. There is no automatic reversal, you would have to specify
that.
As for taxing the system, objects on the stage are passive until an event
happens hence the usage of event listeners. There really is no difference
between doing this versus having a button component on stage that generates say
a click event.

2. Many people will only use movie clips as buttons since they can create
more elaborate effects. This is possible since the same events that a button
can broadcast, movie clips can as well. I would bet that most advanced button
effects are not created using the button component other than in cases where
movie clips are used within the button component timeline (i.e. mc for up,
over, down).

3. There are a lot of tutorials on various sites that explain more advanced
buttons/menus, etc. I would suggest searching around places like flashkit.com,
flashmx2004.com/forums and obviously here to find some of these things. Read,
follow links and get ideas. That is really the best way to go.

4. You can certainly build your own component and adjust properties via
methods or through the property inspector. I would think there are tutorials
out there that explain how to do that but I am not sure. FYI, in the Jeanette
Stallons Training from the Source book, Application Development, there is a
section on doing this.

5. MX2004 Pro is the only one I have ever had so I can't really answer the
upgrade question. What I can say is that one of the things that is new to this
version is forms development that along with AS 2.0 moves flash in the
direction of OO development. I would think MM has a list of new features that
you could look at to help decide for yourself.

Hopefully this helps a little and as I said, if anyone finds something wrong,
please correct me.
Sson
9/2/2004 6:14:16 AM
sry, just found that my post have problem...
AddThis Social Bookmark Button