Week 8: Introducing Interactivity

Topics:

  • ActionScript Basics
    • Using the Actions Panel
    • Flash Banner Ads with ActionScript 2:
      • Creating an invisible “button”
      • Adding Behavior to an object on the stage
      • Using the Trace function to test your code
    • ActionScript 3
      • How to Set up your Files for AS3 applications
      • Targeting an object in AS3
      • Creating Custom Functions
      • Creating an Interactive Button in AS3
      • Using ActionScript for Timeline Control

ActionScript Basics

ActionScript is the language used to program interactivity in Flash. It is what is called an Object Oriented Language, or OOP. We’ll talk more about what that means as we go along. ActionScript is also client-side language, which means that all the processing takes place on the end users machine. In practical terms, this means that the Flash movie can change without reloading the container HTML page, but it also means that Flash cannot save any information back to the Web server.

There are two ways to include ActionScript with your Flash movie, via the Actions panel, or a separate .as files. In this class we are going to explore the first method.

The Actions Panel

Actionscript is adding to your Flash movie through the Actions panel, which you can open either through the Windows menu or Shift+F9 .

The Actions panel will always show you the code for the object on the stage you currently have selected.

ActionScript Syntax

ActionScript is comprised of a series of statements that are always ended with a semi-colon:

some code here;

A more complex statement might be enclosed in curly brackets:

{
  more complex code;
  some more code;
}

If you want to include notes that won’t be read as code, you can do that in the form of comments. There are two types of comments in Actionscript:

// This is a single line comment
/* This is a multi-line
or block comment */

There are also a few naming rules to keep in mind:

  • Code is case sensitive: MovieClip is not the same thing as movieclip
  • Don’t create names that start with a number (it’s ok to have a number elsewhere in the name)
  • Don’t use non-alphanumeric characters in names except hyphen (-) or underscore ( _ )

Flash Banner Ads with ActionScript 2

ActionScript has gone through three versions, 1.0, 2.0, and 3.0. For the most part you should now exclusively use ActionScript 3.0, however the one exception you may encounter in your professional practice is Flash banner ads. Many sites use a click tracking system called ClickTag which is not compatible with AS 3.0. We’re going to review how to set up a ClickTag for a banner ad with ActionScript 2.0, in the case that you might need to do this in the  future.

Keep in mind that there are a few things you can’t use if you are working in AS2, because they were introduced with AS3:

  • 3D objects and transforms
  • TLF Text (use Classic)
  • Bone tool armatures

Step 1: Creating the Invisible Button Symbol

  1. Create a new layer at the top of your timeline layers
  2. On that layer, draw a rectangle on the stage that is the same size as the stage.
  3. Convert the rectangle to a Button symbol (not a MovieClip or Graphic!)
  4. Give the button symbol on the stage an instance name
  5. Open the button symbol to edit it. Copy the rectangle keyframe to the “Hit” frame. Clear all the other frames, so it looks like this:

This will make the button invisible, but clickable. Yay!

Step 2: Adding Behavior to an Object on the Stage

In AS2 (but not AS3!) you can add Actionscript directly to a MovieClip or Button instance on the stage via the Properties panel, by clicking the little arrow at the top right of the panel:

This will open the Actions panel for that object.

The code for creating an “on click” behavior in AS2 for your button is as follows:

on (release) {
 // put what you want to happen when the button is clicked here
}

Step 3: Using the Trace function to test your code

To test whether you’ve added the button and the behavior correctly, you can use the trace function. The trace function will send data to the output window when you are in Flash, but is invisible to the user once the Flash movie is published as a .swf online.

on (release) {
   trace("click");
}

To add a link to the click behavior, use the getURL function:

on (release) {
   getURL("http://www.google.com", "_blank");
 }

The first parameter is the full path to the web page you want to open. The “_blank” parameter will force the linked URL to open in a new browser window.

If you need to use a ClickTag, you will use this code instead:

on (release) {
 getURL(clickTAG, "_blank");
}

ActionScript 3.0

How to set up your files for AS 3.0 Applications

As I’ve mentioned, you cannot add ActionScript directly to objects in AS3. Instead, you can add code to any frame or sequence of frames on the Timeline. This includes the timelines of symbols.

While you can add code to any layer on the timeline, including layers that have symbols or other objects on them, it is much cleaner to designate a single, empty layer for your code. This will prevent you accidentally erasing code or objects when working with one or the other, and it will also make it easier for you or another developer working on the code to find it later.

With this goal in mind, I want you to place all your ActionScript on a single layer at the very top of your timeline called “actions”. So, the first thing you do now when creating a new file is to make this layer, then open the Actions panel and pin this layer so that it is always easy to get to it.

Targeting Objects on the Stage with ActionScript 3.0

Because you are no longer able to add behavior directly to objects, you need to be able to tell actionscript which object on the stage you are adding behavior to. In order to do this, you must target the object using its instance name. That means that every object on the stage that you want to be interactive must have an instance name!

Then, when you want to refer to that object in your ActionScript, you can call it by name, using object notation:

buttonone.dosomething();

You can also find named objects on the stage using the target button in the Actions panel:

Object Properties and Methods

The core functionality of any object-oriented programming language are classes, objects, properties and methods.

Every object on the stage is an instance of a defined class of object within the ActionScript core library. These classes have properties and methods attached to them, and these vary depending on the class. Here’s an easy way to think of the difference between a property and a method:

  • Properties define what an object is
  • Methods describe what an object does
When working with an instance on the stage, you’ll access the properties of that object like so:

instancename.propertyname;

You can change the value of that property like this:

instancename.propertyname = value;

Methods are called like this:

instancename.methodname(arguments);

Creating Custom Functions

ActionScript 3 has many built in functions, like the trace function which we’ve already used. You can also create your own functions, to hold code that you want to be able to reuse anywhere in your project. First you define the function:

function functionname(parameter1,parameter2):return {
    //do something
}

Then, when you want to use the code in the function, you call it using the function name like this:

functionname (value1,value2);

The values in the parentheses are parameters, extra information you can pass into the function when you call it. For example, when you call the trace function, the parameters are whatever you put between the parentheses that you want to send to the output window.

Creating an Interactive Button in AS 3.0

When creating a button or other interactive object in ActionScript 3, there are two steps you must create: an event listener, and an event handler. You can add event listeners and handlers to Button and MoveClip Instances but not Graphic symbols.

The Event Listener

The event listener describes what object to target, and what event to respond to. You do this using the addEventListener method.

This function takes two parameters, the event to respond to (like a mouse click) and the function to run when the event is detected.

 

The first parameter above describes an event of type MouseEvent, and the CLICK event is the particular event that is being listened for. Here are all the MouseEvents you can listen for:

  • CLICK
  • DOUBLE_CLICK
  • MOUSE_DOWN
  • MOUSE_MOVE
  • MOUSE_OUT
  • MOUSE_OVER
  • MOUSE_UP
  • MOUSE_WHEEL
  • ROLL_OUT
  • ROLL_OVER

The Event Handler

The event handler is a function that you must define, to describe what you want to happen when the event happens.

Using ActionScript for Timeline Control

So far animation that you have created on the timeline plays automatically and continuously. You can use ActionScript to control when your movie starts and stops, and to jump around the timeline.

There are a number of timeline related methods of the MovieClip class that you can use to control playback of your main or MovieClip nested timelines:

  • stop( ) – This will stop the movie wherever it appears on the timeline
  • play( ) – Starts playback of the movie
  • nextFrame( ) – Advances the movie one frame
  • prevFrame( ) – Rewinds the movie one frame
  • gotoAndPlay(frame) – Goes to the specified frame (either by name or number) and start playing from that point.
  • gotoAndStop(frame) – Goes to the specified frame (either by name or number) and stop there

There is also one timeline related property that will help you do your homework:

  • currentFrame;

Homework

Take the original walkcycle exercise or the bone tool exercise (or really any of the animation assignments)  you did earlier in the class, and add buttons that do the following:

  • Play
  • Stop
  • Move Forward 1 Frame
  • Move Back 1 Frame
  • Jump to the End
  • Go Back to the Beginning

Here’s a hint: Anything that’s a number in Flash (like frame numbers) can be used  to do simple math – add, subtract, multiply and divide.

Week 7: 3D Transformations, Audio Part 1

Topics:

  • Working in 3D
    • 3D Rotation Tool
    • 3D Translation Tool
    • Global vs. Local Transforms
    • 3D in the Properties Panel
    • 3D in Transform Panel
    • 3D Tweens
  • Audio Part 1: Timeline Audio

Working in 3D

So far we’ve been restricting ourselves to the X and Y axes in our Flash animations, but the time has come to venture into the Z! Today we are going to look at the 3D tools, and 3D Motion Tweens. First there are a couple of things you should know about 3D in Flash:

  • The native 3D in flash is really what we like to call “2 1/2 D” because while you can move and rotate object on three axes, the object themselves are still two-dimensional. To get truly 3D object in Flash you will need the help of third party code libraries.
  • 3D transforms only work if your publish settings are set to Actionscript 3.0 and Flash Player 10.
  • 3D transforms increase file size and processing requirements, so only use them when you need them!
  • You can only perform 3D transformations on MovieClips and TLF Text!

3D Rotation Tool

This is the “top” tool in the 3D tools panel. The three axes of rotation are represented by Red (X), Green (Y), and Blue (Z) circles which you can grab and drag independently with your mouse. Click and drag outside of those circles, within the orange bounding circle, and you can rotate organically in all three dimensions.

3D Translation Tool

This is under the 3D rotation tool. This allows you to move your object in 3D space. The three axes are represented by Red (X), Green (Y), and Blue (Z) arrows, and you can move in that dimension by clicking and dragging on the associated arrow. You can only move in one direction at a time

Global vs Local Transforms

By default, the X,Y,Z axes and coordinates are represented relative, or local, to the object, but if you want to see them relative to the global stage coordinates, you can toggle the Global Transform Button at the bottom of the Tools palette or hit ‘D’.

3D in the Properties Panel

If you want more precise control over your 3D positioning, you can manipulate it in the Properties panel for the selected object. You can also change the Perspective Angle and Vanishing Point here, which will affect how your object appears to move forward and back in the Z axis.

3D in Transform Panel

If you want more precise control over your 3D rotation, you can manipulate it in the Transform panel (not the free Transform tool). You can also change the 3D center point, which is where the object will rotate from.

3D Tweens

You can only apply Motion Tweens to objects with 3D properties – Classic Tweens won’t work. When you apply a Motion Tween to an object with 3D properties set, the 3D Tween option in the contextual menu will automatically be checked. 3D tweens work the same as regular motion tweens, just with the addition of the 3D position and view properties — all tweenable!

There is additional information about 3D transforms in Flash here:

Exploring the new 3D features in Flash CS4 Professional

Using Audio Part 1: Timeline Audio

There are a number of different ways you can play audio through Flash, each has it’s advantages and disadvantages.

Supported Audio types

Many audio types are supported in Flash, but you’ll have best luck with .aiff or .mp3 files. All audio is published out of Flash in the mp3 format, so this is a good format to start with.

Import to Library

You add audio to your Flash movie the same way you import other media assets. You can use either “Import to Library” or “Import to Stage” but either way the audio file will end up in your library, but not on the stage.

Select an imported audio file in the library, and you will see a visual representation of the sound file, in addition to tiny little play and stop buttons to the top and right that allow you to preview the audio file.

To see additional properties of the sound file, double-click the speaker icon next to the filename.

Check your file size! This is going to impact the total size of your swf. You can adjust the compression settings here for this file only or in the Publish Settings panel for the whole movie.

Adding Audio to the Timeline

You add audio to a keyframe on the timeline, in the properties panel when you have a frame selected. The audio will start playing when the swf hits that frame.

Choose the sound you want to add with the “Name” pulldown in the sound section of the properties panel.

Once you do this, you will see the waveform of the sound in the timeline.

Increase the height of the layer by control or right clicking the layer name and changing the layer height:

How the file will playback depends on how the Sync property is set. There are two primary settings you should concern yourself with here:

Event – Plays whole audio file. If you don’t want this, you can do some minor editing in the sound properties. This should be reserved for very short sounds, or you will get awkward echoing as the movie loops and continues to play the sound again and again.

Stream – Plays the sound only as long as the timeline. You can set the repeat and loop settings for replay, but this will always stop a previously playing sound before starting the next one.

The problem with both of these methods is that you aren’t giving the user any control over the playback of the sound. I generally avoid timeline sound in favor of one of the methods we will learn later in the class.

Homework

Choose a song with lyrics and import it into Flash. Create a 10-15 second animation of the song lyrics in time to the music using 3D tweens. The bulk of your animation should be text, but you can use other graphic elements if you like.

Week 6: Advanced Animation Part 1: Bone Tool, Motion Presets, Trace Bitmap

>Week 6 Examples

Topics:

  • Trace Bitmap
  • Swapping symbols
  • Motion Presets
    • Using
    • Saving
  • Bone Tool

Trace Bitmap

Flash has the ability to take an imported bitmap and turn it into vector shapes via the Modify > Bitmap > Trace Bitmap command. The success of this will greatly depend on the image you start with.

Motion Presets

Flash ships with a number of these, which are basically canned motion tween animations. Use these sparingly, and always customize them for your project. “Out of the box” they are very recognizable and make you look like an amateur. More interesting is the ability for you to save your own motion presets, handy when you are doing a project where you are going to use the same transitions multiple times.

Using the Bone Tool

The Bone tool allows you to create an armature of “bones” using either shapes or symbols to create dynamically linked characters or objects that use something called “inverse kinematics” to animate them. You can use bones on either a series of linked symbols or a shape, to different effect.

The Bone Tool only works with CS4 and above, with Actionscript 3. There are some small differences between CS4, CS5 and CS5.5.

Homework

Create a 10 second animation using the Bone Tool, either using linked symbols or shapes. This can be a character animation, something mechanical, something abstract, it’s up to you.

Week 5: Project 1 Presentations

Topics:

  • Project 1 Presentations
  • Optimizing Your Flash Files

Homework

None … Unless you want  it. If you aren’t satisfied with your greeting card after today’s critique, you can turn in a revised version next week and improve your grade!

Optimizing Your Flash Files

If we have time ….

Since most Flash projects are meant to be seen on the Web, you have to be mindful of how large the file size is. The larger the file size, the longer it will take to load, and the more annoyed the viewer will get! Here’s some tips for keeping the file size down:

Strategies for keeping file size low:

  • Lower the frame rate. The lower it is, the smaller the file. Warning, if you do this after you start animating, it’s going to slow everything down, and you will have to adjust your animation to compensate.
  • Flatten effects in Photoshop. Flash will import layer effects like strokes, drop shadows and glows as separate layers, which really adds up to increase file size. If you forget to do this, you can also select multiple Flash layers and use “Convert to Bitmap” in the Modify menu to merge them into a single bitmap.
  • Recreate layer effects in Flash, especially if you are planning to scale the objects they are applied to
  • Recreate text in Flash, especially if you are planning to scale it
  • Adjust publish settings for image compression
  • Adjust individual bitmap settings for image compression
  • Don’t import bitmap art at a larger size than you need it! This will make your file HUGE.