touch event

var isTouch = false;
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
isTouch = true;
}
if(isTouch) {
alert(“touch”)
$parent.on(“touchstart”,function(event) {
if($this.is(event.target)) {

if($parent.hasClass(‘open’)) {
$parent.removeClass(‘open’);

} else {
$parent.addClass(‘open’);
event.stopPropagation();
}
} else {

event.stopPropagation();
}
});

$(document).on(“touchstart”,function(event) {
if($parent.hasClass(‘open’)) {
$parent.removeClass(‘open’);
}
});

}

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.

Flash player 11 cubicCurveTo Example

package  
{
	import flash.display.Graphics;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Jeetendra Singh
	 */
	public class Main extends Sprite
	{
		private var control1:Sprite;
		private var control2:Sprite;
		
		public function Main() 
		{
			init();
		}
		
		private function init():void 
		{
			control1 = new Circle(); 
			control2 = new Circle();
			addChild(control1);
			addChild(control2);
			control1.x = 500;
			control1.y = 500;
			control2.x = 500;
			control2.y = 100;
			control1.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			control2.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			
			addEventListener(Event.ENTER_FRAME, render);
		}
		
		private function onMouseDown(e:MouseEvent):void 
		{
			Sprite(e.target).addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			Sprite(e.target).removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			Sprite(e.target).startDrag();
		}
		
		private function onMouseUp(e:MouseEvent):void 
		{
			Sprite(e.target).removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			Sprite(e.target).addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			Sprite(e.target).stopDrag();
		}
		
		
		
		private function render(e:Event):void 
		{
			var g:Graphics = this.graphics;
			g.clear();
			g.lineStyle(2, 0xff0000);
			g.moveTo(200, 200);
			g.cubicCurveTo(control1.x, control1.y, control2.x, control2.y, 400, 30);
		}
		
	}

}
// Helper Class
import flash.display.Sprite;
class Circle extends Sprite 
{
	public function Circle() 
	{
		this.graphics.beginFill(0xff00ff, 1);
		this.graphics.drawCircle(0, 0, 10);
	}
}

How to validate xml using AIR recursively in folder

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
					   xmlns:s="library://ns.adobe.com/flex/spark" 
					   xmlns:mx="library://ns.adobe.com/flex/mx"
					   status="{Capabilities.os}"
					   alwaysInFront="true"
					   >
	
	<fx:Script>
		<![CDATA[
			import mx.charts.CategoryAxis;
			import mx.events.FlexEvent;
			import mx.utils.ObjectUtil;
			
			private var file:File;
			private var opts:NativeWindowInitOptions;
			private var win:NativeWindow;
			private var label:int = 0;
			protected function button1_clickHandler(event:MouseEvent):void
			{
				file = new File();
				file.addEventListener(Event.SELECT,fileSelect);
				file.browseForDirectory("Select Directory");
				//file.browseForOpen("select file");
			}
			
			protected function fileSelect(event:Event):void
			{
				// TODO Auto-generated method stub
				lbl.text = file.nativePath;	
				var directory:Array = file.getDirectoryListing();
				for each (var f:File in directory)
				{
					if(f.isDirectory){
						recrsiveFolderSearch(f);
					} else {
						processFile(f);
					}
				}
			}
			
			protected function recrsiveFolderSearch(file:File):void
			{
				var directory:Array = file.getDirectoryListing();
				for each (var f:File in directory)
				{
					if(f.isDirectory){
						recrsiveFolderSearch(f)
					} else {
						processFile(f);
					}
				}
			}
			
			private function processFile(file:File):void
			{
				if(file.url.indexOf(".xml") != -1){
					
					try{
						var fileStream:FileStream = new FileStream();
						fileStream.open(file, FileMode.READ);
						var prefsXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
						fileStream.close();
						trace(file.nativePath)
					}catch(e:Error)
					{
						trace(e.message);
						trace("BUG : "+file.nativePath)
					}
					
				}
					
			}
			
			protected function button2_clickHandler(event:MouseEvent):void
			{
				// TODO Auto-generated method stub
				opts = new NativeWindowInitOptions()
				opts.type = NativeWindowType.LIGHTWEIGHT;
				win = new NativeWindow(opts);
				win.title = opts.type;
				win.activate();
			
			}
			
			/* protected function filesystemdatagrid1_creationCompleteHandler(event:FlexEvent):void
			{
				// TODO Auto-generated method stub
				fsDG.creationDateColumn.sortCompareFunction = createDateSortCompFunc("creationDate");
				fsDG.modificationDateColumn.sortCompareFunction = createDateSortCompFunc("modificationDate");
			} */
			
			private function createDateSortCompFunc(sortCol:String):Function {
				return function (obj1:File, obj2:File):int { 
					return ObjectUtil.dateCompare(obj1[sortCol], obj2[sortCol]);
				}
			}
			
		]]>
	</fx:Script>
	
	<fx:Declarations>
		<!-- Place non-visual elements (e.g., services, value objects) here -->
	</fx:Declarations>
	<mx:ApplicationControlBar dock="true">
		<s:Button label="Click to Browse" click="button1_clickHandler(event)" />
		<s:Button click="button2_clickHandler(event)" label="open window"/>
	</mx:ApplicationControlBar>
	<s:Label id="lbl" />
	<!--<mx:FileSystemDataGrid id="fsDG" sizeDisplayMode="bytes" 
						   directory="{File.userDirectory}"
						   creationComplete="filesystemdatagrid1_creationCompleteHandler(event)">
		
	</mx:FileSystemDataGrid>-->
</s:WindowedApplication>