Word Game: Arrange letters in correct order (Drag and Drop))

Hi,

Today i am posting a simple puzzle game it take me around 3.5-4 hrs to complete (strategy/coding/testing).

Below is complete code,

Main class (Document class)

package 
{
	import com.jeet.ui.Button;
	import com.jeet.ui.Drag;
	import com.jeet.ui.Drop;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;

	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class Main extends Sprite 
	{
		private var m_sWord:String = "DREAMLAND";
		private var m_oChars:Array;
		private var m_nCharCount:int;
		private var m_vDrags:Vector.;
		private var m_vDrops:Vector.;
		private var m_nDragsGap:Number = 10;
		private var m_nDropsGap:Number = 10;
		private var m_oButton:Button;
		public function Main():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}

		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point

			splitWord();
			createDragDrops();
			arrange()
			m_oButton = new Button();
			m_oButton.addEventListener(MouseEvent.CLICK,onClickHandler)
			addChild(m_oButton);
			m_oButton.enabled = false;
		}

		private function onClickHandler(e:MouseEvent):void 
		{
			var str:String = "";
			for (var i:int = 0; i < m_nCharCount; i++) 
			{
				str += m_vDrops[i].resident.getLabel();
			}

			m_oButton.enabled = false;
			for (var j:int = 0; j < m_nCharCount; j++) 
			{
				m_vDrags[j].enabled = false;
			}

			var tf:TextField = new TextField();
			tf.text = (str == m_sWord)? "Yes this is right" : "Sorry Correct answer is :" + m_sWord;
			tf.autoSize = "left";
			addChild(tf);
			tf.x = 200;
			tf.y = 200;

		}

		private function splitWord():void 
		{
			m_oChars = m_sWord.split("");
			m_nCharCount = m_oChars.length;
			randomize();
		}

		private function randomize():void 
		{
			for ( var i:int = 0; i < m_nCharCount; i++ )
			{
				var random:int = int( Math.random() * m_nCharCount );
				var char:String = m_oChars[random];
				m_oChars[random] = m_oChars[i];
				m_oChars[i]      = char;
			}
		}

		private function createDragDrops():void 
		{
			m_vDrags = new Vector.(m_nCharCount);
			m_vDrops = new Vector.(m_nCharCount);

			for (var i:int = 0; i < m_nCharCount; i++) 
			{
				m_vDrags[i] = addChild(new Drag()) as Drag;
				m_vDrops[i] = addChild(new Drop()) as Drop;

				m_vDrags[i].setLabel(m_oChars[i]);
				m_vDrags[i].addEventListener("itemDroped", onDroped);
			}

		}

		/**
		 * 
		 * @param	e
		 */
		private function onDroped(e:Event):void 
		{
			// check for hitTest
			var drag:Drag = (e.target as Drag);
			var drop:Drop;
			for (var i:int = 0; i < m_nCharCount; i++) 
			{
				if (drag.hitTestObject(m_vDrops[i])) {
					drop = m_vDrops[i];
					break;
				}
			}
			if (drop) {
				/// hittest success
				if (drop.resident)
				{
					// drop filled already
					if (drag.landlord) {
						// swaping required
						var lastDrop:Drop = drag.landlord;
						var lastDrag:Drag = drop.resident
						setDragOnDrop(lastDrag, lastDrop);
						setDragOnDrop(drag,drop);
					} else {
						// send old drag to origal position
						drop.resident.x = drop.resident.orignalPos.x;
						drop.resident.y = drop.resident.orignalPos.y;
						drop.resident.landlord = null;
						drop.resident = null;
					}
				} else {
					// drop is empty
					if (drag.landlord) {
						// empty last drop
						drag.landlord.resident = null;
						drag.landlord = null
						setDragOnDrop(drag,drop);
					} else {
						// both are new 
						setDragOnDrop(drag,drop);
					}
				}

			} else {
				// hittest Fail send to initial position
				if (drag.landlord) {
					drag.landlord.resident = null;
					drag.landlord = null;
				}
				drag.x = drag.orignalPos.x;
				drag.y = drag.orignalPos.y;
			}
			checkAllDroped()

		}

		private function checkAllDroped():void 
		{
			var success:Boolean = true;
			for (var i:int = 0; i < m_nCharCount; i++) 
			{
				if (m_vDrops[i].resident == null) {
					success = false;
					break;
				}
			}
			if (success) {
				m_oButton.enabled = true;
			} else {
				m_oButton.enabled = false;
			}
		}

		private function setDragOnDrop(drag:Drag, drop:Drop):void 
		{
			drag.x = drop.x + (drop.width - drag.width) / 2; 
			drag.y = drop.y + (drop.height - drag.height) / 2;
			drop.resident = drag;
			drag.landlord = drop;
		}
		/**
		 *   
		 */ 
		private function arrange():void 
		{
			var prevDrag:Drag = m_vDrags[0];
			var prevDrop:Drop = m_vDrops[0];

			prevDrag.x = 50;
			prevDrag.y = 400;
			prevDrop.x = 50;
			prevDrop.y = 50;

			prevDrag.orignalPos = new Point(prevDrag.x, prevDrag.y)

			for (var i:int = 0; i < m_nCharCount; i++) 
			{
				m_vDrags[i].x = prevDrag.x + prevDrag.width + m_nDragsGap
				m_vDrags[i].y = prevDrag.y;
				m_vDrops[i].x = prevDrop.x + prevDrop.width + m_nDropsGap;
				m_vDrops[i].y = prevDrop.y;
				m_vDrags[i].orignalPos = new Point(m_vDrags[i].x, m_vDrags[i].y);
				prevDrag = m_vDrags[i];
				prevDrop = m_vDrops[i];

			}
		}
	}

}

//—————————————–
Drag Class
//—————————————–

package com.jeet.ui 
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFormat;
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class Drag extends BaseComponent
	{
		public var landlord:Drop;
		private var m_oTF:TextField;
		private var m_oBG:MovieClip;
		private var m_oTFormat:TextFormat;
		private var m_oPoint:Point
		private var m_oOrignalPos:Point;
		private var m_oCurrentPos:Point;
		public function Drag() 
		{
			super();
		}

		override protected function init(e:Event):void 
		{
			super.init(e);

			/*m_oOrignalPos = new Point(this.x,this.y)
			m_oCurrentPos = new Point(this.x,this.y)*/

			if (!m_oBG) {
				m_oBG = new MovieClip();
				addChild(m_oBG);
				m_oBG.x = 0;
				m_oBG.y = 0;
				var g:Graphics = m_oBG.graphics;
				g.lineStyle(1, 0x000000);
				g.beginFill(0xffffff);
				g.drawRoundRect( 0, 0, 30, 30, 25, 25);
				g.endFill();
			}

			if (!m_oTF) {
				m_oTF = new TextField();
				m_oTF.autoSize = "left"
				m_oTFormat = new TextFormat("Ariel", 14, 0xff0000);
				m_oTFormat.align = "right";

				m_oTFormat.bold = true;
				m_oTF.defaultTextFormat = m_oTFormat;

				//m_oTF.border = true;

				addChild(m_oTF);
			}
			//setLabel("J");
			alignText()
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler)
		}

		private function mouseDownHandler(e:MouseEvent):void 
		{
			m_oPoint = new Point(e.localX, e.localY);
			if (this.parent) {
				this.parent.addChild(this);
			}
			e.stopImmediatePropagation();
			this.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler)
			this.addEventListener(Event.ENTER_FRAME,enterFrameHandler)
			this.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler)
		}

		private function mouseUpHandler(e:MouseEvent):void 
		{
			dispatchEvent(new Event("itemDroped"));
			m_oPoint = null;
			e.stopImmediatePropagation();
			this.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
			this.removeEventListener(Event.ENTER_FRAME,enterFrameHandler)
			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler)
		}

		private function enterFrameHandler(e:Event):void 
		{
			this.x = stage.mouseX - m_oPoint.x;
			this.y = stage.mouseY - m_oPoint.y;
		}

		public function setLabel(p_sChar:String):void
		{
			if (p_sChar.length == 1) {
				m_oTF.text = p_sChar;
				alignText();
			} else {
				// SHOW ERROR MESSAGE HERE
			}
		}

		public function getLabel():String 
		{
			return m_oTF.text
		}

		private function alignText():void 
		{
			m_oTF.x = m_oBG.x + ( (m_oBG.width - m_oTF.width)  /2 );
			m_oTF.y = m_oBG.x + ( (m_oBG.height - m_oTF.height)  /2 );
		}

		override public function destruct(e:Event = null):void 
		{
			super.destruct(e);
			m_oOrignalPos = null;
			m_oCurrentPos = null;
			m_oPoint      = null;
			m_oTFormat    = null;

			this.removeChild(m_oTF);
			m_oTF = null;

			this.removeChild(m_oBG);
			m_oBG = null;
		}

		public function get orignalPos():Point 
		{
			return m_oOrignalPos;
		}

		public function set orignalPos(point:Point):void 
		{
			m_oOrignalPos = point;
		}

	}

}

//——————————————–
Drop Class
//——————————————–

package com.jeet.ui 
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.events.Event;
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class Drop extends BaseComponent
	{
		public var isFilled:Boolean = false;
		public var resident:Drag;
		private var m_oBG:MovieClip;

		public function Drop() 
		{
			super()
		}

		override protected function init(e:Event):void 
		{
			super.init(e);
			if (!m_oBG) {
				m_oBG = new MovieClip();
				addChild(m_oBG);
				m_oBG.x = 0;
				m_oBG.y = 0;
				var g:Graphics = m_oBG.graphics;
				g.lineStyle(1, 0xff0000);
				g.beginFill(0x000000);
				g.drawRoundRect( 0, 0, 40, 40, 35, 35);
				g.endFill();
			}
		}

		override public function destruct(e:Event = null):void 
		{
			super.destruct(e);
			this.removeChild(m_oBG);
			m_oBG = null;
		}

	}

}

//——————————————-
Button Classs
//——————————————-

package com.jeet.ui 
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class Button extends BaseComponent 
	{
		private var m_oTF:TextField;
		private var m_oNormalBG:MovieClip;
		private var m_oOverBG:MovieClip;
		private var m_oDisableBG:MovieClip;
		private var m_oTFormat:TextFormat;
		public function Button() 
		{
			super();
		}
		
		override protected function init(e:Event):void 
		{
			super.init(e);
			
			if (!m_oDisableBG) {
				m_oDisableBG = new MovieClip();
				createSkin(m_oDisableBG,0xdddddd)
			}
			
			if (!m_oOverBG) {
				m_oOverBG = new MovieClip();
				createSkin(m_oOverBG,0xeeeeee)
			}
			
			if (!m_oNormalBG) {
				m_oNormalBG = new MovieClip();
				createSkin(m_oNormalBG,0xffffff)
			}
			
			if (!m_oTF) {
				m_oTF = new TextField();
				m_oTF.autoSize = "left"
				m_oTFormat = new TextFormat("Ariel", 14, 0xff0000);
				m_oTFormat.bold = true;
				m_oTF.defaultTextFormat = m_oTFormat;
				addChild(m_oTF);
				m_oTF.text = "Check Answer";
				
			}
			arrange()
			
			addEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
		}
		
		private function onMouseOverHandler(e:MouseEvent):void 
		{
			if (!enabled) return;
			hideAllSkins();
			m_oOverBG.visible = true;
		}
		
		private function hideAllSkins():void 
		{
			m_oNormalBG.visible = false
			m_oOverBG.visible = false;
			m_oDisableBG.visible = false;
		}
		
		private function onMouseOutHandler(e:MouseEvent):void 
		{
			if (!enabled) return;
			hideAllSkins();
			m_oNormalBG.visible = true;
		}
		
		override public function get enabled():Boolean 
		{
			return super.enabled;
		}
		
		override public function set enabled(value:Boolean):void 
		{
			hideAllSkins();
			if (value) {
				m_oNormalBG.visible = true;
			} else {
				m_oDisableBG.visible = true;
			}
			
			super.enabled = value;
		}
		
		private function arrange():void 
		{
			if(m_oNormalBG && m_oTF){
				m_oTF.x = m_oNormalBG.x + (m_oNormalBG.width - m_oTF.width) / 2;
				m_oTF.y = m_oNormalBG.y + (m_oNormalBG.height - m_oTF.height) / 2;
			}
		}
		
		private function createSkin(p_oBG:MovieClip, color:uint):void 
		{
			addChild(p_oBG);
			p_oBG.x = 0;
			p_oBG.y = 0;
			var g:Graphics = p_oBG.graphics;
			g.lineStyle(1, 0x000000);
			g.beginFill(color);
			g.drawRoundRect( 0, 0, 120, 40, 20, 20);
			g.endFill();
		}
		
		override public function destruct(e:Event = null):void 
		{
			super.destruct(e);
			removeEventListener(MouseEvent.MOUSE_OVER, onMouseOverHandler);
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
		}
		
	}

}

//——————————————-
Component Classs
//——————————————-

package com.jeet.ui 
{
	import flash.display.MovieClip;
	import flash.events.Event;

	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class BaseComponent extends MovieClip 
	{

		public function BaseComponent() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
			mouseChildren = false;
		}

		protected function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			addEventListener(Event.REMOVED_FROM_STAGE, destruct);
		}

		public function destruct(e:Event = null):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, destruct);
		}

	}

}

//---------------------------------------

Hope you enjoy it.

Flash Develop for Flash Player 11 compilation

first download flash develop and flex sdk and flash player and playerglobal.swc

now extract flexsdk in your hard drive and paste playerglobal.swc in “frameworks\libs\player\11.0\”

Install Flash develop by double clicking its exe file,

Create new project,

Go to Project>Properties>Output>Platform. Change Target to Flash Player 11.0.
In Project>Properties>Compiler Options >advanced, change Additional Compiler Options to “-swf-version=13” (without quotes)

Go to Project>Properties>Output>Test Movie. Select Open document from dropdown list. Click Edit button and type “bin\index.html” (without quotes)

now edit bin\index.html and change wmode to “direct”. for more detail click this link.

Using Keyboard Event and dynamic textfield in actionscript 3.0

This is a basic example of keyboard Event and Dynamic textfield.I also want to share you a handful shortcur which i found today using “Flash Builder Burrito”, that is “CTRL + SHIFT + D”, to use just click on top of your function Definition and press this shortcut this will create a multiline comment block for commenting function,  

 package

 {

      import flash.display.MovieClip;

      import flash.events.KeyboardEvent;

      import flash.text.TextField;

      public class KeyBoardEventTest extends MovieClip

      {

           private var outputText:TextField;

           public function KeyBoardEventTest()

           {

                super();

                init()

           }

           /**

            * This function Initilize Event

            */

           protected function init():void

           {

                // creating a dynamic textfield for key logging

                outputText = new TextField();

                outputText.x = 0

                outputText.y = stage.stageHeight >> 1;

                outputText.width = stage.stageWidth;

                outputText.height = stage.stageHeight >> 1;

                outputText.border = true;

                outputText.selectable = true;

                outputText.multiline = true;

                outputText.wordWrap = true;

                addChild(outputText);

                stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);

                stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);

           }

           /**

            * Listener for Keyboard KEY_DOWN Event

            * @param e

            * 

            */          

           protected function onKeyDown(e:KeyboardEvent):void

           {

                trace("onKeyDown # keyCode :"+e.keyCode+", charCode:"+e.charCode);

           }

           /**

            * Listener for Keyboard KEY_UP Event

            * @param e

            * 

            */          

           protected function onKeyUp(e:KeyboardEvent):void

           {

                trace("onKeyUp # keyCode :"+e.keyCode+", charCode:"+e.charCode);

           }

           /**

            * Custom trace function 

            * @param value

            * 

            */          

           private function trace(value:*):void

           {

                outputText.appendText("\n"+String(value));

           }

      }

 }

unfamiliar typecast issue of array

HI,

Today i am working on a project in which i want to insert an array value to a textfield i use this syntex

tf.text = Array(getValues()).join(“,”);

function getValues():Array
{
//return Array
return new Array(“a”,”s”,”d”,”f”);
}

i got a warning :
Array(x) behaves the same as new Array(x). To cast a value to type Array use the expression x as Array instead of Array(x).

so next time if you want to typecast array use

tf.text = (getValues() as Array).join(“,”);

integer comparision with NaN

Hi,
You may be shocked while comparing a integer variable with NaN like this because default value of a integer is 0

var a:int;

if(a == NaN)
{
// code when a is not initilize
}

This will not provide desired result.


var a:int;
if(isNaN(a))
{
// code when a is not initilize
}

This will also not provide desired result.

Try this code

var a:int;
trace(a);//0
a = 10;
trace(a)//10
a = undefined;
trace(a)//0

Moral of the story: Integer number does not have NaN.

for more info see lukasz comment below

Moving balls

I did change in my previous example and make Balls moveable
  
 
  
 package 
  
 {
  
     import flash.display.Graphics;
  
     import flash.display.MovieClip;
  
     /**
  
      * ...
  
      * @author Jeet Chauhan
  
      */
  
     public class BallWithParameter extends MovieClip
  
     {
  
         
  
         private var color:uint;
  
         private var radius:Number;
  
         
  
         public function BallWithParameter(radius:Number = 10,color:uint=0xffff00) 
  
         {
  
             this.color = color;
  
             this.radius = radius;
  
             draw()
  
         }
  
         
  
         private function draw():void
  
         {
  
             var g:Graphics = graphics;
  
             g.beginFill(color, 1);
  
             g.drawCircle(0, 0, radius);
  
         }
  
         /**
  
          * this will set position of current instance
  
          * @param    dx
  
          * @param    dy
  
          */
  
         public function setPosition(dx:Number, dy:Number):void
  
         {
  
             this.x = dx;
  
             this.y = dy;
  
         }
  
         
  
         /**
  
          * this will move current instance 
  
          * @param    dx
  
          * @param    dy
  
          */
  
         public function moveBy(dx:Number, dy:Number):void
  
         {
  
             this.x += dx;
  
             this.y += dy;
  
         }
  
         
  
     }
  
 
  
 }
  
 
  
 
  
 
  
 package 
  
 {
  
     import flash.display.MovieClip;
  
     import flash.events.Event;
  
     
  
     /**
  
      * ...
  
      * @author Jeet Chauhan
  
      */
  
     public class MultipleBall extends MovieClip
  
     {
  
         private var balls:Array;
  
         public function MultipleBall() 
  
         {
  
             draw()
  
         }
  
         
  
         private function draw():void
  
         {
  
             balls = new Array();
  
             for (var i:int = 0; i &lt; 100; i++) 
  
             {
  
                 var ball:BallWithParameter = new BallWithParameter(Math.random() * 5 + 5, Math.random() * 0xffffff);
  
                 balls.push(ball);
  
                 //ball.setPosition(Math.random() * stage.stageWidth,Math.random() * stage.stageHeight)
  
                 ball.x = Math.random() * stage.stageWidth;
  
                 ball.y = Math.random() * stage.stageHeight;
  
                 addChild(ball);
  
             }
  
             stage.addEventListener(Event.ENTER_FRAME, animate);
  
         }
  
         
  
         private function animate(e:Event):void 
  
         {
  
             for (var i:int = 0; i &lt; 100; i++) 
  
             {
  
                 BallWithParameter(balls[i]).moveBy(Math.random() * 10 - 5, Math.random() * 10 - 5);
  
             }
  
         }
  
         
  
     }
  
 
  
 }  

Second Class


This is improved version of previous post this is more reusable then previous post.
package  
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class BallWithParameter extends MovieClip
	{
		private var color:uint;
		private var radius:Number;
		
		public function BallWithParameter(radius:Number = 10,color:uint=0xffff00) 
		{
			this.color = color;
			this.radius = radius;
			draw()
		}
		
		private function draw():void
		{
			var g:Graphics = graphics;
			g.beginFill(color, 1);
			g.drawCircle(0, 0, radius);
		}
		
	}

}

This class initilize random color and radius ball use it as a document class

package  
{
	import flash.display.MovieClip;
	
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class MultipleBall extends MovieClip
	{
		
		public function MultipleBall() 
		{
			draw()
		}
		
		private function draw():void
		{
			var ball:BallWithParameter = new BallWithParameter(Math.random() * 5 + 5, Math.random() * 0xffffff);
			ball.x = Math.random() * stage.stageWidth;
			ball.y = Math.random() * stage.stageHeight;
			addChild(ball);
		}
		
	}

}

this will create multiple circles

package  
{
	import flash.display.MovieClip;
	
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class MultipleBall extends MovieClip
	{
		
		public function MultipleBall() 
		{
			draw()
		}
		
		private function draw():void
		{
			for (var i:int = 0; i < 100; i++) 
			{
				var ball:BallWithParameter = new BallWithParameter(Math.random() * 5 + 5, Math.random() * 0xffffff);
				ball.x = Math.random() * stage.stageWidth;
				ball.y = Math.random() * stage.stageHeight;
				addChild(ball);
			}
			
		}
		
	}

}

Basic AS3 Class Example


This is a simplest example,I hope this will help you to learn as3 and class concept, I love to code and want to share my code with other who want to learn as3 so i am starting a series or tutorials from basic to advanced level

package  
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class Ball extends MovieClip
	{
		
		public function Ball() 
		{
			draw()
		}
		
		private function draw():void
		{
			var g:Graphics = graphics;
			g.beginFill(0xff0000, 1);
			g.drawCircle(10, 10, 10);
		}
		
	}

}
In this example we create a class named Ball, which extends MovieClip class, In flash every object which have physical instance must extend MovieClip or Sprite Class,Sprite Class is super class of MovieClip, 

then we get graphics property of MovieClip class

var g:Graphics = graphics;

and then set color to fill

g.beginFill(0xff0000, 1);

then draw a circle on it   

g.drawCircle(10, 10, 10);

create dynamic line using graphics class

This class simply create a line using Graphics class lineTo and moveTo methods hope this will help someone


package 
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author Jeet Chauhan
	 */
	public class drawLine extends Sprite 
	{
		// variable to hold graphics class instanve
		private var g:Graphics;
		
		public function drawLine():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			g = graphics
			// this will set line thickness from 1 to 4 and a random color
			g.lineStyle(int(Math.random() * 3) + 1, Math.random() * 0xff0000);
			// this will moves the current drawing position to (50,50)
			g.moveTo(50, 50);
			draw();
		}
		
		private function draw():void
		{
			//Draws a line from the current drawing position to (500, 500).
			g.lineTo(500, 500);
		}
		
	}
	
}

as3 increment and decrement operator

What will be the result of snippet in AS3 ,Guess?

var a:int = 1;
a = ++a + a++ + a;
a = a– + –a;
a = ?

Solution

var a:int = 1;
// a == 1
a = ++a + a++ + a;
// ++a is pre increment operator(first increment then assignment) and a++ is post increment operator(first assignment then increment) as3 compiler parse left to right so first ++a then a++ and finally a so result become
// 2 + 2 + 3 = 7

a = a– + –a;
// a– is post decrement operator and –a is pre decrement operator
// 7 + 5 = 12

a = ? // 12

there is a Gotcha ,if you use c concept then you will give wrong answer, because as3 parse From left to right (opposite to c language)
if have any confusion please comment i will try to answer