Create a Glowing Mouse Trailer in Flash

Mouse Trailers are objects which follow the mouse cursor as you move it. In this tutorial, I’ll help you create a blue and shiny mouse trailer using ActionScript 3.0.

Final Result Preview

Let’s take a look at the final result we will be working towards:

Step 1: Brief Overview

Using ActionScript we’ll duplicate a MovieClip then modify its alpha, scale, and position properties to get a nice trailer effect.

Step 2: Starting

Open Flash and create a new Flash File (ActionScript 3).

Set the stage size to your desired resolution and add a background color. I’ve used 600×300 in size and added a blue radial gradient (#4584D4, #184D8F). Also, I added a black rectangle with 60% alpha and text to display an instructional message. Let’s take a look at the image.

””

Step 3: Creating the Main Shape

This trailer is composed of one simple shape which is duplicated and scaled when you move the mouse.

Select the Oval Tool, draw a 6×6 px circle and fill it with a radial gradient (#FFFFFF, #5CFAFF).

Convert this shape to a MovieClip add a Glow filter, use the values in the following image:

Convert this to a MovieClip and name it "LightBall", remember to check the "Export for ActionScript" option.

Step 4: ActionScript

Create a new ActionScript File (Command + N) and save it as "MouseTrailer.as"

Step 5: Import Necessary Classes

These are the classes we’ll need, if you need specific help with any of them please check the Flash help (F1).

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.

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.

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.

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.

private function startTrailer(e:MouseEvent):void
{

Step 10: Duplicating the LightBall

This code creates a new LightBall when the mouse is moved.

/* 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.

/* 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:

/* 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.

private function animate(e:Event):void
{

Step 14: Alpha

The alpha is reduced by 5% every frame.

/* Alpha */

e.target.alpha -= 0.05;

Step 15: Remove Invisible Objects

When the LightBall is no longer visible (alpha < 0) the object is removed.

/* 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.

/* Scale */

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

Step 17: Vertical Position

/* Y Position */

e.target.y += 3;
}

Step 18: Using the Class

Time to go back to the Fla.

Open the Properties Panel, add "MouseTrailer" as the Document Class and you’ll be ready to test your movie!

Conclusion

Now you have a nice looking Mouse Trailer wich you can customize however you want. Try changing the shape of the MovieClip, the size, the color – there are a lot of options! I hope you enjoyed this tut, thanks for reading.

  • Alexis

    How do you link the actionscript with the movie????????? please help!!!???

  • lily

    the tutorial is really great thanks, i was looking for this kind or tutorial for two days :) but now i have one problem after adding some other scripts to my animation Glowing Mouse disappear how can i fix it can anyone help? (this is first time i have used class)
    thanks in forward

  • http://www.actionscriptinstitute.com admec

    Hey!

    I have created an unique mouse trailer, u will love to see that.

    You can see many more effects on my site here: http://www.admecindia.co.in/spri2.html

    Thanks and code for that is:
    var spring = .1;
    var friction = .8;
    var gravity = 5;

    var ax = 0;
    var ay = 0;
    var dx = 0;
    var dy = 0;
    var mx = 0;
    var my = 0;
    var ex = 0;
    var ey = 0;
    var cx = 0;
    var cy = 0;

    this.onEnterFrame = function () {
    ax += (_xmouse-a._x)*spring;
    ay += (_ymouse-a._y)*spring;
    ay += gravity;
    ax *= friction;
    ay *= friction;
    a._x += ax;
    a._y += ay;
    //****************————
    dx += (a._x-d._x)*spring;
    dy += (a._y-d._y)*spring;
    dy += gravity;
    dx *= friction;
    dy *= friction;
    d._x += dx;
    d._y += dy;
    //****************————
    mx += (d._x-m._x)*spring;
    my += (d._y-m._y)*spring;
    my += gravity;
    mx *= friction;
    my *= friction;
    m._x += mx;
    m._y += my;
    //****************————
    ex += (m._x-e._x)*spring;
    ey += (m._y-e._y)*spring;
    ey += gravity;
    ex *= friction;
    ey *= friction;
    e._x += ex;
    e._y += ey;
    //****************————
    cx += (e._x-c._x)*spring;
    cy += (e._y-c._y)*spring;
    cy += gravity;
    cx *= friction;
    cy *= friction;
    c._x += cx;
    c._y += cy;

    this.createEmptyMovieClip(“myLine”, 200);
    myLine.lineStyle(1);
    myLine.moveTo(_xmouse, _ymouse)
    myLine.lineTo(a._x, a._y);
    myLine.lineTo(d._x, d._y);
    myLine.lineTo(m._x, m._y);
    myLine.lineTo(e._x, e._y);
    myLine.lineTo(c._x, c._y);

    }

    Thanks
    Ravi Bhadaurua
    ActionScript n Flex Programmer
    admec multimedia institute, Rohini, New Delhi

  • http://www.actionscriptinstitute.com Ravi

    hello,

    I come again with some doubts, actually i want to convert this as3 mouse trailer script to as2. but i am facing some problems.

    My script is:
    var obj = new Object();
    obj.onMouseMove = function() {
    createEmptyMovieClip(“s”, this.getNextHighestDepth());
    s.attachMovie(“bl”, “bll”, s.getNextHighestDepth());
    s._x = _root._xmouse+Math.random()*s._width;
    s._y = _root._ymouse-Math.random()*s._height;
    s._alpha -= 0.05;
    if (s._alpha <= 0){
    removeListener(obj);
    removeChild(s);
    }
    s._xscale -= 0.1;
    s._yscale -= 0.1;
    s._y += 3;
    }
    Mouse.addListener(obj);

    Thanks in advanced
    admec multimedia

  • mohamed

    thanks 4 this tutorial :)

    but how can i add a mouse event of url ??

    thanks

  • Pingback: Weeks 10 – 12 « bowiecube