# \[AS2 OOP\] Best way to instance a class?

**URL:** <https://forum.kirupa.com/t/as2-oop-best-way-to-instance-a-class/182044>\
**Category:** flash\
**Created:** [March 15, 2006, 2:23am UTC](https://forum.kirupa.com/t/as2-oop-best-way-to-instance-a-class/182044 "2006-03-15T02:23:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Ford](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@Matthew\_Ford](https://forum.kirupa.com/u/Matthew_Ford)\
**Post date:** [March 15, 2006, 2:23am UTC](https://forum.kirupa.com/t/as2-oop-best-way-to-instance-a-class/182044/1 "2006-03-15T02:23:36Z")

</div>

[FONT=Arial]Hello everyone. I love Kirupa![/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]I am wrapping my brain around OOP and I need some advice. I am making two posts on two different subjects.[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]My question here is: when should I link an AS2 class to a library symbol which I then instance, and when should I use “new” in code to create a new instance of a class? (Feel free to correct my terminology.)[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]Let’s say I have a menu for general system options. I want to define it in a class called MenuSystem. One of the methods I want to attach to it is “summon”, which makes the menu visible, plays a sound, and changes its contents based on context.[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]I make a symbol in the Library which lays out all the menu’s elements and give this symbol the linkage identifier “ID\_menuSystem”.[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]It seems I have two ways I can create, initialize, and refer to this thing and darned if I know which is better practice.[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]\*\*\*METHOD A: [/FONT]  
[FONT=Arial]Make the class MenuSystem extend MovieClip. [/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]The class constructor does not do anything.[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]In the class, references to the menu clip’s methods are done thusly:[/FONT]  
[FONT=Arial]this.\_visible=true;[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]In the library, fill in the symbol’s AS 2.0 class as “MenuSystem”.[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]In my main code, in a startup initialization function, I create the menu on the stage with:[/FONT]  
[FONT=Arial]\<MC to attach the menu to\>.attachMovie(“ID\_menuSystem”, “theMenuSystem”, depth)[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]And then call the method anytime by referring to the instance:[/FONT]  
[FONT=Arial]\<path\>.theMenuSystem.summon();[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]\*\*\*METHOD B:[/FONT]  
[FONT=Arial]Don’t make the class MenuSystem extend MovieClip. [/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]The class constructor takes an argument “attachTo” for the MC to which the constructed menu’s MC should be attached.[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]The class constructor has in it:[/FONT]  
[FONT=Arial]this.myMC = attachTo.attachMovie(“ID\_menuSystem”, “theMenuSystem”, depth);[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]In the class, references to the clip’s methods are done thusly:[/FONT]  
[FONT=Arial]this.myMC.\_visible=true;[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]In my main code, in a startup initialization function, I create the menu with:[/FONT]  
[FONT=Arial]this.theMenuSystem = new MenuSystem(\<MC to attach the menu to\>);[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]And then call the method anytime by referring to the instance:[/FONT]  
[FONT=Arial]this.theMenuSystem.summon();[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]\*\*\*QUESTION:[/FONT]  
[FONT=Arial]Which method should I use, A or B? My main priorities are, in order:[/FONT]  
[FONT=Arial]1) Make code that can scale up to be part of a rather complex application.[/FONT]  
[FONT=Arial]2) Make code which will be easily reusable in other projects.[/FONT]  
[FONT=Arial]3) Make code which is comprehensible to other (real) coders (me being a fake one).[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]???[/FONT]  
[FONT=Arial] [/FONT]  
[FONT=Arial]Thanks very much for any advice you can give to someone who wants to do good OOP but is lacking much formal training.[/FONT]
