As much as I love ActionScript 3, and the huge improvement that is was over that of ActionScript 2, it still has some small downfalls. With the help of third-party frameworks, AS3 development can be drastically simplified. Using the CASA Lib framework for AS3 has simplified development on my AS3 projects significantly. This article contains the top uses of the CASA Lib framework.
Referencing the Stage
Problem:
Nothing is more annoying during development than being unable to reference the main stage in order to manipulate the display objects that are on the screen. In order to reference the stage, you must reference the “stage” property that is located only on the document root class. If one of your other classes needs to reference the stage property, it must do it through some type of association with your document root, or a parameter in your constructor which accepts the stage reference.
Solution:
Call
StageReference.setStage(this.stage)
at the beginning of your document root’s constructor. Then, whenever you need to access the stage, call
StageReference.getStage()
in any of your classes.
Listening to Event.ENTER_FRAME
Problem:
As you probably know, the Event.ENTER_FRAME is only dispatched on Display Objects in flash. That means that any other class, including classes that extend EventDispatcher cannot hook into an ENTER_FRAME event.
Solution:
Since you’ve already setup your StageReference
, all you have to do is call
EnterFrame.getInstance().addEventListener(Event.ENTER_FRAME, enterFrameHandler);
to start listening for ENTER_FRAME events on a non-display object.
Key Stroke Combinations
Problem:
When dealing with game development, we often find ourselves in situations where we want to detect various complex key combinations, such as a user pressing both “A” + “D” at the same time in order to trigger some in-game combo move.
Solution:
First, you have to define your key combination in terms of the ASCII codes that you are listening for.
var keyCombo:KeyCombo = new KeyCombo(new Array(67, 65, 84));
This code is creating a key stroke combination of the letters “C”, “A”, and “T”. Next, you must register your key combination with the Key singleton.
Key.getInstance().addKeyCombo(keyCombo);
Once the key combination has been registered, you can start listening for the combination of keys.
Key.getInstance().addEventListener(KeyComboEvent.DOWN, keyComboDownHandler); Key.getInstance().addEventListener(KeyComboEvent.RELEASE, keyComboReleaseHandler); Key.getInstance().addEventListener(KeyComboEvent.SEQUENCE, keyComboSequenceHandler);
KeyComboEvent.DOWN will get triggered when all of the keys are DOWN at the same time.
KeyComboEvent.RELEASE will get triggered when all of the keys are RELEASED at the same time.
KeyComboEvent.SEQUENCE will get triggered as long as the keys are pressed in the sequence which they are defined (“C” first, “A” second, “T” third).