0

I know global variables are supposed to be bad but is it possible to create global classes? I am creating an application and I want to have one class that handles sound. From any class I would like to be able to say soundhandler.playSound(); without having to pass references all over the place. It should just know it is there.

Any help greatly appreciated.

senor tee
  • 47
  • 9

1 Answers1

2

You're referring to static members.

Your class SoundHandler would have a static method called playSound(), which can be implemented like so:

package
{
    public class SoundHandler
    {
        public static function playSound():void
        {
            // @todo Logic
        }
    }
}

Your playSound() method is now accessible via:

SoundHandler.playSound();

Note: You mentioned global methods being bad, however this is a perfect candidate for these and something I would actually recommend (as much as I hate using static).

Additional: ActionScript 3's Math class contains mostly static members e.g. Math.round()


Your question (comment): Do I need to initiate SoundHandler in the document class?

No, in fact you shouldn't make an instance of SoundHandler at all. The only requirement is that you must have SoundHandler imported in your current class to access it:

import yourpackage.SoundHandler;
Marty
  • 39,033
  • 19
  • 93
  • 162
  • So I would just instantiate the SoundHandler in the main document class? Thanks for the reply. – senor tee Jan 20 '12 at 11:13
  • @senortee See edit. Basically, you do not need to create an instance of a class to access its static elements. – Marty Jan 20 '12 at 11:19
  • Ahhhhh import. That's where I've been going wrong. Thanks so much, this is not explained elsewhere, you have just supplied a big bit of jigsaw that was missing. – senor tee Jan 20 '12 at 11:19
  • 1
    @senortee You should consider a new AS3 editor like my personal favorite: [FlashDevelop](http://www.flashdevelop.org/). This interface will automatically add imports at the top of your class as you work. – Marty Jan 20 '12 at 11:21
  • Interesting, looks good. I will take a closer look. Thanks again and have a good weekend! – senor tee Jan 20 '12 at 11:23
  • If you want to initialize static variables without having to initialize the class, you could use a static constructor as well. – avanderw Jan 20 '12 at 11:35
  • Hi there, could you provide an example? – senor tee Jan 20 '12 at 12:12
  • @MartyWallace Cheers for the heads up. FlashDevelop is so much lighter and smoother than the Flash IDE. Collapsible functions. Joy! :) – senor tee Jan 20 '12 at 15:37
  • Look into Singleton.registerClass if you want to only have one instance of a class – The_asMan Jan 20 '12 at 17:06
  • So here's another issue. If I import a class then call a function within it that adds an object to the stage it can't do it. Is this because it isn't in the display list itself? How can I have a globally accessible class that can add things to the display list? Thanks guys. – senor tee Jan 31 '12 at 15:21
  • @senortee If you post a detailed question for the problem you're having and link to it here, I'll be able to help you out in proper depth. – Marty Jan 31 '12 at 22:07
  • Hi Marty. This was my question. Still having some issues so any help appreciated > http://stackoverflow.com/questions/9082820/as3-global-class-that-can-add-objects-to-stage – senor tee Feb 01 '12 at 14:48