2 Replies Latest reply on Oct 3, 2006 7:01 PM by Newsgroup_User

    How can I make unique instances of my own classes?

    disco-logic
      Ok Ive got about as far as writing a really simple class in a seperate .as file to try and get these things to work.

      Lets assume that class works fine. This is a terrible assumption even at the level of complexity Im dealing with but I can come back later and moan about any individual issues I have with it.

      The problem is that the whole reason Im doing it this way is to be able to make as many instances of this class as I want. The particular test peice Im doing spams another "Pobject" (thats my class) somewhere random on the screen at a random orientation.

      Now if Im not mistaken then I use a line like this to make the new and unique Pobject:

      var pobjectname:Pobject = new Pobject(this,"drone",px,py,angle);

      Thing is I want it to be unique. How can I make the "pobjectname" part unique each time.

      I want to use a cancatenation like:
      pobject + pobjectcount;
      I suppose I could define it as a variable beforehand.
      var pobjectname = string;
      pobjectname = "pobject" + pobjectcount;

      I just get the impression though that Ill just be creating a pobject called "pobjectname" each time.

      Am I totally missing the point/is it doable?

      Also if I wanted to add some behaviour to every pobject is it possible to use a
      onClipEvent(enterFrame) style thing within my pobject code?
      Say so that on each frame every pobject moves forwards according to it individual orientation?

      If all this makes no sense then Ill tell you what Im aiming at in the end:
      Imagine pobject is like a unit in a game that you control.
      Basically I want to be able to create a situation where I can create as many pobjects as I want and have each one follow the standard code on how a pobject acts but do so indipendant of the others.

      Any advice on this subject would be greatly appreciated.
        • 1. How can I make unique instances of my own classes?
          coldMiner Level 1
          var test:Array = new Array();
          var maxPobject:Number
          for(var i:Number=0;i<maxPobject; i++){
          var pobjectname:Pobject = new Pobject(this,"drone"+i,px,py,angle);
          test.push(pobjectname);
          }
          test[0].doSomeThing(50);
          test[1].doSomeThing(120);
          test[3].doSomeThing(321);
          //////////////////////////////////////
          in the class Pobject you have to add a doSomeThing function
          example
          public doSomeThing (what:Number):Void{
          nameOfMovieClip.onEnterFrame = function(){
          if(this._x<what){
          this._x++;
          } else {
          delete this.onEnterFrame;
          }
          };
          }
          ///////////////////
          //////////////////
          I've done similar things that you describe.
          And I find it easier to have a manager class that controls the other class that is beeing duplicated.
          Then you can do things like myManager.addUFO(); myManager.countUFOs(); myManager.hideUFOs(); and lot's of other stuff.
          • 2. Re: How can I make unique instances of my own classes?
            Level 7

            "disco-logic" <webforumsuser@macromedia.com> wrote in message
            news:efv1o2$kco$1@forums.macromedia.com...
            > Ok Ive got about as far as writing a really simple class in a seperate .as
            > file
            > to try and get these things to work.
            >
            > Lets assume that class works fine. This is a terrible assumption even at
            > the
            > level of complexity Im dealing with but I can come back later and moan
            > about
            > any individual issues I have with it.
            >
            > The problem is that the whole reason Im doing it this way is to be able to
            > make as many instances of this class as I want. The particular test peice
            > Im
            > doing spams another "Pobject" (thats my class) somewhere random on the
            > screen
            > at a random orientation.
            >
            > Now if Im not mistaken then I use a line like this to make the new and
            > unique
            > Pobject:
            >
            > var pobjectname:Pobject = new Pobject(this,"drone",px,py,angle);
            >
            > Thing is I want it to be unique. How can I make the "pobjectname" part
            > unique
            > each time.
            >

            There are several ways to do this, however the way I like to do it is with
            another "pobjectField" class that holds all of the references to the
            pobjects. For instance you might have something like this as a doInit()
            function of a PobjectField class.

            .... define a variable for your class called pobjects ....

            private var pobjects:Array;

            public function doInit(_pobjects:Array) {
            for (var i:Number = 0; i < _pobjects.length; i++) {
            var pobjectname:Pobject = new Pobject(this, "drone", _pobjects );
            pobjects.push(pobjectname);
            }
            }

            then, when you want to create your PobjectField of Pobjects you can do
            someting like:

            var PobjectData:Array = new Array({px:1, py:2, angle:90}, {px:2, py:30,
            angle:180}....);
            myPobjectField.doInit(PobjectData);

            Then you can add all sorts of usefull methods to your PobjectField and
            Pobject classes to track whatever you need for example:

            //add to PobjectField class

            public function getPobject(_index):Pobject {
            return pobjects[_index];
            }


            HTH




            > I want to use a cancatenation like:
            > pobject + pobjectcount;
            > I suppose I could define it as a variable beforehand.
            > var pobjectname = string;
            > pobjectname = "pobject" + pobjectcount;
            >
            > I just get the impression though that Ill just be creating a pobject
            > called
            > "pobjectname" each time.
            >
            > Am I totally missing the point/is it doable?
            >
            > Also if I wanted to add some behaviour to every pobject is it possible to
            > use
            > a
            > onClipEvent(enterFrame) style thing within my pobject code?
            > Say so that on each frame every pobject moves forwards according to it
            > individual orientation?
            >
            > If all this makes no sense then Ill tell you what Im aiming at in the end:
            > Imagine pobject is like a unit in a game that you control.
            > Basically I want to be able to create a situation where I can create as
            > many
            > pobjects as I want and have each one follow the standard code on how a
            > pobject
            > acts but do so indipendant of the others.
            >
            > Any advice on this subject would be greatly appreciated.
            >