Thursday, September 10, 2009

getDefinitionByName - Example in AS3.0



getDefinitionByName is a method in Actionscript 3.0 with the help of which you can access any class @ runtime. For better understanding of this method I have created this small app. In this example user can select any class from the combobox and we can dynamically add it on stage.

We need to create certain objects in library which we would be adding in the combobox. On selection from the combobox we will use getDefinitionByName(ClassName).

Document Class - DocMainCls.as - Download Here



package {

import flash.events.MouseEvent;

import flash.display.Sprite;

import flash.display.MovieClip;

import flash.text.TextField;

import com.GetDefinitionByName_example;


public class DocMainCls extends MovieClip{



private var objGetDefinitionByName_example:GetDefinitionByName_example = new GetDefinitionByName_example();

private var objsAddedOnStage:Array = new Array();

private var objCounter:uint = 0;



public function DocMainCls(){

init();

}

private function init(){

attachToStageBtn.addEventListener(MouseEvent.MOUSE_DOWN, attachBtnPressFunction);

}

private function attachBtnPressFunction(e:MouseEvent):void{

if(objToAddDropDown.selectedItem.data < 0)

{

msgBox.text = "Select Object To Add..."

}

else

{

objGetDefinitionByName_example.objToAddName = objToAddDropDown.selectedItem.data;

objsAddedOnStage.push(objGetDefinitionByName_example.getClipToAddOnStage());

myStage.addChild(objsAddedOnStage[objCounter]);

objsAddedOnStage[objCounter].objId = "StageObj"+objCounter;


objsAddedOnStage[objCounter].addEventListener(MouseEvent.MOUSE_DOWN, stageObjectDragFunction);

objsAddedOnStage[objCounter].addEventListener(MouseEvent.MOUSE_UP, stageObjectDropFunction);

msgBox.text = "Object on stage "+objGetDefinitionByName_example.objToAddName

objCounter++;

}

}


private function stageObjectDragFunction(e:MouseEvent):void{

e.target.startDrag();

}

private function stageObjectDropFunction(e:MouseEvent):void{

e.target.stopDrag();

}

}

}



The important method in above class is "attachBtnPressFunction". It is an event method. In this method

objGetDefinitionByName_example.objToAddName = objToAddDropDown.selectedItem.data;
objsAddedOnStage.push(objGetDefinitionByName_example.getClipToAddOnStage());
myStage.addChild(objsAddedOnStage[objCounter]);


The first line in above is setting a value for the object to be added. It is assiging class name from the combobox dropdown to the GetDefinitionByName_example class below.

GetDefinitionByName_example Class - GetDefinitionByName_example.as - Download Here

package com{

import flash.display.MovieClip;

import flash.utils.getDefinitionByName;



public class GetDefinitionByName_example{

private var _objToAddStage:String;

private var _objType:Class;

private var _objTypeInstance:MovieClip;



public function GetDefinitionByName_example(){

trace("Class -> GetDefinitionByName_example::::: constructor initiated...")

}

public function set objToAddName(_setVal:String):void

{

_objToAddStage = _setVal;

trace("Class -> GetDefinitionByName_example:::Method -> objToAddName::::"+ _objToAddStage)

}

public function getClipToAddOnStage():MovieClip

{

_objType = getDefinitionByName(_objToAddStage) as Class;

_objTypeInstance = new _objType as MovieClip

return _objTypeInstance;

}

public function get objToAddName():String

{

return _objToAddStage;

}

}

}

In the above class "objToAddName" It is a getter setter method which basically sets the class which the getDefinitionByName will access. We set the comboxBox dropdown values to be passed to this method.
The other most important method of this class is "getClipToAddOnStage()" this basically fetches the class and returns its
movieclip object which is further processed to add on stage.

This method is very important when we create dynamic attachment on stage or whenever we have requirement to access the
classes dynamically.

You can download the complete source files here

-Nitin

Using FormBuilder, Validators & FormGroup in Angular 2 to post data to Node server (node.js)

This blog explains usage of  FormBuilder , Validators , FormGroup classes in Angular 2 projects. In this post we will create HTML form a...