The Sprite class is new in ActionScript 3.0, This class is a basic display list building block, which can display graphics and can also contain childrens. Depending on the Sprite class we can create a Sprite object, this Sprite object is similar to a movie clip, but does not have a timeline. This is an appropriate base class for objects that do not require timelines.
To demonstrate the same, here in this article I am posting one simple application which makes use of Sprite class.
Flash Application: In the first frame, press F9 to open up ActionScript editor window, Copy and paste the code and save it as rotateRectObj.fla
Respective Code:
//Creating an object rectangle of type Sprite
var rectangle:Sprite = new Sprite();
rectangle.graphics.lineStyle(0,0x7B7B7B); //lineStyle(thickness, color)
rectangle.graphics.beginFill(0xA6A6A6); //beginFill(color)
rectangle.graphics.drawRect(0,0,60,60); //drawRect(x,y,width,height)
rectangle.graphics.endFill(); //endfill
var dot:Shape = new Shape(); //dot is a Shape Object
dot.graphics.lineStyle(0,0xFF0000); //lineStyle(thickness, color)
dot.graphics.beginFill(0xFF0000); //beginFill(color)
dot.graphics.drawCircle(0,0,2); //drawCircle(x,y,radius)
dot.graphics.endFill(); //endfill
rectangle.addChild(dot); //add the dot object to the Rectangle
dot.x = 1; //x position -> 1
dot.y = 1; //y positin -> 1
this.addChild(rectangle); //adding a rectangle to the stage
rectangle.x = 200; //x position -> 200
rectangle.y = 200; //y position -> 200
/*
adding an EventListener to the stage, and here we are calling 'rotateRectObject'
function on when ENTER_FRAME is fired.
*/
stage.addEventListener(Event.ENTER_FRAME, rotateRectObject);
function rotateRectObject(evt:Event):void {
//rotation is a property to rotate the respective object
rectangle.rotation+=10;
}Once done with the saving of the file, then click ctrl+Enter to preview the same.
Explanation of the Code:
My requirement in this application is to rotate an Rectangle Object clock wise. For this I have created an Sprite object named ‘rectangle’ and to this object I have added some additional graphics like ‘lineStyle’, beginFill, drawRect, endFill
Methods and Properties used in this example:
linestyle()is a method, Which specifies a line style like thickness , color for the object. In our example we have written like this:rectangle.graphics.lineStyle(0,0x7B7B7B); // Here 0 is the thickness and 0x7B7B7B is the color, I have choosen my own colorActual Syntax for lineStyle is:public function lineStyle(thickness:Number, color:uint = 0, alpha:Number = 1.0, pixelHinting:Boolean = false, scaleMode:String = "normal", caps:String = null, joints:String = null, miterLimit:Number = 3):void
beginFill()is a method, which Specifies a simple one-color fill, Flash Player does not render the fill until the endFill method is called.Actual Syntax for beginFill is:public function beginFill(color:uint, alpha:Number = 1.0):void
drawRect()is a method, which draws a rectangle. We must set the line style, fill, or both before you call thedrawRect()method, by calling thelinestyle(),beginFill()method.Actual Syntax for drawRect is:public function drawRect(x:Number, y:Number, width:Number, height:Number):void
addChild()is a method, which adds a child DisplayObject instance to this DisplayObjectContainer instance.Actual Syntax for addChild is:public function addChild(child:DisplayObject):DisplayObject
addEventListener()is a method, which adds respective event to the object. Like Event.ENTER_FRAME, MouseEvent.CLICK etc., and when event has fired call a respective function, like in this example I am calling rotateRectObject function.- rotation is a property, which indicates the rotation of the target object. Here the target object is ‘rectangle’.
Output:
Related Entries...
John Resig, has explained us a better and interesting way of Method Overloading. Here he has discusse ...
Making a preloader in flash is very simple. By using ProgressBar and Image Loader components, we can ...
Hi Dev Folks, this topic explains you about different patterns used to match character combinations i ...
AIR is rocking in the web world, if you guys! Search for Adobe AIR you will get tons of web snippets ...
I am back again with some helpful snippet code. Here using JavaScript we can pro actively sort date f ...
Hey folks, hmmm... How can I start.... !!! Yeah, well folks... below is very simple snippet code for ...
As we all know AJAX is rocking the Web World all around, just I had a thought to developer a simple A ...
You guys might know the importance of XML, XML (Extensible Markup Language) is a flexible markup lang ...
Hi Guys, I would like to share a JavaScript snippet code which will remove special characters (like ! ...
As you guys already know that Adobe Flash CS3 is awesome enough, in CS3 we can see more features and ...






















One Response
[...] Simple example to demonstrate Sprite class in Flash ActionScript 3.0 Sothink SWF Decompiler for Mac supports Flash CS3 (9.0) and ActionScript 3.0 Binding XML Data using XMLConnector (Flash Application) [...]