- Exclusive Author
- Item was Featured
- Author was Featured
- Author had a File in an Envato Bundle
- Has been a member for 4-5 years
- Sold between 100 000 and 250 000 dollars
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- India
Sorry, AS3 noob question! 
Assume that we have a movieclip:
var MC:MovieClip = new MovieClip(); addChild (MC);
Set variable values without declaration in this way:
MC.myString = "XXX"; MC.myNumber = 123; MC.myMC = new MovieClip();
Is this legal in AS3 ? This doesn’t declares data type. Will this cause performance issues or conflicts?
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Interviewed on the Envato Notes blog
- Author was Featured
- Item was Featured
- Beta Tester
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
Hi,
no, that’s fine… they will be casted as generic Objects.
keep in mind that you can save values like that only through instances of a dynamic class (MovieClip in this case).
Parker
- Sold between 100 000 and 250 000 dollars
- Author had a File in an Envato Bundle
- Has been a member for 4-5 years
- Author had a Free File of the Month
- Won a Competition
- Author was Featured
- Item was Featured
- Bought between 10 and 49 items
VF saidMovieClip is dynamic class which means you can add properties at runtime (like you did in your example)
Is this legal in AS3 ? This doesn’t declares data type. Will this cause performance issues or conflicts?
untyped vars are allowed in AS3 , they don’t impact preformances as long as you’re not accessing them several thousands of times per frame.
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Interviewed on the Envato Notes blog
- Author was Featured
- Item was Featured
- Beta Tester
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
Obviously,
if you use a generic class like MovieClip for your objects the only way to save some instance variables is that one.
When you’ll be more skilled about OOP you will create your own objects with classes that extend a base class and in that case
MC.myString = “XXX” will be handled by a setter that edit the instance variable _my_string for example…
Using setters and getters allow you to check that the data saved into these variables have the right format and type.
- Sold between 250 000 and 1 000 000 dollars
- Exclusive Author
- Interviewed on the Envato Notes blog
- Author was Featured
- Item was Featured
- Beta Tester
- Author had a File in an Envato Bundle
- Author had a Free File of the Month
This would be good OOP for your case:
package {
import flash.display.MovieClip;
class MyClass extends MovieClip {
private var _my_string:String;
//getterfor _my_string
function get myString():String {
return _my_string;
}
//setter for _my_string
function set myString(value:String):void {
_my_string = value;
}
function MyClass() {
//constructor
}
}
}
var mc:MyClass = new MyClass();
mc.myString = "XXX";
//to get the value just:
trace(mc.myStyring);
Parker
- Author had a Free File of the Month
- Microlancer Beta Tester
- Beta Tester
- Contributed a Tutorial to a Tuts+ Site
- Won a Competition
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Item was Featured
- Author was Featured
- Referred between 1 and 9 users
Nico is right
using getters and setters is a great technique, here’s a great tutorial by Dru Kepple where you can learn more about them
http://active.tutsplus.com/tutorials/actionscript/as3-101-oop-inheritance-setters-getters-basix/
Hope this helps – it helped me a lot 
- Exclusive Author
- Item was Featured
- Author was Featured
- Author had a File in an Envato Bundle
- Has been a member for 4-5 years
- Sold between 100 000 and 250 000 dollars
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- India
Thanks parker and bitfade, really helpful! 
DaniMun, thanks for the link.
- United States
- Has been a member for 4-5 years
- Exclusive Author
- Author was Featured
- Sold between 50 000 and 100 000 dollars
- Item was Featured
- Contributed a Tutorial to a Tuts+ Site
- Author had a Free File of the Month
DaniMun said
Nico is rightusing getters and setters is a great technique
Coming from AS2 as my first scripting language, these were foreign to me. I realize you can execute things inside a getter or a setter, but you can also just call an internal function if you want to execute additional things, and then just change or return the property that you would have with the getter or setter. Personally I find it a lot easier to just use internal vars. I’m pretty sure it’s faster as well 
- Author had a Free File of the Month
- Microlancer Beta Tester
- Beta Tester
- Contributed a Tutorial to a Tuts+ Site
- Won a Competition
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Item was Featured
- Author was Featured
- Referred between 1 and 9 users
- Author had a Free File of the Month
- Microlancer Beta Tester
- Beta Tester
- Contributed a Tutorial to a Tuts+ Site
- Won a Competition
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Item was Featured
- Author was Featured
- Referred between 1 and 9 users
CrackerJack said
DaniMun saidComing from AS2 as my first scripting language, these were foreign to me.
Nico is rightusing getters and setters is a great technique
Actually AS2 OOP allows for getters and setters usage 

