15 Replies Latest reply on Nov 20, 2006 8:46 AM by clbeech

    Functions within classes, returning variables Help!

    Enzuguri
      Firstly I would like to say, before we start, that I am in no way a professional developer,

      this is for a university project, and as such, I can't handle large peices of code, if you

      post a reply with this stuff, I'm not going to be able to understand it. I don't mean to be rude (I am british after all) but having been a vetern of many forums, posting pages and pages of advanced code implementations is not going to help me out.

      I'm trying to write a pre-loader class, I've seen a few on various websites, but i'm

      stripping out the code that i don't need because I just need the nuts and bolts. i'm running

      into problems getting data in and out of functions/methods within the class. The class for

      the preloader looks like this..

      /*
      Preloader class
      */

      class Preloader {
      // instance variables
      public var movieName:String;


      // constructor statement
      public function Preloader(movieName:String) {
      this.movieName = movieName;
      }
      //Methods

      public function loadObj(targetClip) {
      _root.createEmptyMovieClip(targetClip, 100);
      loadMovie(movieName, targetClip);
      };

      public function bTotal() {
      return this.loadObj.getBytesTotal();
      };

      public function bLoaded() {
      return this.loadObj.getBytesLoaded();
      };

      public function pLoaded() {
      return this.loadObj.getBytesLoaded()/this.loadObj.getBytesTotal()*100;
      };
      public function sendStuff():String {
      var myMessage = "i'm a variable, get me out of here!";
      return myMessage;
      };
      }

      And i'm using the class on the first frame of a flash file with a dynamic text box named

      status.

      Frame 1

      import Preloader.as;
      var clipPreload:Preloader = new Preloader("smalljpeg.jpg");
      clipPreload.loadObj("mc");
      this.status.text = clipPreload.sendStuff;

      When I run the animation the status text box returns [type function] instead of the string.

      I'm sure this is something really simple, I eventually want to be able to get the functions

      bTotal to return, and i'll use the other functions on clipevents, enterframe or intervals. Is this a

      problem with defining variables within functions instead of public/private?

      Many thanks in advance for all your help
      Alexander
        • 1. Re: Functions within classes, returning variables Help!
          Level 7
          will one line be short enough? :-)

          this.status.text = clipPreload.sendStuff();



          "Enzuguri" <webforumsuser@macromedia.com> wrote in message
          news:ejscoo$7t9$1@forums.macromedia.com...
          > Firstly I would like to say, before we start, that I am in no way a
          > professional developer,
          >
          > this is for a university project, and as such, I can't handle large peices
          > of
          > code, if you
          >
          > post a reply with this stuff, I'm not going to be able to understand it. I
          > don't mean to be rude (I am british after all) but having been a vetern of
          > many
          > forums, posting pages and pages of advanced code implementations is not
          > going
          > to help me out.
          >
          > I'm trying to write a pre-loader class, I've seen a few on various
          > websites,
          > but i'm
          >
          > stripping out the code that i don't need because I just need the nuts and
          > bolts. i'm running
          >
          > into problems getting data in and out of functions/methods within the
          > class.
          > The class for
          >
          > the preloader looks like this..
          >
          > /*
          > Preloader class
          > */
          >
          > class Preloader {
          > // instance variables
          > public var movieName:String;
          >
          >
          > // constructor statement
          > public function Preloader(movieName:String) {
          > this.movieName = movieName;
          > }
          > //Methods
          >
          > public function loadObj(targetClip) {
          > _root.createEmptyMovieClip(targetClip, 100);
          > loadMovie(movieName, targetClip);
          > };
          >
          > public function bTotal() {
          > return this.loadObj.getBytesTotal();
          > };
          >
          > public function bLoaded() {
          > return this.loadObj.getBytesLoaded();
          > };
          >
          > public function pLoaded() {
          > return this.loadObj.getBytesLoaded()/this.loadObj.getBytesTotal()*100;
          > };
          > public function sendStuff():String {
          > var myMessage = "i'm a variable, get me out of here!";
          > return myMessage;
          > };
          > }
          >
          > And i'm using the class on the first frame of a flash file with a dynamic
          > text
          > box named
          >
          > status.
          >
          > Frame 1
          >
          > import Preloader.as;
          > var clipPreload:Preloader = new Preloader("smalljpeg.jpg");
          > clipPreload.loadObj("mc");
          > this.status.text = clipPreload.sendStuff;
          >
          > When I run the animation the status text box returns [type function]
          > instead
          > of the string.
          >
          > I'm sure this is something really simple, I eventually want to be able to
          > get
          > the functions
          >
          > bTotal to return, and i'll use the other functions on clipevents,
          > enterframe
          > or intervals. Is this a
          >
          > problem with defining variables within functions instead of
          > public/private?
          >
          > Many thanks in advance for all your help
          > Alexander
          >


          • 2. Re: Functions within classes, returning variables Help!
            clbeech Level 3
            Well, for someone who doesn't claim to be a 'code guy/girl' your sure working with some advanced concepts. Class file definition is not exactly the starting point for a beginer? : )

            I may have something to offer, variables that are declaired within a Function are only local to that Function and do not exist outside of it. Also _global variables cannot be used within a class file definition, so this won't work for you either.

            You must declair the variable earlier in your AS before the Function statement(s) containing them, as in ... (I know you didn't want code, but )

            var myMessage:String = new String();

            then within the Function statement you may say ...

            myMessage = "i'm a variable, get me out of here!";

            Now if you changing this variable to a number you need to type it differently ..

            var myNumber:Number = new Number(); (this is called 'strict typing')

            and so on. However it's still a little more complex to pass a variable out of a class file. all for now though.
            • 3. Re: Functions within classes, returning variables Help!
              anonymous thing Level 1
              this.status.text = clipPreload.sendStuff();

              Without (), you asign a reference to sendStuff function, it doesn't call and execute the function...
              • 4. Re: Functions within classes, returning variables Help!
                clbeech Level 3
                yep, yep, didn't see that.
                • 5. Re: Functions within classes, returning variables Help!
                  Enzuguri Level 1
                  Thanks for your quick response guys, i found that I did need to declare the variable in the opening few lines, then I needed to put the all important "this." infront of my variables. So i now have this..

                  class Preloader {
                  // instance variables
                  public var movieName:String;
                  public var myMessage:String;

                  ...and the sendStuff functions now reads..

                  public function sendStuff():String {
                  myMessage = "i'm a variable, get me out of here!";
                  this.myMessage = myMessage
                  return this.myMessage;
                  };
                  • 6. Re: Functions within classes, returning variables Help!
                    Enzuguri Level 1
                    Still.... the sendStuff function was just a test to see if i could actually get the data out of the class, my real trouble is getting information from the second loadObj function into the remaining 3 functions bTotal , bLoaded , and pLoaded. I've setup the targetclip variable as a public varaible now, so all the functions "know" that the targetclip is set to "mc"....

                    class Preloader {
                    // instance variables
                    public var movieName:String;
                    public var targetClip;
                    .....

                    public function loadObj(targetClip) {
                    this.targetClip = targetClip;
                    _root.createEmptyMovieClip(targetClip, 100);
                    loadMovie(movieName, targetClip);
                    };

                    This is from when i call

                    clipPreload.loadObj("mc");

                    I Can now read out the targetClip property mc, which i've tried and works, using the sendstuff my going..

                    myMessage = this.TargetClip

                    However, I basically want this function...

                    public function bTotal() {
                    return this.targetClip.getBytesTotal();
                    };

                    To read

                    function bTotal():Number{
                    return _root.mc.getBytesTotal();
                    }

                    Is this something to do with loadVars? I was reading about this earlier.
                    Again many thanks in advance
                    alexander
                    • 7. Re: Functions within classes, returning variables Help!
                      clbeech Level 3
                      Interesting, glad it's starting to work? looks as though there's an extra statement in there, you should not need the ...

                      this.myMessage = myMessage; //(needs the ' ; ' though)

                      I think, and the return statement should only need the variable name, and not with a 'this' proceeding it. in this way you calling a pathway and shouldn't be necessary, a little surprised it doesn't throw an error.
                      • 8. Re: Functions within classes, returning variables Help!
                        Enzuguri Level 1
                        Yeah I thought that too, but i removed it and it didn't work, or maybe i'm removing the wrong combinations of this.'s
                        N.E.ways I tried directly referencing the mc by using
                        this.status.text = mc.getBytesTotal();
                        but it comes up as 0, in the reference is says it will come up as undefined if it can't find anything. Maybe this is because it is local, i'm going to try putting it online now.
                        • 9. Re: Functions within classes, returning variables Help!
                          clbeech Level 3
                          on the second post, you may need to 'type' the targetClip var, as in ...

                          public var targetClip:MovieClip = new MovieClip();

                          Also, I'm not certain, but I think a 'best practice' might be to declare a var within the functions that call a return, instead of assigning the value directly to the return statement. As in...

                          function bTotal():Number{
                          var tempBtotal:Number = _root.mc.getBytesTotal();
                          return tempBtotal;
                          }

                          Also the use of _root can sometimes screw stuff up, because it will always refer to the main swf timeline, UNLESS it's being called from within a loaded swf or MC, then it can sometime refer to that MC's timeline. you should declare the _lockroot property in the main timeline or that of the loading MCs to be able to refer to the correct mc timeline your trying to read. Better to use the exact pathway to the mc, than using _root, or even 'this.' refering to the mc being loaded by the function.
                          • 10. Re: Functions within classes, returning variables Help!
                            Enzuguri Level 1
                            ok cheers clbeech,
                            just one thing tho, if i create a new movieClip object with

                            public var targetClip:MovieClip = new MovieClip();

                            what do i do about

                            public function loadObj(targetClip) {
                            this.targetClip = targetClip;
                            _root.createEmptyMovieClip(targetClip, 100); //this bit?
                            loadMovie(movieName, targetClip);
                            };
                            • 11. Re: Functions within classes, returning variables Help!
                              Enzuguri Level 1
                              uh oh.....
                              Line 8: A class's instance variables may only be initialized to compile-time constant expressions.
                              public var targetClip:MovieClip = new MovieClip();
                              • 12. Re: Functions within classes, returning variables Help!
                                clbeech Level 3
                                seems as though the statement ...

                                this.status.text = mc.getBytesTotal();

                                might be undefined because mc.getBytesTotal(); will return a number and not a string, you may need to convert the value before being able to use it that way. How 'bout try ...

                                trace(mc.getBytesTotal() ); // or assign the total to a variable first ...

                                var TEMPtotal = mc.getBytesTotal();
                                trace(TEMPtotal);

                                this should give you a value in the Output panel that you can track without mucking up your code.
                                • 13. Re: Functions within classes, returning variables Help!
                                  clbeech Level 3
                                  hummm, working on it ....

                                  may not need the whole statement within a class file, try ...

                                  public var targetClip:MovieClip; //only

                                  As for ...

                                  "what do i do about

                                  public function loadObj(targetClip) {
                                  this.targetClip = targetClip;
                                  _root.createEmptyMovieClip(targetClip, 100); //this bit?
                                  loadMovie(movieName, targetClip);
                                  }; "

                                  You may only need ...

                                  public function loadObj(targetClip) {
                                  loadMovie(movieName, targetClip);
                                  }

                                  Since, 'targetClip' has already been established as a MovieClip, it should be ready and waiting to use a your 'holder clip' ... I think?
                                  • 14. Re: Functions within classes, returning variables Help!
                                    Enzuguri Level 1
                                    well... I've managed to get the code to work with the _root .swf, i put all my functions together because i realised I don't need to get the total bytes and loaded bytes seperatly (i'll add this at a later date). so i've done..
                                    public function getLoaded() {
                                    bTotal = _root.getBytesTotal();
                                    bLoaded = _root.getBytesLoaded()
                                    pLoaded = Math.ceil(bLoaded/bTotal*100);
                                    return pLoaded;
                                    };
                                    which does work, but i'm still having trouble pointing to "mc"
                                    • 15. Re: Functions within classes, returning variables Help!
                                      clbeech Level 3
                                      I still do think that this may have something to do with the use of _root here. Since this function still resides within the class file, try pointing to 'targetClip.getBytesLoaded()' and so on, as the variable 'targetClip' should still change with each new MC loaded.