Moving From Flash AS2 To Flash AS3: MovieClip Buttons In MovieClip Classes
(Note: I'm converting my Flash game "Spin City" from AS2 to AS3 so it can be re-skinned for another project. I've decided to document this activity in a series of tutorials so they might benefit anyone else who is going through the same development process)
OK, this one drove me crazy today, but I finally discovered the answer. I try as hard as possible to keep all actions related to my Flash code in my classes and not place any code on the time line. this includes buttons and their related code for handling events. I got it to work in AS3, but I could not get the hand cursor to show-up when hovering over buttons. Here is what I found out.
| Flash AS2 |
In AS2, handling button actions in a class that is associated with a MovieClip that contained that button (i.e. A TitleScreen class that includes a [PLAY] button) was done like this:
- Define a variable for the button in your class so you can set its properties. The name must be the same name as you named the button in your MovieClip. Even though you never define the variable, at run-time it will be associated with the button because they have the same name.
- Set the onRelease handler of the button to a function in your class
- Have the button handler call _parent[function] so that you can operate within the scope of your class (and not the button) (Note: You can eliminate this by using a delegate)
- Perform action
| [cc lang="javascript" width="550"] class TitleScreen extends MovieClip { var button_play:MovieClip; function TitleScreen() { function play_button_release() { [/cc] |
AS2 considers the MovieClip defined by button_play as a "button" because we have assigned a call-back function to its onRelease handler. This means that when hovering over the button, the cursor will change to the "hand" that instantly informs the user that this is a button than can be pressed.
| Flash AS3 |
In AS3, there are few more steps you have to take to simulate the same functionality. In some ways it is much simpler than AS2, and in some ways, a bit more involved. At any rate, it is different than AS2, and that is why I'm covering it in this blog. Here are the steps you need to perform to make it work:
Easier:
AS3 is easier for two reasons:
- The event handler you assign to your button is called in the context of where that function exists. This means you no longer have to call _parent[function] or use a delegate to get back into the context of your class.
- You do not have to define a MovieClip that exists in the MovieClip associated with your class as a variable in your class. These MovieClips are already defined for you.
However, there are a couple complications with AS3:
- There are no longer onRelease and onPress events to handle. You use MouseEvent.CLICK, MouseEvent.MOUSE_DOWN, MouseEvent.MOUSE_UP and MouseEvent.DOUBLE_CLICK (among others) for this purpose.
- You need to set the useHandCursor property to true so the cute little useful hand will show-up when the cursor hovers over the button.
- You need to set the buttonMode property to true so the MovieClip that defines the button will act like one (i.e. the useHandCursor property will actually have an effect)
Here is what the code looks like:
| [cc lang="javascript" width="550"] package fashiondash { import flash.events.MouseEvent; public class TitleScreen extends flash.display.MovieClip { public function TitleScreen() { button_play.addEventListener(MouseEvent.CLICK, playagainButtonRelease); button_play.useHandCursor = true; button_play.buttonMode = true; } function playagainButtonRelease(e:MouseEvent) { } } |
So there goes another short lesson. I had no idea that little things I took for granted such as the hand cursor would pose problems like this in AS3. There certainly is a lot to re-learn with this generation of ActionScript.
Pingback: Moving From AS2 to AS3: A Handy List of Resources | Flash Speaks Actionscript
Pingback: Moving From AS2 to AS3: A Handy List of Resources « The Joe Lake Blog – joeylakey.com
Pingback: Bathroom Remodeling Philadelphia PA
Pingback: comment jailbreak iphone 4s
Pingback: best supplements for women
Pingback: fast cash advance
Pingback: Pussy
Pingback: czekoladowe fontanny
Pingback: Bungalow Ile Maurice
Pingback: free online games for girls
Pingback: http://thebestelectroniccigarette.org
Pingback: calgary commerical photographers
Pingback: cheap college textbooks
Pingback: czekoladowe fontanny
Pingback: Bridgeport village
Pingback: Paintball
Pingback: Hypnotherapy Surrey
Pingback: Dental Hygienist Schools in georgia
Pingback: network marketing
Pingback: unlock screen iphone 4
Pingback: fat burning workouts for men
Pingback: lose weight
Pingback: best stocks
Pingback: cure cancer
Pingback: payday loans
Pingback: Linfa y tratamientos
Pingback: african mango plus
Pingback: speech for wedding
Pingback: war games
Pingback: swissdent
Pingback: Location Villa Maurice
Pingback: safe cobs
Pingback: cheap honeymoon
Pingback: Denver Granite Countertops
Pingback: BBQ Side Dishes
Pingback: Increase datpiff views and downloads
Pingback: Juvederm Denver
Pingback: Interior Designers Denver
Pingback: sbo
Pingback: Smart Battery Charger
Pingback: norfolk escorts
Pingback: dildos
Pingback: asianbeauties
Pingback: chiropractic center lakeland
Pingback: elmahdy
Pingback: Keyword Tool
Pingback: juegos para nokia c3
Pingback: Sex webcam
Pingback: sex-gifs.net
Pingback: paper shredding houston
Pingback: knullkontakt
Pingback: lawyers
Pingback: macho alfa
Pingback: flatten stomach exercises
Pingback: roulette system
Pingback: bail bonds San Bernardino
Pingback: Kitchen Remodeling New York
Pingback: auto repair rochester ny
Pingback: rayban outlet
Pingback: jailbreak iphone 4 ios
Pingback: iphone unlocked
Pingback: The Best E-Business And E-Marketing Products In The Market
Pingback: british passport
Pingback: this site
Pingback: Awesome post! Hey, make sure you check out how I lost over 70 pounds:
Pingback: reduce jaw pain
Pingback: new homes for sale
Pingback: what is the quickest way to lose weight
Pingback: can jailbreak iphone 4