Start Flash Application
Click CTRL + N (it means New File) and select Flash File(Action Script 3.0)
At the Properties Panel, set this :
Size : 450 x 300
Frame Rate : 25fps
Then, draw a circle on the stage . Make sure that the circle is black with no stroke.
------------------------------------------------------------------------------------------------------------------------
Ok, now you have a black circle on the stage. Next is to convert the circle into symbol. Press F8 or Right Click on the circle and choose “Convert to Symbol”.
Give the circle name : “blackcircle”
Wait, do not press OK yet. Hit the Advanced button to Check these:
Export for Action Script
Export in First Frame
And then name the class as : “colorsofcircle”
Make sure the base class is as follows : flash.display.MovieClip
And then click OK.
Now, save the file as circlecolors.fla (File->Save As)
now, click Ctrl + Enter. Can you see any animation? No, because there is no code to animate your circle…------------------------------------------------------------------------------------------------------------------------
Next is , click Ctrl + N or (File-> New) and select ActionScript File.
You will see a blank file. Here is where you put your code. Just copy and paste the code below and save the file as “colorsofcircle.as”,..
package {
import flash.display.*;
import flash.events.*;
import flash.geom.*;
public class colorsofcircle extends MovieClip {
var radians:Number;
var speed:Number;
var radius:Number;
var originalX:Number;
var originalY:Number;
static var colorArray:Array = new Array(0xFFFF33, 0xFFFFFF, 0x79DCF4, 0xFF3333, 0xFFCC33, 0x99CC33);
public function colorsofcircle() {
originalX = this.x;
originalY = this.y;
setProperties();
this.addEventListener(Event.ENTER_FRAME, flyCircleIn);
}
function setProperties()
{
speed = Math.ceil(5*Math.random());
radius = 20*Math.random();
radians = 0;
trace(speed);
this.scaleX = 0;
this.scaleY = 0;
var randomColorID:Number = Math.floor(Math.random()*colorArray.length);
var myColor:ColorTransform = this.transform.colorTransform;
myColor.color = colorArray[randomColorID];
this.transform.colorTransform = myColor;
this.alpha = 1;
}
function flyCircleIn(e:Event) {
radians += Math.abs(speed/10);
this.x += Math.round(radius*Math.cos(radians));
this.y += Math.round(radius*Math.sin(radians));
this.scaleX += Math.abs(speed/10);
this.scaleY += Math.abs(speed/10);
this.alpha -= Math.abs(speed/100);
if (this.alpha < 0)
{
resetCircle();
}
}
function resetCircle()
{
this.x = originalX;
this.y = originalY;
setProperties();
}
}
}
now go back to your circlecolors.fla and hit Ctrl + Enter .
can you see that your blackcircle now animates and changes color? If yes, then you are on the right track ! Ok now Right Click on your circle you created and press Copy . then, paste around 3 or 4 or 10 more circle on your stage but at different location as image below :
Press Ctrl + Enter . Can you see the animation created? :)
Okay.Below is the explanation of the actionscript code ...
---------------------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------------------
The colorsofcircle ClassEach of your circle movie clips are associated with the colorsofcircle class file, and all that code that is contained inside the colorsofcircle class file gets excecuted each time for each circle.
Each Individual Circle
Digging a bit deeper into each circle, the code does a few things on the circle:
- Sets the initial size, color, and position of each circle.
- Starts an enterFrame loop to create the animation.
- Scales the circle while fading it out at the same time.
- Resets the circle's size, color, and position once the circle completely fades out.
- package {
- import flash.display.*;
- import flash.events.*;
- import flash.geom.*;
- .
- .
- .
- .
- }
- public class colorsofcircle extends MovieClip {
- var radians:Number;
- var speed:Number;
- var radius:Number;
- var originalX:Number;
- var originalY:Number;
- static var colorArray:Array = new Array(0xFFFF33, 0xFFFFFF, 0x79DCF4, 0xFF3333, 0xFFCC33, 0x99CC33);
- public function ColorfulCircle() {
- originalX = this.x;
- originalY = this.y;
- setProperties();
- this.addEventListener(Event.ENTER_FRAME, flyCircleIn);
- }
- originalX = this.x;
- originalY = this.y;
- setProperties();
- this.addEventListener(Event.ENTER_FRAME, flyCircleIn);
This line wraps up the code found in our constructor. Like I mentioned earlier, the constructor is the gateway to the class. As such, it contains some pretty heavy-hitting code that calls other functions that are essential to what your class does. We'll look at some of those classes first - starting with the setProperties function.
- function setProperties()
- {
- speed = Math.ceil(5*Math.random());
- radius = 20*Math.random();
- radians = 0;
- trace(speed);
- this.scaleX = 0;
- this.scaleY = 0;
- var randomColorID:Number = Math.floor(Math.random()*colorArray.length);
- var myColor:ColorTransform = this.transform.colorTransform;
- myColor.color = colorArray[randomColorID];
- this.transform.colorTransform = myColor;
- this.alpha = 1;
- }
The setProperties function, as its name implies, sets the properties that your circle will posses. The first couple of lines simply set some properties for our circle's zoom speed, its radius, current radians value, and initial size:
- speed = Math.ceil(5*Math.random());
- radius = 20*Math.random();
- radians = 0;
- trace(speed);
- this.scaleX = 0;
- this.scaleY = 0;
The next series of lines are related to picking a random color:
- var randomColorID:Number = Math.floor(Math.random()*colorArray.length);
- var myColor:ColorTransform = this.transform.colorTransform;
- myColor.color = colorArray[randomColorID];
- this.transform.colorTransform = myColor;
The last line in setProperties is:
- this.alpha = 1;
- function flyCircleIn(e:Event) {
- radians += Math.abs(speed/10);
- this.x += Math.round(radius*Math.cos(radians));
- this.y += Math.round(radius*Math.sin(radians));
- this.scaleX += Math.abs(speed/10);
- this.scaleY += Math.abs(speed/10);
- this.alpha -= Math.abs(speed/100);
- if (this.alpha < 0)
- {
- resetCircle();
- }
- }
First, we increment our radians value by taking our speed value and dividing it by 10:
- radians += Math.abs(speed/10);
- this.x += Math.round(radius*Math.cos(radians));
- this.y += Math.round(radius*Math.sin(radians));
Now that we have the circular movement out of the way, let's now look at the lines of code that animate the circle's size and transparency:
- this.scaleX += Math.abs(speed/10);
- this.scaleY += Math.abs(speed/10);
- this.alpha -= Math.abs(speed/100);
When the circle fully fades out, we want to put an end to the animation in its current state and reset everything to a new set of properties:
- if (this.alpha < 0)
- {
- resetCircle();
- }
- function resetCircle()
- {
- this.x = originalX;
- this.y = originalY;
- setProperties();
- }
Conclusion
The preceding sections covered a lot of code, so let's use this moment to quickly recap what our code is doing.
- Each of our circles is like an independent life form with its own instance of ColorfulCircle powering it. First, the constructor gets called where the initial values of our circle are stored in various variables and our enter frame event is associated with our event handler.
- At each tick of the frame, 24 per second if you keep the default Flash CS4 value for frame rate, your circle gradually becomes larger, moves in a circular path, and becomes a bit less visible. Once your circle becomes fully invisible, we reset the circle back to a good starting point by calling the same function you called earlier for setting the initial properties.
- Even though we are calling the same function for setting the properties, the properties that get set will probably not be the same. There is a certain level of randomness that is introduced where the speed, radius, and color variables are set with the help of a Math.random call. This is nice because it keeps your animation a bit more unique and lively as each circle gets reset to a different variant of itself.
No comments:
Post a Comment