Designed with animators in mind

Tutorials

Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 Tutorial 5

Tutorial 5 - Javascript Interfacing

This tutorial will show you how to move objects in your Flash animation around using Javascript. This is useful for creating games since you can use Javascript to store things such as scores and game states. For example by making use of rotations and movements you could make a game of pool.

Example 1

Example 2

Example 3

Javascript to SWF Interfacing


1. Make sure you are on Level 0 of your animation. This level should only have one frame or a stop action on the first frame.

2. Select the object that you want to control with the blue cursor.

3. If it is not already grouped as a MovieClip, right click on the shape and choose Group as MovieClip from the menu.

4. Click on the Frame tab on the right hand side. This gives you all the information about the selected object on this frame.

5. Enter a reference for this object in the reference box and click Apply. (This is different from the actual name of the object since you might have two of the same object on your frame but you can give each one a separate reference.) If you don't see this box download the latest edition of Express Animator from the main page. (Click on the alternative link.)

6. When you have embedded your Flash object in your HTML webpage give the object tag the ID "myflash" for example. The following is some javascript code which can control the objects you referenced. This either goes in your script tags in your HTML page or in a separate .js file.

Javascript source code
// Get a reference to your Flash SWF animation.
// This is different for different browsers so use the following:
var flash;
if(window.myflash)   flash = window.document["myflash"];
if(document.myflash) flash = document.myflash;

//----------------------Setup some global variables---------------//

var _x=0, _y=1, _xscale=2, _yscale=3, _currentframe=4, _totalframes=5;
var _alpha=6, _visible=7, _width=8, _height=9, _rotation=10;
var _target=11, _framesloaded=12, _name=13, _droptargert=14, _url=15;   
var _quality=19, _xmouse=20, _ymouse=21;


// Wait until this window loads before doing anything

window.onload = function()
{

  //----------------------Set Object Values-----------------------//

  // Set the position of the object to (50,200):
  flash.SetVariable("myobject._x",50);
  flash.SetVariable("myobject._y",200);

  // Set the rotation of the object to 45 degrees:
  flash.SetVariable("myobject._rotation",45);

  // Set the opacity value of the object to 50%:
  flash.SetVariable("myobject._alpha",50);

  // Set string in text box
  flash.SetVariable("mytextbox","Hello there!");


  //--------------------Get Object Values-------------------------//

  // Get the position of the object:
  var x = flash.TGetPropertyAsNumber("myobject",_x);
  var y = flash.TGetPropertyAsNumber("myobject",_y);

  // You get the general idea.
}
7. Say you have a movieclip star inside another movieclip starmovie which is on Level 0. The way the get the position of star is to write TGetPropertyAsNumber("starmovie.star",_x) So you have to give the full path to the movieclip if it is embedded in several other movieclips. The name of Level 0 is _root so to get the width of the movieclip you could write TGetPropertyAsNumber("_root",_width).


SWF to Javascript Interfacing


1. Select a shape with the blue cursor.

2. Right click on it and choose Group as Button

3. Now go to the Actions menu and click on Button Events... On Press..

4. In the actions window go to the Actions menu and choose GetURL( hyperlink )

5. In the hyperlink box, type in

javascript: myfunction()

6. This tells it that when you click on the button it must execute the function called myfunction.

7. Close the action window and export your animation as a SWF.

8. Once your have embedded the SWF in your webpage you can now write your javascript function in your HTML page. For example:

function myfunction()
{
    alert("You have pressed a button in the SWF!");
}

Notes


Don't use GetVariable to get properties of objects use TGetPropertyAsNumber because this works on Internet Explorer and Firefox. The example may not work locally on your computer due to Windows XP not allowing javascript to run locally. You will need Express Animator to follow this tutorial. The examples have been tested on Internet Explorer 7 and Mozilla Firefox 2.

Glossary of useful Javascript SWF functions

These functions are members of the Javascript Flash animation object.
Function NameDescription
GetVariable( varName )Returns the value of the specified variable.
GotoFrame( frameNumber )Movies animation to the specified frame.
IsPlaying()Returns true if the animation is playing.
LoadMovie( layerNumber, url )Loads the SWF at the given URL to layer.
Pan ( x, y, mode )Pans animation by (x,y).
PercentLoaded()Returns the percentage loaded.
Play() Plays the animation.
Rewind() Rewinds the animation.
SetVariable( variableName, value ) Sets the value of the variable specified.
SetZoomRect ( left, top, right, bottom ) Sets the zoom rectangle.
StopPlay() Stops the animation.
TotalFrames() Returns the total number of frames in the animation.
Zoom( percent ) Zooms the animation by the percentage specified.

The following functions require you to specify the path (target) to the MovieClip that you want to apply it to.
Function NameDescription
TCallFrame( target, frameNumber ) Executes the action in the frame.
TCallLabel( target, label ) Executes the action in the frame with label.
TCurrentFrame( target ) Returns the number of the current frame.
TCurrentLabel(target)Returns the label of the current frame.
TGetProperty( target, property)Returns the property given by the number.
TGetPropertyAsNumber (target, property)Returns the property as a number.
TGotoFrame( target, frameNumber ) Goto the frame.
TGotoLabel( target, label ) Goto the frame given by the label.
TPlay( target ) Play the given movieclip.
TSetProperty( target, property, value) Set the given property.
TStopPlay( target ) Stops the given movieclip.


Français Deutsch Español Chinese
home help features examples links who are we? forum inexpensive magazine free graphics gallery bargain news website animation value testimonials rent cartoons extras

Express Animator © 2007-8

Animation News
Monday July 21, 2008