Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Buttons in AS3??????

Explorer ,
Jan 06, 2009 Jan 06, 2009
I used to use a script like this in a frame on the timeline to add functionality to my buttons.

btn_01.onRelease = function(){
_root.targ_Main.loadMovie("oranythingelseineededthebuttontodo");
};

Now in AS3 I have no clue!!!!!!!!!!

And apparently you have to be some sort of uber programmer just to read the documentation. Can anyone tell me how to make a simple button work?
TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 06, 2009 Jan 06, 2009
function btn01Clicked(evt:MouseEvent):void{
// actionscript for what clicking the btn does
}

btn_01.addEventListener(MouseEvent.CLICK, btn01Clicked);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2009 Jan 06, 2009
I don't understand it yet but I'm trying it. Thanks!

Apparently I can't load a external swf into a target any longer so I'll have to get that worked out somehow as well.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 06, 2009 Jan 06, 2009
You're welcome.

Look into the URLLoader and addChild() methods for the other things you mention.

There's quite a bit that has changed in AS3. There's a tabulation of sorts if you look up Migration in the AS3 help docs.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 06, 2009 Jan 06, 2009
leodsmith,

> Now in AS3 I have no clue!!!!!!!!!!

Yes, things have changed, but keep in mind, you don't *have* to use AS3.
Your publish settings (File > Publish Settings) let you configure any FLA
for ActionScript 3.0, 2.0, 1.0, and even older versions (precursors, really)
of the language.

> And apparently you have to be some sort of uber
> programmer just to read the documentation.

I realize it can feel that way, but there's a trick to figuring out the
documentation, and it call comes down to something called classes. In a
nutshell, classes are like recipes, or blueprints, for the objects you now
and use every day in Flash. If you're using a button symbol, you're
actually dealing with a button object; that is, an instance of the Button
class in AS2 or the SimpleButton class in AS3. If you're using a movie
clip, you're dealing with an instance of the MovieClip class, regardless of
the version of AS. Text fields are defined by the TextField class. Sounds
are defined by the Sound class, and so on.

ActionScript 3.0 has a larger API (more classes) than previous versions,
but it's structured the same way. For example, AS3 provides a set of
companion classes for audio: Sound, SoundChannel, SoundTransform, and
SoundMixer. AS2 only provides a Sound class. On the face of it, that may
seem easier because it's less to deal with ... but every coin has two sides.
In AS2, the concept of a sound channel is present (this is the thing that
lets you differentiate between different "sound banks"), but there is no
SoundChannel class. Instead, AS2 associates Sound instances with a
particular timeline (that is, with a movie clip) ... so even if AS2 is more
familiar, it isn't always as intuitive as it should be.

Okay, back to classes. So classes define objects, and as a general
rule, classes are categorized into three headers: Properties, Methods, and
Events. Sometimes you'll see a few more, but those three are the main ones.
Properties refer to an object's characteristics (width, height, number of
letters, volume, etc.). Methods refer to things the object can do
(gotoAndPlay(), setTextFormat(), attachAudio(), etc.). Finally, events
refer to things the object can react to (mouse ups, the end of an audio
file, the passing of a timeline frame, and so on.)

So if you're looking up something about buttons, try the SimpleButton
class in AS3, or the Button class in AS2 (usually, the class names are the
same for both versions; this happens to be an oddball). Look up events, to
see what sort of "can react to" opportunities exist for buttons, then check
out the sample code. Granted, that may be easier said than done -- new
stuff is always bewildering until you get familiar with it -- but I hope
that at least helps you get your bearings. Be aware that most classes
inherit functionality from each other (just like genetics in humans), so
when you see hyperlinks in the documentatoin for showing inherited
properties, methods, or events, be sure to click them!

In this case, as NedWebs correctly pointed out, you're going to use the
addEventListener() method to associate a particular MouseEvent with a custom
function. In principle, this is the same way it happens in AS2 (unless
you're attaching code directly, which AS3 doesn't support).

AS2:
btn_01.onRelease = function() {
// something to do
};

Here, you're using an anonymous function (meaning, the function isn't
named). You could also do this:

btn_01.onRelease = clickHandler;
function clickHandler() {
// something to do
};

... which, on a certain level, is pretty much the same thing. Giving your
function a name is arguably better, becuase if you ever need to kill that
event handler, you can set it to null:

btn_01.onRelease = null;

... and then, if you ever need to re-enstate that event handler, you can
simply re-associate it with the same previously named (and already declared)
function:

btn_01.onRelease = clickHandler;

So in AS3, you're doing the same thing:

btn_01.addEventListener(
MouseEvent.CLICK,
function(evt:MouseEvent) {
// something to do
}
);

... or, for more control, use a named function:

btn_01.addEventListener(MouseEvent.CLICK, clickHandler);
function clickHandler(evt:MouseEvent) {
// something to do
};

See the similarities? In AS3, the event actually sends itself to the
receiving function, which is where there's a parameter inside the function
(I used "evt", but you can use whatever variable name makes sense for you).
This parameter is typed as an instance of the MouseEvent class because, as
you can imagine, it's an instance of the MouseEvent class. Like any other
class, MouseEvent has properties (listed in the MouseEvent class), and one
of those properties lets you refer back to the object that dispatched the
event in the first place.

It's all in there; you just have to dig a bit and see what your options
are.

Here are some links that might give you a hand:

Colin Moock
http://www.insideria.com/2008/01/actionscript-30-is-it-hard-or.html
http://www.insideria.com/2008/07/the-charges-against-actionscri.html

One of my blog entries ...
http://www.quip.net/blog/2007/flash/making-buttons-work-in-flash-cs3
(written for Flash CS3, but it's about AS3, so it still applies)

Obviously, these Web references are free, but you might also get
something out of an AS2-to-AS3 migration book I recently co-authored for
O'Reilly:

The ActionScript 3.0 Quick Reference Guide
http://tinyurl.com/2s28a5
(only two Amazon reviews so far, but they're both 5 out of 5)


David Stiller
Co-author, Foundation Flash CS4 for Designers
http://tinyurl.com/5j55cv
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2009 Jan 06, 2009
I haven't had time to read thouroughly the last response but thank you both for your input.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 06, 2009 Jan 06, 2009
David. I'm only part way through your comments but can see this will be extremely helpful. Thanks again for such an effort to help.

Now it's my job do do some hard work learning.

Owe you one!!!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 06, 2009 Jan 06, 2009
leodsmith,

> David. I'm only part way through your comments but can see this
> will be extremely helpful. Thanks again for such an effort to help.

Glad to hear that! :)

> Now it's my job do do some hard work learning.

Honestly, the hard work pays off. I'm self-taught when it comes to
Flash, and I don't come from a programming background (e.g., no computer
science degree). We all have different aptitudes, prior experience, and
learning styles, so all you can really go by is your own pace. As long as
you feel encouraged, you're getting somewhere. 🙂 Sometimes it just helps
to hear from others where you might try giving it a start.

Good luck!


David Stiller
Adobe Community Expert
Dev blog, http://www.quip.net/blog/
"Luck is the residue of good design."


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2009 Jan 14, 2009
I'm using this now but can't get the swf to unload,. can't seem to get right way to use remove.Child.

stop ()


btn_Plan.addEventListener(MouseEvent.CLICK,
function(evt:MouseEvent) {
var myLoader:Loader = new Loader(); targ_Main.addChild(myLoader); var url:URLRequest = new URLRequest("loads/Plan.swf"); myLoader.load(url);
}
);

btn_Photos.addEventListener(MouseEvent.CLICK,
function(evt:MouseEvent) {
var myLoader:Loader = new Loader(); targ_Main.addChild(myLoader); var url:URLRequest = new URLRequest("loads/Photos.swf"); myLoader.load(url);
}
);

btn_Video.addEventListener(MouseEvent.CLICK,
function(evt:MouseEvent) {
var myLoader:Loader = new Loader(); targ_Main.addChild(myLoader); var url:URLRequest = new URLRequest("loads/Video.swf"); myLoader.load(url);
}
);
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 14, 2009 Jan 14, 2009
Hi,

It's hard to tell what the problem might be without seeing the code to perform the unload...are you saying that the code you did post is working ok and loading the external movie clips as you wish?

On AS3 in general, I think that David Stiller's comment are right on the money. I got into my current position about a year ago and started using AS3 then. While I have a few years of experience with AS2, I was also initially bewildered by the AS3 structure and philosophy.

Now, however, I wouldn't go back to AS2 for any amount of money. The internal logic and consistency of AS3 is head and shoulders above AS2. It does take time to get the hang of it, but once you do you realize how much more programmatically powerful it is.

Good luck with your project, and please, post a more detailed code example for us to review! :)

Regards,
Dave Spaar
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2009 Jan 14, 2009
btn_Plan.addEventListener(MouseEvent.CLICK,
function(evt:MouseEvent) {
targ_Main.removeChild(myLoader);
var myLoader:Loader = new Loader(); targ_Main.addChild(myLoader); var url:URLRequest = new URLRequest("loads/Plan.swf"); myLoader.load(url);
}
);

I just figured it would be best to remove first.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2009 Jan 14, 2009
This is the code I would use in AS2 and can have the menu of buttons be it's own clip too.

stop ()

this.btn_Plan.onRelease = function(){
_parent.targ_Main.loadMovie("loads/Plan.swf");
};

this.btn_Photos.onRelease = function(){
_parent.targ_Main.loadMovie("loads/Photos.swf");
};

this.btn_Video.onRelease = function(){
_parent.targ_Main.loadMovie("loads/Video.swf");
};
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Jan 14, 2009 Jan 14, 2009
I don't see how you can put the removeChild call in the same function as the addChild call. Is myLoader defined earlier in your code so that it's a global variable? I mean, I do see it being instantiated for the load call, but I don't see how it would exist for the removeChild call.

Have you played with the examples in the help at all? I guess more importantly, what version of Flash are you using? CS3 or CS4? If so I would suggest that you should look at the examples for the Loader class in the help file, copy and paste them as described in that section, and play around a bit with the code...I've found that that, while time consuming, it also makes it easier in the long run to understand the classes I'm working with.

I still feel that I don't have a good sense of what you're attempting. Is the code you're posting in a frame, or is it in a document class associated with your movie, or someplace else? If you're not familiar with a document class, that's a whole different discussion, but it's relevant to your situation. Seriously, though, I can't overestimate the usefulness of actually playing with the examples, they give a really good and simple means of getting the hang of the basics of the object-oriented nature of AS3. Sorry I haven't been much help yet, but I'm willing to keep trying if you are. 🙂
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2009 Jan 14, 2009
LATEST
My objective is to have a menu that is a movie clip containing multiple buttons. The buttons load an external swf on the main stage. I place an empty symbol on the stage to use as a atrget which allows me to change the location of the loaded swfs and/or make them load at the same location on the stage. Anyway this is how I've done it for years and I'm way overdue to learn the new way. I've been away from Flash for a while.

I definately like to work with the help files it's just that now it seems so alien, I'm not even sure what to look for. Like the document class which I've not heard of.

Geez I have a long way to catch up.

Thank uyou for your patience and effort!!!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines