Flash Components      Flash Menu      Flash Gallery      Flash Slideshow      FLV Player      Flash Form      MP3 Player      PhotoFlow      Flash CMS      3D Wall      Flash Scroller

Flash / AS

Silverlight

WPF

ASP.net / PHP

Photoshop

Forums

Blog

About

 


FlashComponents
  Galleries
  Slideshows
  Menus
  Design & Effects
  Audio & Video
  User Interface
  Templates

 

 

 

 

 
Springs
in MX: Making the Prototype Work
         written by ilyas usal a.k.a. pom

In the first tutorial concerning springs, there was a problem when I was trying to apply the prototype move to several objects. It worked with Flash 5, but not with MX, where all the objects had the same motion, as if Flash had grouped them into one big object. Well, I did my little research, and it turns out that Flash MX handles variables quite differently from Flash 5.

Making It Work
The prototype should look like this:


 

Basically, I put this. in front of all the variables. Explanation :

Writing the prototype the way I had (without the this.) makes the variables relative to the timeline containing the definition of the prototype. This means that all the movie clips will share these variables, and consequently the same behavior. Another risk would be that the variables overwrite variables from that timeline if they have the same name.

Putting this. in front of the variables makes them relative to the object that calls the function, that is to say the movie clip in the present situation.

How Flash Handles Variables
If you try this code in the main timeline:

function move(){
x++;
};

Then when you use this function, you create or modify a variable in the main timeline.

If you turn it into :

function move(){
var x;
x++;
};

A variable is created in the variable space of the function, and it will be destroyed when you get out of the function.

And if we write in the main timeline :

MovieClip.prototype.myFunction=function() { x++;}

Flash looks for x in the main timeline, where the function has been defined, no matter from which timeline is called the function.

Now with this :
 

function move(){
this.x++;
}

x belongs to the object calling the function (this refers to the object possessing the function as a method).
This means that if we use this function as a method :
 

MovieClip.prototype.move=move;

this will point to the movie clip calling its method move.

To summarize a little bit
When the ActionScript interpreter sees a reference to a variable in a method/function, it checks first if it is defined INSIDE the function (with var), then in the timeline where the function/method has been defined. Using this, we tell Flash not to look at the variables of the function/method nor in the timeline containing the definition of the function, but rather in the object to which the method/function belongs.

This article is written by Ilyas Usal. Ilyas is also known as ilyaslamasse on the kirupa.com forums! If you have any questions, please post them on the forums by clicking here.

 
pom 0]

 


kirupa.com's fast and reliable hosting provided by Media Temple. flash components
The Text Animation Component for Flash CS3
Check out the great, high-quality flash extensions. Buy or sell stock flash, video, audio and fonts for as little as 50 cents at FlashDen.
Check out our high quality vector-based design packs! Flash Effect Components

Flash Templates
CSS Templates
Dreamweaver Templates

flash menus, buttons and components
Digicrafts Components The best flash components ever!
Entheos Flash Website Templates Buy and sell FLAs at Ultrashock!
Upload, publish, deliver. Secure hosting for your professional or academic video, presentations & more. Screencast.com Purchase & Download Flash Components
Learn how to advertise on kirupa.com