Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 06-26-2007, 03:35 PM   #1
ddobbs
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=_root.createEmptyMovieClip("mc",1)
mcRef.my_special_property="i am special"
mcRef.my_index=4


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
TomMalufe
Salmon Eater
 
TomMalufe's Avatar
 
Join Date: Jun 2007
Location: Doylestown, PA, USA.
Posts: 740
Default

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
senocular
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
Default

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)
__________________
(6)
senocular is offline   Reply With Quote
Old 06-26-2007, 05:45 PM   #4
panel
AS3
 
panel's Avatar
 
Join Date: Mar 2007
Location: Warsaw
Posts: 1,991
Send a message via Skype™ to panel
Default

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
ddobbs
Registered User
 
Join Date: Aug 2005
Posts: 6
Default

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...

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

for(i=0;i<propArr.length;i++) {
	
	mcRef=this.createEmptyMovieClip("mc_"+i,i)
	mcRef.my_special_property=propArr[i]
	mcRef.my_index=i
	
	mcRef._x=80*i
	mcRef._y=10
	
	mcRef.beginFill(0xFF0000);
	mcRef.moveTo(0, 0);
	mcRef.lineTo(50, 0);
	mcRef.lineTo(50, 20);
	mcRef.lineTo(0, 20);
	mcRef.endFill();
	
	mcRef.onPress=function() {
		trace([this.my_special_property,this.my_index])
	}
}
new AS3 code in 2 files, main.as:
Code:
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.x=80*i
				scRef.y=10				
				scRef.buttonMode=true
				scRef.addEventListener(MouseEvent.MOUSE_DOWN, clickMe)
				addChild(scRef)
				
			}
						
		}
		
		public function clickMe(event:MouseEvent):void {
			trace(event.target.my_special_property+" "+event.target.my_index)
		}		
	}
}
and specialContainer.as:
Code:
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)
		{
			graphics.beginFill(0xFF0000)
			graphics.drawRect(0,0,50,50)
			this.my_special_property=p1
			this.my_index=p2
		}
	}
}

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

Thanks again.
ddobbs is offline   Reply With Quote
Reply


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 03:58 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.