Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

Dynamically adding a method to an Object with Actionscript 3.0

This is a discussion on Dynamically adding a method to an Object with Actionscript 3.0 within the Tutorials forums, part of the Flash English category; As we have seen in Actionscript basic tutorials , we can add a property to objects at runtime , dynamically. In this ...


Go Back   Forum Flash CS3 Flash CS4 > Flash CS3 Flash CS4 > Flash English > Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 14-10-08, 06:26
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,535
Rep Power: 6
Flep is on a distinguished road
Dynamically adding a method to an Object with Actionscript 3.0

As we have seen in Actionscript basic tutorials, we can add a property to objects at runtime, dynamically.

In this tutorial we'll see that you can also dynamically implement methods to objects (in this case the class Object).


The code that we will see is just an object, must be an instance of a dynamic class (in fact, the Object class of Actionscript 3.0 is dynamic), may have properties and methods implemented by us dynamically.

Very useful when you load XML files associating XML data to an object.


Let's see some examples ...


Example 1


Code:
var miles:Object=new Object();
miles.surname="davis";
trace(miles.surname);
miles.getSurname=function():String
{
	return this.surname;
}

trace(miles.getSurname());

In this case I implement a property surname to an Object miles.

I can assign the value of such property directly (as the first trace):


Code:
trace(miles.surname);

or by implementing a method ( function ) at the same object that returns value of surname property of miles object.


Code:
miles.getSurname=function():String
{
	return this.surname;
}

trace(miles.getSurname());

Note that I used "this" to refer at property surname of the object from inside of the method.

If you do not use "this" Flash finds a global variable named surname ( there is not ) and gives an error.


Example 2


Code:
var miles:Object=new Object();
miles.surname="davis";
miles.sex="male";
miles.music="jazz";

miles.getInfo=function():String
{
	return("miles "+this.surname+" is "+this.sex+" and plays "+this.music);
}

trace(miles.getInfo());

In this case I implement a method called getInfo to object miles that returns a concatenation of strings made on the properties of object miles.

Like the following trace does:


Code:
trace(miles.getInfo());

Example 3


Code:
var miles:Object=new Object();
miles.surname="davis";
miles.sex="male";
miles.music="jazz";
miles.skin=function():uint
{
	return skinColor("black");
}

var jaco:Object=new Object();
jaco.surname="pastorius";
jaco.sex="male";
jaco.music="fusion";
jaco.skin=function():uint
{
	return skinColor("white");
}

function skinColor(s:String):uint
{
	var col:uint;
	
	switch(s)
	{
		case "black":
			col=0x000000;
		break;
		
		case "white":
			col=0xFFFFFF;
		break;
		
		case "red":
			col=0xFF0000;
		break;
		
		case "yellow":
			col=0xFFFF00;
		break;
	}
	
	return col;
}

trace(miles.skin());
trace(jaco.skin());

In this case we have 2 objects ( jaco and miles) and a function (skinColor) reported globally.

The function skinColor is used to add value at methods"skin" of both items.

Indeed methods skin calls the function skinColor passing a value and receiving another (in this case the hex number of colors).


Source files:
Attached Files
File Type: zip MethodsToObjects.zip (16.7 KB, 20 views)

__________________

 


I recommend: Essential Actionscript 3.0

- I do not reply technicians pvt messages. Open a thread !
- Non rispondo ai messaggi privati con domande tecniche. Apri una discussione sul forum !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Actionscript 3.0 perlinNoise method Flep Tutorials 6 05-12-08 10:05
New HitTest method of Actionscript 3.0 Flep Tutorials 3 05-03-08 23:06
adding an eventListener to an Object in a For Loop guitarman Flash English 2 31-01-08 05:04
BitmapData - draw method of Actionscript 3.0 Flep Tutorials 0 09-10-07 19:43
curveTo Method with Actionscript 3.0 Flep Tutorials 0 29-09-07 10:18


All times are GMT. The time now is 17:04.

Powered by vBulletin version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap