PhotoDune

AS3 variable declaration

4493 posts
  • Exclusive Author
  • Author had a File in an Envato Bundle
  • Elite Author
  • 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
+4 more
VF says

Sorry, AS3 noob question! :P

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?

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

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

1844 posts
  • Elite Author
  • 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
  • Bought between 10 and 49 items
+4 more
bitfade says

Is this legal in AS3 ? This doesn’t declares data type. Will this cause performance issues or conflicts?
MovieClip is dynamic class which means you can add properties at runtime (like you did in your example)
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.
3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

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.

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says

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

1605 posts
  • 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
  • Referred between 1 and 9 users
+5 more
DaniMun says

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 :)

4493 posts
  • Exclusive Author
  • Author had a File in an Envato Bundle
  • Elite Author
  • 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
+4 more
VF says

Thanks parker and bitfade, really helpful! :)

DaniMun, thanks for the link.

5007 posts The Dude Abides
  • United States
  • Elite Author
  • Has been a member for 4-5 years
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Contributed a Tutorial to a Tuts+ Site
  • Author had a Free File of the Month
+4 more
CodingJack says

Nico is right :) using 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 :)

1605 posts
  • 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
  • Referred between 1 and 9 users
+5 more
DaniMun says

Thanks parker and bitfade, really helpful! :) DaniMun, thanks for the link.

Don’t mention it, Ramesh, glad I could help ;)

1605 posts
  • 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
  • Referred between 1 and 9 users
+5 more
DaniMun says


Nico is right :) using getters and setters is a great technique
Coming from AS2 as my first scripting language, these were foreign to me.

Actually AS2 OOP allows for getters and setters usage :)

http://www.flashscript.biz/MX2004/OOP_tutorial/lesson5.html

by
by
by
by
by