Tut Request: Buttons in ActionScript3.0

A place for tutorials on how to get the most out of Flash

Tut Request: Buttons in ActionScript3.0

Postby IslandArrogance » Fri Jun 03, 2011 4:12 am

As a new user to flash I'm finding it difficult to get my buttons to work correctly.

For example: putting...
Code: Select All Code
Button1.addEventListener(MouseEvent.CLICK, button1Click);

function button1Click(event:MouseEvent):void{
   gotoAndPlay("Scene 1", 1);
}

on the "actionscript" Layer in the first frame
with
Code: Select All Code
gotoAndPlay("Scene 1", 1);

at the end, to loop the animation.

This works. When I press the button it goes to frame one and continues to loop.

But. Adding in ...
Code: Select All Code
Button2.addEventListener(MouseEvent.CLICK, button2Click);

function button2Click(event:MouseEvent):void{
   gotoAndPlay("Scene 1", 35);
}

to the beginning of the new animated section (also on a new scene entirely [with altered gotoAndPlay(...) to match] and I tried putting the second button's code on various frames)
also with looping script in the last frame.

causes all buttons to be disabled and the playhead to just run. (almost like adding the second bit of code causes ALL action script to be disabled.) Also I'm running CS3, AS3.0



Now that's sort of a long detour to the subject, but I'm just providing an example.

I'm requesting for an AS3.0 button tutorial, any help would be appreciated.

I guess I am also calling out to anyone who knows AS3.0 to bring forth their skills and post whatever they can.

There's those of us who are new that could use whatever little tips or advice anyone provides, and for those more experienced, I'm sure you could learn different techniques as well.


*EDIT: I fixed my problem from above, by just adding
Code: Select All Code
Button2.addEventListener(MouseEvent.CLICK, button2Click);
to the first frame.

So there's a bit of teaching for you. :)
IslandArrogance
Newly Registered
 
Joined: Thu Jun 02, 2011 6:15 am

Re: Tut Request: Buttons in ActionScript3.0

Postby Thraur » Thu Jun 09, 2011 1:12 am

IMHO, The easiest way to do this would be to make your button a class and avoid sticking AS3 code on the timeline outside of timeline control functions like stop, gotoAndPlay, etc. I'm a fan of keeping as much AS3 code outside of library objects and as external AS3 files instead. If you've done programming outside of flash, it will make the most sense this way.

Perhaps not what your looking for, but this is how I organized my AS3 code:
- I have a buttons package. (your scripts folder\buttons\)

- I made myself a generic "ClickableButton" class (buttons\ClickableButton.as) that can be extended for all buttons in the buttons folder:

Code: Select All Code
package buttons {
   
   import flash.display.SimpleButton;
   import flash.events.*;
   
   /* A simple clickable button class that can be extended. Simply override clicked function which is called on mouse click. */
   public class ClickableButton extends SimpleButton {
      
      public function ClickableButton() {
         addEventListener(MouseEvent.CLICK, clicked, false, 0, true); // Weak reference should allow object to be GC'd
      }
      
      // Override this function to do whatever needs to be done on clicking
      protected function clicked(e:Event):void {}
   }
}


In your library where your button is, mark as export for actionscript and set the class to buttons.MyObjectsName. Then you can create another class like below (also in buttons package - buttons\MyObjectsName.as):

Code: Select All Code
package buttons {
   import flash.events.Event;
   public class MyObjectsName extends ClickableButton {
      public function MyObjectsName() {}
      protected override function clicked(e:Event):void {
         // Do stuff
      }
   }
}


For any other buttons, you would only have to extend ClickableButton and make them different classes.

If you aren't doing anything with the click event, you could take it further and make the generic class call a function with no params, then you wouldn't even need to have the import flash.events.Event in the subclass.
User avatar
Thraur
 
Joined: Thu Mar 17, 2011 4:11 am

Re: Tut Request: Buttons in ActionScript3.0

Postby IslandArrogance » Tue Jul 12, 2011 8:51 am

I have returned!

Apologies for the late reply, but I have worked on and completed my first game, simple? yes, public? no, but
this
Code: Select All Code
import flash.events.MouseEvent;

zerobutton.addEventListener(MouseEvent.CLICK, zeroClick);

function zeroClick(event:MouseEvent):void{
   gotoAndPlay("action1")
}

firstbutton.addEventListener(MouseEvent.CLICK, firstClick);

secondbutton.addEventListener(MouseEvent.CLICK, secondClick);

thirdbutton.addEventListener(MouseEvent.CLICK, thirdClick);

fourthbutton.addEventListener(MouseEvent.CLICK, fourthClick);


and several of these

Code: Select All Code
function secondClick(event:MouseEvent):void{
   gotoAndPlay("action2")
}


and these

Code: Select All Code
gotoAndPlay("action2");


Are what's making my stuff work. (Also I'm using CS3)

I have each button on an individual layer, and in a folder. with 2 keyframes per layer (#1 and #550 something, start and finish)

then various animations spread out along that. (Like from frame 1 to 20 there's an animation with a loop command at the end, all the buttons are there. Now also from frame 22 to 40 there's another animation with the buttons [and so forth] {and a bunch of layers for all the gibbly bits and such} it's pretty sequential.)

The Swf file is 222kb and the fla file is 3,609kb. (After "save and compress")

Again, I'm very new at this, so I'm not sure if that's good or what? seems okay but IDK

Also, I have no clue what you're talking about. :(

Please bear with me I'm teaching myself and learning as I go, which is partially why I posted my AS3 stuff up there, but what do you mean by making it a class? what exactly does that do? and which of your posted code does what exactly? :/
IslandArrogance
Newly Registered
 
Joined: Thu Jun 02, 2011 6:15 am


Return to Tutorials



Who is online

Users browsing this forum: No registered users