Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 06-26-2007, 03:35 PM   #1
Registered User
Join Date: Aug 2005
Posts: 6
Default dynamic properties

What is the proper way of creating dynamic properties in AS3.0? To clarify what I mean, in AS1.0/2.0 I could do something like this...

mcRef.my_special_property="i am special"

What is the equivalent AS3.0 code? And can this be done for Sprites as well as MovieClips? Thanks!
ddobbs is offline   Reply With Quote
Old 06-26-2007, 03:45 PM   #2
Salmon Eater
TomMalufe's Avatar
Join Date: Jun 2007
Location: Doylestown, PA, USA.
Posts: 740

The way you do something like that in AS3 is you extend the Sprite or MovieClip class file.

ActionScript Code:
package {      import flash.display.Sprite;      public class Special extends Sprite {           public var mySpecialProperty:*;      } }

It's considered proper to use private vars in a class and then setup getter and setter functions for it. But you don't have to in every case.
TomMalufe is offline   Reply With Quote
Old 06-26-2007, 05:35 PM   #3
six eyes
senocular's Avatar
Join Date: Jan 2003
Location: San Francisco, CA (USA)
Posts: 7,875
Send a message via ICQ to senocular Send a message via AIM to senocular Send a message via MSN to senocular Send a message via Yahoo to senocular

MovieClip instances are dynamic in AS3 so you can add dynamic properties to them just as you could in AS2. Some other classes, like Sprite, are not dynamic, however, and you would then have to use a MovieClip instead or some other dynamic alternative (or place your properties in a subclass as demonstrated by Tom)
senocular is offline   Reply With Quote
Old 06-26-2007, 05:45 PM   #4
panel's Avatar
Join Date: Mar 2007
Location: Warsaw
Posts: 1,991
Send a message via Skype™ to panel

But if you really wanna create variables in runtime you have to create dynamic class

ActionScript Code:
package {     public dynamic class MyClass     {         public function MyClass()         {                     }     } }
panel is offline   Reply With Quote
Old 06-26-2007, 08:47 PM   #5
Registered User
Join Date: Aug 2005
Posts: 6

Thanks fellas. I think I got it working but it doesn't seem proper for some reason. Here's the AS1/2 code followed by the new AS3 code...

propArr=new Array("Red","Coffee","Arizona")

for(i=0;i<propArr.length;i++) {
	mcRef.moveTo(0, 0);
	mcRef.lineTo(50, 0);
	mcRef.lineTo(50, 20);
	mcRef.lineTo(0, 20);
	mcRef.onPress=function() {
new AS3 code in 2 files, main.as:
package {
	import flash.display.Sprite;
	import flash.events.MouseEvent;  

	public class main extends Sprite
		public function main()
			var propArr:Array=new Array("Red","Coffee","Arizona")
			for(var i:int=0;i<propArr.length;i++) {
				var scRef:specialContainer = new specialContainer(propArr[i],i);
				scRef.addEventListener(MouseEvent.MOUSE_DOWN, clickMe)
		public function clickMe(event:MouseEvent):void {
			trace(event.target.my_special_property+" "+event.target.my_index)
and specialContainer.as:
package {
	import flash.display.Sprite;

	public class specialContainer extends Sprite
		public var my_special_property:String;
		public var my_index:int;
		public function specialContainer(p1:String,p2:int)

Is there some correct way/better way to do this? or just an alternate way?

Thanks again.
ddobbs is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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 Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a PDF file in Flash AudryLucy Projectors and CDs 20 12-30-2006 01:43 PM
Help strict datatype dynamic objects and their properties ironchefmoto ActionScript 2.0 4 08-17-2006 10:04 PM
Monitoring when images in dynamic text field are downloaded pattyg ActionScript 2.0 2 08-07-2006 04:22 PM
Dynamic Movieclips and movieclip properties Jmc8385 Simple Stuff (Newbies) 2 05-13-2005 06:00 PM
retrieving dynamic movieclip properties and more rowanhealy ActionScript 2.0 2 04-21-2005 04:01 PM

All times are GMT. The time now is 11:37 PM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.