/* ************************************************ EXAMPLE: Hello World AUTHOR: Keith Peters CREATED: August 27, 2005 MODIFIED: August 27, 2005 Macromedia® Flash® by Example http://www.ifbin.com ************************************************ */ /* This example takes a sample string, "Hello, world", and creates a movie clip containing a text field for each letter in the string. On each frame, the distance from the mouse to each letter is calculated. If it is less than a minimum distance, the letter springs away from the mouse. It will also spring back to its home position. The basic formula for a spring is: acceleration = (target - currentlocation) * spring velocity += acceleration */ // import Delegate class to allow for better handling of events import mx.utils.Delegate; class HelloWorld { private var target:MovieClip; // the movie clip to attach assets to private var message:String = "Hello, world";// the message to display on screen private var letters:Array; // an array to hold the letter movie clips private var minDist:Number = 100; // distance at which mouse will affect letters private var spring:Number = 0.1; // how strong the attraction/repulsion force will be private var damp:Number = 0.9; // a friction value /** Static main function. Can be called from a class level. Takes a movie clip reference as a parameter. */ public static function main( target:MovieClip ):Void { // create new instance of this class, passing the target movie clip. var helloWorld:HelloWorld = new HelloWorld( target ); } /** Constructor for class. Takes a target movie clp as a parameter. */ public function HelloWorld( target:MovieClip ) { // assign the parameter to this class's target property this.target = target; // call the init function to get things rolling. init(); } /** Initializes class */ private function init():Void { // call the makeLetters function to parse the message and make the letter movie clips makeLetters(); // use Delegate to create a reference to this class's onEnterFrame method. // this ensures that onEnterFrame will run within the scope of this class // assign the delegate to the target movie clip's onEnterFrame handler. target.onEnterFrame = Delegate.create( this , onEnterFrame ); } /** Parses the message string and creates the letter movie clips. */ private function makeLetters():Void { // create an array to hold the letter movie clips. letters = new Array(); // for each letter in the message, do the following... for( var i:Number = 0 ; i < message.length ; i++ ) { // create a new movie clip on the target movie clip var letter:MovieClip = target.createEmptyMovieClip( "letter" + i , i ); // create a text field inside of it letter.createTextField( "tf" , 0 , -20 , -20 , 40 , 40 ); // assign the text, as one letter of the message letter.tf.text = message.charAt( i ); // set the properties of the text field. letter.tf.selectable = false; letter.tf.setTextFormat( new TextFormat( "Arial" , 30 , 0 ) ); // line the letters up so they go across the screen letter._x = ( Stage.width / 2 ) - ( message.length / 2 * 30 ) + ( i * 30 ); letter._y = Stage.height / 2; // homeX and homeY are the same as initial position // letters will spring back here after they are moved letter.homeX = letter._x; letter.homeY = letter._y; // initial velocity is 0 letter.vx = 0; letter.vy = 0; // add to array letters.push( letter ); } } /** This function will be called as the handler for the target movie clip's onEnterFrame But because it is assigned using Delegate, the scope of the function will be this class, not the movie clip */ public function onEnterFrame():Void { // for each letter in the array, do the following for( var i:Number = 0 ; i < letters.length ; i++ ) { // get a reference to the letter movie clip var letter:MovieClip = letters[i]; // get x, y and total distance var dx:Number = letter._x - _xmouse; var dy:Number = letter._y - _ymouse; var dist:Number = Math.sqrt( dx * dx + dy * dy ); // if it is close enough to the mouse... if( dist < minDist ) { // find a target x,y position that is far away from the mouse var tx:Number = _xmouse + dx / dist * minDist; var ty:Number = _ymouse + dy / dist * minDist; // spring the letter to that point letter.vx += ( tx - letter._x ) * spring; letter.vy += ( ty - letter._y ) * spring; } // spring the letter to its home position letter.vx += ( letter.homeX - letter._x ) * spring; letter.vy += ( letter.homeY - letter._y ) * spring; // apply some friction so it eventually slows down letter.vx *= damp; letter.vy *= damp; // add the velocity to the position letter._x += letter.vx; letter._y += letter.vy; } } }