Web Design Tutorials: Photoshop, HTML, Flash, PHP

Flash Tutorial – Random Boxes Text Effect

In this tutorial I will show you how you can create a simple Flash text effect when the mouse hovers over the text. This is quite easy to accomplish with a little help from ActionScript 3.

Setting up the environment

1. Create a new Flash document of size 250×250.

2. First, create a static text field and type some text in it (e.g. FlashMyMind). Position the text in the center of the stage.

3. Convert the text field to a movie clip. Name it “My Text” and set the registration point to the top left corner. My movie clip ended up looking the following.

Flash text

4. Give the movie clip an instance name of “myText“.

5. Now draw a rectangle of size 10×10 to the stage. Use some stroke e.g. 2 pixels. The colors won’t matter since we will manipulate it with ActionScript 3.

6. Convert the rectangle to a movie clip. Name it “My Rectangle” and set the registration to the center.

7. Linkage the movie clip to a class named “MyRectangle“. If you don’t know how to linkage movie clips, please see step 4 from the External Classes tutorial.

8. Remove the rectangle movie clip from the stage.

Moving to ActionScript 3

9. Create a new layer for ActionScript and type the following.

//We use a timer to create the rectangles when the mouse is over the text.
//This timer will send an event every 0.02 seconds.
var timer:Timer = new Timer(20, 0);
timer.addEventListener(TimerEvent.TIMER, createRectangle);

//We want to know wheter the mouse is over the text or not
var mouseOverText:Boolean = false;

//Make the text look like a button (hand cursor appears on mouse hover)
myText.buttonMode = true;

//Listen when the mouse is over the text
myText.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);

//Listen when the mouse moves out from the text
myText.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);

//This function is called when the mouse is over the text
function mouseOverHandler(e:Event):void {

	//Start the timer
	timer.start();
}

//This function is called when the mouse moves out from the text
function mouseOutHandler(e:Event):void {

	//Reset the timer
	timer.reset();
}

//This function is called every 0.02 seconds when the timer is running
function createRectangle(e:Event):void {

	//Create a new rectangle
	var rectangle:MyRectangle = new MyRectangle();

	//Assign a random x position to the rectangle
	rectangle.x = myText.x + Math.random() * myText.width;

	//Assign the y position to the rectangle
	rectangle.y = myText.y + myText.height / 2;

	//Assign a random scale for the rectangle
	rectangle.scaleX = rectangle.scaleY = Math.random() * 2;

	//Assign a random x and y speed for the rectangle
	rectangle.xspeed = Math.random() * 10 - 5;
	rectangle.yspeed = Math.random() * 10 - 5;

	//Assign a random alpha speed (how fast the rectangle disappears)
	rectangle.alphaSpeed = -(Math.random() * 0.1);

	//Assign a random scale speed
	rectangle.scaleSpeed = Math.random() * 0.05;

	//Assign a random color for the rectangle
	var colorInfo:ColorTransform = rectangle.transform.colorTransform;
	colorInfo.color = 0xffffff * Math.random();
	rectangle.transform.colorTransform = colorInfo;

	//We don't want to catch any mouse events from the rectangle
	rectangle.mouseEnabled = false;

	//Add ENTER_FRAME listener for the rectangle
	rectangle.addEventListener(Event.ENTER_FRAME, animate);

	//Add the rectangle to the stage.
	//We add it to index zero because we want the rectangle to stay behind the text.
	addChildAt(rectangle,0);
}

//This function is called in each frame
function animate(e:Event):void {

	//Save the rectangle to a local variable
	var rectangle:MyRectangle = (MyRectangle)(e.target);

	//Update its position
	rectangle.x += rectangle.xspeed;
	rectangle.y += rectangle.yspeed;

	//Update the alpha
	rectangle.alpha += rectangle.alphaSpeed;

	//Update the scale
	rectangle.scaleX += rectangle.scaleSpeed;
	rectangle.scaleY += rectangle.scaleSpeed;

	//Assign a random rotation for the rectangle
	rectangle.rotation = Math.random() * 256;

	//Remove the rectangle from the stage if alpha is below 0
	if(rectangle.alpha < 0) {
		rectangle.removeEventListener(Event.ENTER_FRAME, animate);
		removeChild(rectangle);
	}
}

10. That’s it, test your Flash movie!

Source: tutorials.flashmymind.com

you may also like:

Leave a Reply

Powered by WordPress | Designed by Elegant Themes