16Jun

Identifying A Class Object with Mouse Click Event’s currentTarget

No comments

I have noticed that the currentTarget from a MouseEvent driven function will produce the respective object that has called such function.

I have a class that will reproduce X number (in this case 10) of objects (from the c.as file -> just circle renderings) - I just simply wanted to identify each object that is created. To demonstrate, just click each circle in the movie below and the id of the circle will be output in the text window. Make sure you drop in the TextArea and UIScrollbar Components on to the stage and then delete them from the stage - we just do this to make sure the components are placed in our library.

Here is the new circle class, file named c.as:

package {
	/* this class will simply draw a circle */

	import flash.display.Sprite;

	public class c extends Sprite {
		private var radius:Number;
		private var color:uint;
		private var id:String;

		public function c(radius:Number=20, color:uint=0xb3d830, id:String="")
		{
			this.radius = radius;
			this.color = color;
			this.name = id;
			init();
		}

		public function init():void {
			graphics.beginFill(color);
			graphics.drawCircle(0, 0, radius);
			graphics.endFill();
		}
	}
}

The document class, file named CurrentTarget.as:

package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import fl.controls.TextArea;
    import fl.controls.ScrollPolicy;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;

	public class CurrentTarget extends Sprite
	{
		private var cArr:Array; // an array of all my circle objects
		private var output:TextArea;

		public function CurrentTarget()
		{
			init();
			Output();
		}

		private function init():void
		{
			// let's build 4 circles from our Circle class file -> c.as
			// we will pass in a random radius and a random color
			BuildCircles ( 10 );
		}

		private function BuildCircles ( i:Number ):void
		{
			cArr = new Array();

			for ( var ct:uint = 0; ct < i; ct++ )
			{
				// note: in the line below 'c' represents the class called 'c' - this class is in the file 'c.as'
				cArr.push ( new c ( (Math.random() * 20) + 10, Math.random() * 0xffffff, String(ct) ) );

				// add this object to the stage
				addChild ( cArr[ct] );

				// move this object to some point on the stage
				cArr[ct].x = Math.random() * stage.stageWidth;
				cArr[ct].y = Math.random() * (stage.stageHeight - 100); // minus 100 so the objects aren't underneath our output text box

				// add the event listener
				cArr[ct].addEventListener ( MouseEvent.MOUSE_DOWN, traceName );
			}
		}

		private function traceName(m:MouseEvent):void
		{
			Message ( "You clicked the object with name of: " + m.currentTarget.name );
		}

		// UI elements
        public function Output():void
        {
            // Small Black Text Formatting
            var smallBlackFormat:TextFormat = new TextFormat();
            smallBlackFormat.font = "Arial";
            smallBlackFormat.color = 0xffffff;
            smallBlackFormat.size = 10;
            smallBlackFormat.underline = false;  

            // output TextField
            output = new TextArea();
            output.verticalScrollPolicy = ScrollPolicy.ON;
            output.condenseWhite = true;
            output.setSize(700, 100);
            output.move(0,600);
            addChild(output);
        }  

        public function Message(m:String):void
        {
            output.appendText (m + "\n");
        }
	}
}

Share/Save/Bookmark

Categories: Actionscript 3, Arrays

Monday, June 16th, 2008 at 12:47 pm and is filed under Actionscript 3, Arrays. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply