Simple example to demonstrate Sprite class in Flash ActionScript 3.0

Advertisement
Post Pic

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:

  1. 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
  2. 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
  3. drawRect() is a method, which draws a rectangle. We must set the line style, fill, or both before you call the drawRect() method, by calling the linestyle() , beginFill() method.Actual Syntax for drawRect is:
    public function drawRect(x:Number, y:Number, width:Number, height:Number):void
  4. 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
  5. 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.
  6. rotation is a property, which indicates the rotation of the target object. Here the target object is ‘rectangle’.

Output:

Related Entries...

You Might Like:

Advertisement

Please let us know your feedback about the article through the comments section below, we want to hear from you about our website. And your comment / advice may help us to serve you better in upcoming articles.

Thank You!
Vivekanand [Founder of DeveloperSnippets]

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) [...]

Leave Your Response

* Name, Email, Comment are Required

Write For Us

Online Sponsors

Start Foreign Exchange Trading today with the Forex Affiliate Program and also you can have access Online Forex News to know what happens every single moment.


Forex Floor


Switch to our mobile site