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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s