Thursday, 1 October 2009

Glowing mouse effect



view plaincopy to clipboardprint?
package
{
import flash.display.Sprite;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.events.Event;

package
{
import flash.display.Sprite;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.events.Event;Step 6: Mouse Trailer Class
We extend the Sprite Class to access the addChild() method. Remember that the name of the class has to be the same as the file name.

view plaincopy to clipboardprint?
public class MouseTrailer extends Sprite
{

public class MouseTrailer extends Sprite
{Step 7: Variables
There's only one variable in this class, the LightBall variable. This is used to create a new instance of the LightBall that we created in the Fla.

view plaincopy to clipboardprint?
var lightBall:LightBall;

var lightBall:LightBall;Step 8: Constructor
In the constructor function we'll add the lines that hide the Mouse cursor and the Listener that will start the Trailer.

view plaincopy to clipboardprint?
public function MouseTrailer():void
{
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, startTrailer);
}

public function MouseTrailer():void
{
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE, startTrailer);
}Step 9: Start Trailer Function
This function will handle the trailer, setting its properties.

view plaincopy to clipboardprint?
private function startTrailer(e:MouseEvent):void
{

private function startTrailer(e:MouseEvent):void
{Step 10: Duplicating the LightBall
This code creates a new LightBall when the mouse is moved.

view plaincopy to clipboardprint?
/* Create a new LightBall object */

lightBall = new LightBall();

/* Create a new LightBall object */

lightBall = new LightBall();Step 11: Position
The new LightBall position is based on the width and height of the clip and the position of the Mouse cursor.

view plaincopy to clipboardprint?
/* Position */

lightBall.x = mouseX + Math.random() * lightBall.width;
lightBall.y = mouseY - Math.random() * lightBall.height;

/* Position */

lightBall.x = mouseX + Math.random() * lightBall.width;
lightBall.y = mouseY - Math.random() * lightBall.height;
Step 12: Adding to Stage
We add the LightBall to the stage with the following code:

view plaincopy to clipboardprint?
/* Add to Stage */

addChild(lightBall);

/* Add Listener to Animate function */

lightBall.addEventListener(Event.ENTER_FRAME, animate);

/* Add to Stage */

addChild(lightBall);

/* Add Listener to Animate function */

lightBall.addEventListener(Event.ENTER_FRAME, animate);Step 13: Animate Function
The alpha, scale and vertical position are handled in this function.

view plaincopy to clipboardprint?
private function animate(e:Event):void
{

private function animate(e:Event):void
{Step 14: Alpha
The alpha is reduced by 5% every frame.

view plaincopy to clipboardprint?
/* Alpha */

e.target.alpha -= 0.05;

/* Alpha */

e.target.alpha -= 0.05;Step 15: Remove Invisible Objects
When the LightBall is no longer visible (alpha < 0) the object is removed.

view plaincopy to clipboardprint?
/* If lightBall is no longer visible, remove it */

if (e.target.alpha <= 0)
{
e.target.removeEventListener(Event.ENTER_FRAME, animate);

removeChild(e.target as Sprite);
}

/* If lightBall is no longer visible, remove it */

if (e.target.alpha <= 0)
{
e.target.removeEventListener(Event.ENTER_FRAME, animate);

removeChild(e.target as Sprite);
}Step 16: Scale
The scale is reduced by 10% every frame.

view plaincopy to clipboardprint?
/* Scale */

e.target.scaleX -= 0.1;
e.target.scaleY -= 0.1;

/* Scale */

e.target.scaleX -= 0.1;
e.target.scaleY -= 0.1;Step 17: Vertical Position
view plaincopy to clipboardprint?
/* Y Position */

e.target.y += 3;
}

//have fun!

No comments: