Author Archive for Zevan

06
Jun

TweenLite Introduction


One of my favorite things to do in Flash is to program different types of motion. From particle systems to the boss at the end of an advergame—the basic principles for moving things around the screen with code are the same. There was a time when all the different types of motion that I saw happening—back in the Flash 4 days—were a complete mystery to me. I remember spending a long time reverse engineering open source examples from ultrashock.com.

Motion Myth

For some reason lots of people seem to think that ActionScript animation creates smoother animation than the Flash timeline. This is simply not true. The timeline is just as capable of creating “smooth” animations as ActionScript is.

On the timeline, if you want a smoother animation, use a framerate around 30fps and put more space between your keyframes.

ActionScript vs. Timeline

Students sometimes ask me when to use coded animation and when to use timeline animation. The answer is more complicated than you might expect. Here’s a basic set of rules:

TIMELINE ANIMATION IS FOR:
- animation that is always the same (static animation)

ACTIONSCRIPT ANIMATION IS FOR:
- interactive animation
- random animation
- animation driven from an outside source like an rss feed
- complex, static physics simulations that would be hard to animate on the timeline
- complex interactive physics simulation
- time-based animation—that is, animation that always runs at the same speed independent of framerate

Continue reading ‘TweenLite Introduction’

30
May

Linkage Classes as Arguments


I like to make my custom UI elements easy to skin. If we wanted to create a check box class that was easy to skin, one thing we could do is create an FLA based AS3 component. Unfortunately to do that takes advanced programming skills. If you’re an intermediate programmer you might want to be able to create a skinnable UI element without all of the AS3 component architecture.

One way to achieve this is by creating a custom class that uses Library elements as assets. To increase flexibility (for swapping skins, for example), you can create each graphical element of your check box, store them in your Library, assign a linkage class name to each element, and then pass those linkage class names into your custom class.

Continue reading ‘Linkage Classes as Arguments’

17
May

Grids: Arranging Clips


It’s extremely common to need to position display objects in a grid formation. I usually find myself doing it when displaying thumbnails for an image gallery. This post will take you through the process step by step.

Continue reading ‘Grids: Arranging Clips’

13
May

The Power of Relative Positioning


Understanding the basic display object properties like x, y, width, height etc… is imperative for anyone interested in intermediate level ActionScript. It’s important to practice using properties until you understand them well enough to solve problems using them. If you’re not comfortable with properties yet, you should read through Chapter 3 and then try the exercises in the Property Practice (simple motion) post.

Continue reading ‘The Power of Relative Positioning’

20
Apr

Property Practice (simple motion)


Having a good grasp of the basic properties of display objects is extremely important for anyone interested in intermediate level ActionScript. This post assumes that you’ve taken the time to read through Chapter 3. If you’re new to ActionScript, it will likely help with this post if you read up to Chapter 7.

This post contains a few short exercises that will put your knowledge of properties to the test.

As a guide, here are the properties that you will need to be familiar with:

x
y
scaleX
scaleY
height
width
rotation

For each example you’ll need to use the properties in conjunction with an enterFrame event:

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);

function onLoop(evt:Event):void {
  // change properties over time
}

Continue reading ‘Property Practice (simple motion)’

20
Feb

Graphics Class clear() Method


Over the past two weeks I’ve taught two different lessons covering the basic ins and outs of the Graphics class (Drawing API). Something I noticed is that it’s not always immediately apparent when you’re starting out that the clear() method can be used to animate dynamically drawn vector graphics. That is, in both my class demos I introduced clear() by using it to erase a canvas when a button was clicked. When I described the idea of continuously clearing and redrawing graphics to create an animation there was some confusion, so I created a simple demo on the fly:

addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);

function onLoop(evt:Event):void {
	graphics.clear();
	graphics.lineStyle(1,0x000000);
	graphics.moveTo(100,100);
	graphics.lineTo(mouseX, mouseY);
}

Copy this into your timeline if you’re not sure what it does.

Continue reading ‘Graphics Class clear() Method’

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)’

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’

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’