1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
| 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;
}
}
} |
Partager