ActiveDen

AS3 variable declaration

2998 posts
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Author had a File in an Envato Bundle
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 50 and 99 items
  • India
  • Referred between 200 and 499 users
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?

1 year ago
3086 posts
  • Has been a member for 3-4 years
  • Author had a File in an Envato Bundle
  • Interviewed on the Envato Notes blog
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • Italy
  • Referred between 100 and 199 users
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

1 year ago
1623 posts
  • Has been a member for 3-4 years
  • Won a Competition
  • Author had a File in an Envato Bundle
  • Interviewed on the Envato Notes blog
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Bought between 10 and 49 items
  • Italy
  • Referred between 50 and 99 users
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.
1 year ago
3086 posts
  • Has been a member for 3-4 years
  • Author had a File in an Envato Bundle
  • Interviewed on the Envato Notes blog
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • Italy
  • Referred between 100 and 199 users
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.

1 year ago
3086 posts
  • Has been a member for 3-4 years
  • Author had a File in an Envato Bundle
  • Interviewed on the Envato Notes blog
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 10 and 49 items
  • Italy
  • Referred between 100 and 199 users
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

1 year ago
1448 posts
  • Has been a member for 3-4 years
  • Won a Competition
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Community Superstar
  • Beta Tester
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
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 :)

1 year ago
2998 posts
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Author had a File in an Envato Bundle
  • Exclusive Author
  • Sold between 100 000 and 250 000 dollars
  • Elite Author
  • Bought between 50 and 99 items
  • India
  • Referred between 200 and 499 users
VF says

Thanks parker and bitfade, really helpful! :)

DaniMun, thanks for the link.

1 year ago
4213 posts
  • Has been a member for 3-4 years
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Community Superstar
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 50 000 and 100 000 dollars
  • Bought between 100 and 499 items
  • United States
  • Referred between 50 and 99 users
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 :)

1 year ago
1448 posts
  • Has been a member for 3-4 years
  • Won a Competition
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Community Superstar
  • Beta Tester
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
DaniMun says

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

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

1 year ago
1448 posts
  • Has been a member for 3-4 years
  • Won a Competition
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Community Superstar
  • Beta Tester
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 1 and 9 users
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

1 year ago
by
by
by
by
by