6 Replies Latest reply on Aug 23, 2006 10:20 PM by abeall

    Global Object Building

    OmniFlash
      I am using flash MX and looked in the actionscript dictionary and it came with that example:

      function Book(name, price){
      this.name = name;
      this.price = price;
      }

      book1 = new Book("Confederacy of Dunces", 19.95);
      book2 = new Book("The Floating Opera", 10.95);

      Now the problem is I want my object to be globaly usable by other movieclips

      I tried
      function _global.Book(name, price)

      and it wouldn't accept it

      I tried:
      _global.Book = new function (name, price)

      but obviously it wasn,t an object that it gave...

      So my qustion is how do I make global objects or classes
        • 1. Re: Global Object Building
          abeall Level 3
          You're second try was very close. Check F1 reference:
          Learning ActionsScript 2.0 in Flash > Functions and Methods > About functions and methods > About types of methods and functions > Writing anonymous and callback functions

          ----------[ From F1 reference ]----------
          To write an anonymous function:

          Create a movie clip on the Stage, and then select the clip.
          Open the Property inspector, and type my_mc into the Instance Name text box.
          Select Frame 1 of the Timeline, and type the following code into the Actions panel:
          var myWidth = function () {
          trace(my_mc._width);
          };
          //later in code you can add
          myWidth();
          Select Control > Test Movie.
          The width of the movie clip is displayed in the Output panel.
          • 2. Re: Global Object Building
            abeall Level 3
            So in your case, it would be:

            _global.Book = function (name, price){

            }

            However, keep in mind that if you are using AS2.0 there is a new class architecture to use, this is the old AS1.0 way.
            • 3. Re: Global Object Building
              CogDev
              why not just make the class in a seperate .as file?
              • 4. Re: Global Object Building
                OmniFlash Level 1
                Now the trouble is I CAN'T make an object

                The way you explained was a way to make a function but I need to be able to re-use an object:

                Here is what I tried and it didn't work:
                _global.Player = function(power, defence, precision, speed, HP, currentHP, movingSpeed)
                {
                this.power = power;
                this.defence = defence;
                this.precision = precision;
                this.speed = speed;
                this.HP = HP;
                this.currentHP = currentHP;
                this.movingSpeed = movingSpeed;
                }
                var player = _global.Player(5, 5, 5, 5, 100, 100, 10);
                trace (player.power);
                • 5. Re: Global Object Building
                  OmniFlash Level 1
                  oops I forgt the new _global.Player

                  My bad...ok it works
                  • 6. Re: Global Object Building
                    abeall Level 3
                    Yep. You should also be able to use:

                    var player = new Player(5, 5, 5, 5, 100, 100, 10);

                    as well(don't need _global prefix).