Archive Page 2

28
Jan

More Motion (Gravity)


Chapter 7 of the book contains an overview of motion programming techniques. This post expands on some of these techniques.

Continue reading ‘More Motion (Gravity)’

12
Jan

Chapter 3 Document Classes


Thanks to Nick, we discovered an error in the document classes that accompany the source code for Chapter 3. The error has been corrected, and the archive has been replaced. This has nothing to do with the book, so this only applies to those early coders who downloaded the Chapter 3 source archive prior to January 12, 2008.

We introduce syntax and concepts early on in a way that allows us to focus closely on the topics themselves, without having to dive immediately into classes. We then use classes more and more as we progress through the chapters.

However, if you already have some experience with classes (or want to start pushing yourself to this ideal early on), we also provide a second copy of all the timeline examples, using document classes instead. This way you can experiment with both approaches and pick the style that suites you best at the outset of the book.

So, none of the timeline examples were affected and, if you only need to think about this if you downloaded the Chapter 3 archive before January 12th.

Thanks Nick!

12
Jan

Site Testers


We would like to start a small list of folks who would be willing to put this site through its paces. If anyone is interested/willing to test the site over time- (no commitment required, just test as you go) please send us a note through the Contact form. Thanks.

14
Dec

Sending Data from AVM2 to AVM1


In Chapter 13, we showed how to use a LocalConnection to communicate between an AS3-based movie in Flash Player’s AVM2, and an AS2-based movie in Flash Player’s AVM1. The example was basic, using functions to stop and start an animated movie clip. In this first of future additional examples, we’ll pass data into a function in the AVM! movie.

Continue reading ‘Sending Data from AVM2 to AVM1′

06
Dec

Sound Mixer Source


In Chapter 11 we took an extensive look at sound in AS3. We’ve created a sound mixer demo to showcase some of the techniques covered in the book.

Continue reading ‘Sound Mixer Source’

02
Dec

Complex Particles Source


In Chapter 7 we talked extensively about programming motion with AS3, and created a basic particle system. This post contains source for a more complex particle system, based on the book example.

Continue reading ‘Complex Particles Source’

21
Nov

Mandatory Argument Types


Loosely related to the Dispatching Custom Events post, here's a quick example of how to deal with the data type checking of mandatory arguments, such as those used with event listeners.

Below is all the source code required for this example, including the errant segments. Lines 1 through 7 setup a simple timer that calls a function every 3 seconds, until the timer is triggered three times. If you test just that portion of the code, you'll see an initial delay of three seconds, followed by three traces to the Output panel.

Suppose, however, that you also want to trigger the same event once, immediately. If you just call the function, as seen in line 10, you will get an error. This is because you can't call a listener with required arguments, without sending the required values. That is, the listener function requires an event object for the evt parameter, and an error will be thrown if it is not supplied.

ActionScript 3.0:
  1. var timr:Timer = new Timer(3000, 3);
  2. timr.addEventListener(TimerEvent.TIMER, onTimer, false, 0, true);
  3. timr.start();
  4.  
  5. function onTimer(evt:TimerEvent):void {
  6.     trace("timer function triggered");
  7. }
  8.  
  9. //scenario 1
  10. //onTimer();
  11. //error: 1136: Incorrect number of arguments.  Expected 1.
  12. onTimer(null);
  13.  
  14. //scenario 2
  15. onTimer(new TimerEvent(TimerEvent.TIMER));
  16.  
  17. //scenario 3
  18. //stage.addEventListener(MouseEvent.CLICK, onTimer, false, 0, true);
  19. //TypeError: Error #1034: Type Coercion failed: 
  20. //   cannot convert flash.events.MouseEvent to flash.events.TimerEvent.
  21. stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
  22.  
  23. function onClick(evt:MouseEvent):void {
  24.     onTimer(new TimerEvent(TimerEvent.TIMER));
  25. }

One easy way to accomplish this goal, is to send null with the function call, as seen in line 12. This will prevent an error from being thrown. If the event itself can be of use to you, you can create a new Timer event and send that to the listener argument. This can be seen in line 15, and is also covered in the previous post. All you have to do is follow the new keyword, with the desired event class, and the specific event you want to be created.

The third scenario is an example of wanting to augment the timer with mouse clicks. If you simply add a mouse listener to the stage, as seen in line 21, you will also get an error. The onTimer function will be called, but the event passed to the function will be a mouse event, rather than the required timer event. One way to get around this is to create an interim function, that correctly receives the mouse event and sends the desired timer event to the ultimate function, as seen in lines 23 through 25.

20
Nov

Dispatching Custom Events


In Chapter 3, we cover the basics of using events. One topic that's not covered extensively (appearing in later chapters), is using the dispatchEvent() method to send a specific, or even custom, event. This method is contained within the EventDispatcher class.

The dispatchEvent() method takes an Event object as its first and only argument. The following code will dispatch a custom event of type "my event":

dispatchEvent(new Event("my event"));

You can also use any AS3 event constant:

dispatchEvent(new Event(Event.INIT));

The below class dispatches an event after a one second timer has elapsed:

package {

    import flash.events.EventDispatcher;
    import flash.utils.Timer;
    import flash.events.*;

    public class DispatchEventExample extends EventDispatcher {

        private var _timer:Timer;

        public function DispatchEventExample() {
            _timer = new Timer(1000, 1);
            _timer.addEventListener(TimerEvent.TIMER, onTimer,
                                    false, 0, true);
            _timer.start();
        }

        private function onTimer(evt:TimerEvent):void {
            dispatchEvent(new Event("one second elapsed"));
        }
    }
}

To listen for the event, use the addEventListener() method. Similar to when dispatching events, as discussed previously, you can either listen for an event constant (such as Event.ENTER_FRAME, or your own custom event class), or a simple string. Here is an example document class that listens for the "one second elapsed" event:

package {

    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite {

        private var _dispatchExample:DispatchEventExample;

        public function Main() {
            _dispatchExample = new DispatchEventExample();
            _dispatchExample.addEventListener("one second elapsed",
                                      onOneSecond, false, 0, true);
        }

        private function onOneSecond(evt:Event):void {
            trace("_dispatchExample has dispatched an event");
        }
    }
}

Continue reading 'Dispatching Custom Events'

19
Nov

Stopping Event Propagation


Stopping an event's propagation, or continued movement through the display list, is another option for more advanced event processing. In most cases, an event will not die at the target phase right after it is executed. Instead, it will bubble back up through any existing ancestors. However, you can also prevent the event from continuing on its journey by halting the propagation process.

Continue reading 'Stopping Event Propagation'

19
Nov

Interesting Use for Capture Phase


Earlier, in the "Event Phases in Action" post, we discussed using the event listener capture phase. This post shows an animation-based example of the capture phase in use.

Before AS3, if I wanted to reference a bunch of unnamed clips I would use a for..in loop:

// as2 style
for (var i:String in this) {
    trace(this[i]);
}

I would follow this up with a check to see if this[i] was a movie clip and, if it was, I would do something with it. This approach no longer makes much sense with the advent of the AS3 display list. Instead, you would use something like this:

for (var i:uint = 0; i < numChildren; i++) {
    trace(getChildAt(i));
}

If we wanted to get at all the nodes of the display list our code would start to look a bit more complicated. We addressed this in the book in Chapter 4, in the "Displaying the Display List" section beginning on page 54. As an interesting alternative we can use the ADDED_TO_STAGE event's capture phase to get at all the nodes on the display list. Here is a simple example:

var container:Sprite = new Sprite();
container.name = "container";
var one:Sprite = new Sprite();
one.name = "one";
var two:Sprite = new Sprite();
two.name = "two"
var three:Sprite = new Sprite();
three.name = "three";
var four:Sprite = new Sprite();
four.name = "four";

one.addChild(two);
two.addChild(three);
three.addChild(four);

container.addChild(one);

container.addEventListener(Event.ADDED_TO_STAGE, onAdded, true, 0, true);
addChild(container);

function onAdded(evt:Event):void {
    trace(evt.currentTarget.name + " " + evt.target.name);
}

This will trace the following to the output window:

container one
container two
container three
container four

The above code starts off by creating a set of nested sprites. Sprite container holds sprite one, sprite one contains sprite two, etc. Given this group of sprites we add a listener to the container sprite with the capturePhase boolean set to true. Because all sprites dispatch an ADDED_TO_STAGE event, the listener function runs for each sprite starting with one moving down the display list to two and so on. The currentTarget property of the event object remains container.

Continue reading 'Interesting Use for Capture Phase'