11 Replies Latest reply on Jun 19, 2008 3:53 PM by Newsgroup_User

    removing a class

    Level 7
      Hi all - this might be the silliest question around, but I just can't seem
      to figure it out.

      I've created a highlighter tool, that allows the user to highlight text on
      the stage. I'm initializing the external class by using:

      import highlighter;
      var myhighlighter:highlighter = new highlighter();

      But when the user goes to the next frame, I can't figure out how to turn the
      thing off. How can I delete the myhighlighter variable?

      I've tried:

      delete myhighlighter

      and

      myhighlighter=null;

      But nothing has worked. I know this has got to be easy, but I just can't
      seem to figure it out. Thanks for your help.




        • 1. Re: removing a class
          crazyjoemilan Level 2
          I generally build in a delete function or clear function into my custom classes (depending on how I build it). I would write a function in the class to clear itself, then call that.

          myhighlighter.clearHilight(); //or something like that.

          If you didn't want to do that, does highlighter extend MovieClip ? Because then you can removeMovieClip().
          • 2. Re: removing a class
            Level 7
            Hi Mr Helpy,

            Thanks very much for your reply. I'm happy to hear that I'm on the right
            track.

            Calling the clear function within my class is no problem. What I don't
            understand is how does my function (within the .as file) actually stop the
            highlighter function from running.

            This is the public function that runs the highlighter:

            public function highlighter(){
            //this is my hilighter code
            }


            Then below it I have

            function stopHighlighter () {
            //I call this from my fla no problem
            //but how do I reference another function within this .as file? (For
            example, hilighter.stopRunningNow)
            }


            I hope my question makes sense. I'm really new to AS3 and external classes
            and I'm kind of surprised that I've made it this far!!

            Cheers,
            Brock





            "Mr Helpy mcHelpson" <webforumsuser@macromedia.com> wrote in message
            news:g3b97u$609$1@forums.macromedia.com...
            >I generally build in a delete function or clear function into my custom
            >classes
            > (depending on how I build it). I would write a function in the class to
            > clear
            > itself, then call that.
            >
            > myhighlighter.clearHilight(); //or something like that.
            >
            > If you didn't want to do that, does highlighter extend MovieClip ?
            > Because
            > then you can removeMovieClip().
            >


            • 3. Re: removing a class
              crazyjoemilan Level 2
              var myhi:highlighter = new highlighter;
              //now the object myhi has all the functions available to it.
              myhi.stopHighlighter();
              //stopHighlighter must be a public function (not private) so it can be called externally.

              the only fun part about this of course, is scoping. If you define your var myhi in a different function or class, you won't be able to see it, so you'll need to resolve that (if thats how you have it built).

              Does that make sense?
              • 4. Re: removing a class
                Level 7
                Hi Mr. Helpy,

                Thanks a bunch for your help. I ended up getting it to work by adding an if
                statement to my highlighter class and a Boolean variable that gets set when
                the stopHighlighter (public) function is called. So it looks something like
                this:

                package com.brockclasses{

                public class highlighter {

                private var shouldIHilight:Boolean=true;

                public function highlighter(){
                if(shouldIHilight == true){
                //run the highlighter
                }

                //stop the highlighter
                public function stopHighlighter () {
                shouldIHilight=false;
                }
                }
                }

                And on the second frame of my fla I call:

                myhighlighter.stopHighlighter();


                I'm not sure if this is the correct way to do something like this because I
                don't know if the highlighter class is still tying up resources, even though
                it won't run because the if statement is false. Regardless, like I said I'm
                new to this AS3 thing. This works and I guess I'll stick with it.

                Again, thanks a lot for your help.

                Cheers.






                "Mr Helpy mcHelpson" <webforumsuser@macromedia.com> wrote in message
                news:g3bd6o$aek$1@forums.macromedia.com...
                > var myhi:highlighter = new highlighter;
                > //now the object myhi has all the functions available to it.
                > myhi.stopHighlighter();
                > //stopHighlighter must be a public function (not private) so it can be
                > called
                > externally.
                >
                > the only fun part about this of course, is scoping. If you define your
                > var
                > myhi in a different function or class, you won't be able to see it, so
                > you'll
                > need to resolve that (if thats how you have it built).
                >
                > Does that make sense?
                >


                • 5. Re: removing a class
                  Rothrock Level 5
                  You are right, this is not the way to do this. Since you haven't done anything to actually remove the instance (you can't remove the class, just instances of it) it is still there and it is still taking resources.

                  Not sure if you are interested, but free advice is just that: FREE.

                  First it is good practice to give your class names that begin with a capital letter. If you check the Flash classes you will see that is their convention and it just helps later on. So from now on I will talk about the Highlighter class.

                  The next thing I'm confused about is how this trick is working. You appear to have your conditional check for the shouldIHilight (shoudn't that be shouldHighlight?) inside the constructor of your class. If that is the case how is it working? Are you creating multiple instance of the Highlighter class?

                  Now for your original question and the answer is, "It depends." How do you know that delete myHighlighter isn't working? I'm guessing that inside the class maybe you create a instance of a MovieClip? Or perhaps you have a setInterval or onEnterFrame inside that class? In that case as Helpy said you need to create some kind of method like destroy or clear or some such that stops all those things, removes any clips or other artwork, removes any listeners, etc. And then finally deletes the Highlighter instance.

                  So without knowing the details of what you have inside the class it is hard to tell you how to stop it.
                  • 6. Re: removing a class
                    crazyjoemilan Level 2
                    So without knowing the details of what you have inside the class it is hard to tell you how to stop it.

                    I think this is where I'm getting caught up as well. Also, you'll have to forgive me, as I thought this was in AS2 not AS3, but I see I should've read more closely on your post!

                    All the same, please post the code that runs your highlighter from the main class/fla. Hooray!
                    • 7. Re: removing a class
                      Level 7
                      Hi Rothrock and Mr. Helpy,

                      Again - thank you so much for all your help. I appreciate your time and
                      advice (and Rothrock - little things like naming classes with a capital is
                      good to know and I appreciate that too.)


                      I'm almost embarrassed to post my code because I'll bet it's riddled with
                      errors but here goes (and I've never posted so much code in a forum before
                      so if there is a better way to do this please let me know. I could upload
                      the fla and .as somewhere - what's proper etiquette?).

                      And one last thing before I begin, I totally understand if you guys don't
                      want to waste your time with this. I get that this is a lot to wade
                      through, so no worries).



                      First off, in my fla library I have a linked highlighter MC that has
                      separate MCs for stroke and fill colour, so they can be changed dynamically.
                      I also have a blank movie clip (I put all the highlighter stuff in a MC so
                      that I can put it on a layer in the timeline and then write stuff on top of
                      it.)




                      Secondly, from my fla I call:

                      var myCursor:hiLighter=new hiLighter;//hiLighter = linkage name of pen in
                      library
                      addChild (myCursor);
                      var myhighlighter:highlighter = new highlighter(myCursor, hilightHolder,
                      275, 60, 315, 130, 20, 0xFFFF00, 0xD1B611, 0xEFF5FC, true, btnErase);

                      All of the function properties (12 of them) are little tweaks for the
                      highlighter that are probably more complicated than it needed to be. (I was
                      trying to make this thing really versatile). In order they are:

                      whatClip to use as a cursor (in fla library), hilighter area holder (an
                      empty MC on fla stage), boundingBox - left,top,width, height, thickness of
                      hilight pen, colour of highlighter, colour of stroke on highlighter (ie. the
                      pen's outline), backgroundColour of drawing area, wantALine around drawing
                      area, nameOfEraseBtn on fla stage)




                      Finally, the code in my external .as is:


                      package com.brockclasses{
                      import fl.motion.Color;//for hilighter cursor colour
                      import flash.ui.Mouse;
                      import flash.events.MouseEvent;
                      import flash.events.Event;

                      import flash.geom.*;
                      import flash.display.*;


                      //-------------------------------------------

                      public class highlighter {


                      private var doDraw:Boolean=false;
                      private var spBoard:Sprite=new Sprite;
                      private var shDrawing:Shape=new Shape;
                      private var _hilightColour:Number;
                      private var _hilightLineSize:Number;
                      private var _nameOfEraseBtn:Object;
                      private var _myCursor:MovieClip;
                      private var _hilightAreaX:Number;
                      private var _hilightAreaWidth:Number;
                      private var _hilightAreaY:Number;
                      private var _hilightAreaHeight:Number;

                      private var shouldIHilight:Boolean=true;


                      public function highlighter (myCursor:MovieClip, hilightHolder:MovieClip,
                      hilightAreaX:Number, hilightAreaY:Number, hilightAreaWidth:Number,
                      hilightAreaHeight:Number, hilightLineSize:Number, hilightColour:Number,
                      hilightStrokeColour:Number, backgroundColour:Number, wantALine:Boolean,
                      nameOfEraseBtn:Object) {


                      _hilightColour = hilightColour;
                      _hilightLineSize = hilightLineSize;
                      _myCursor = myCursor;
                      _nameOfEraseBtn = nameOfEraseBtn;
                      _hilightAreaX = hilightAreaX;
                      _hilightAreaY = hilightAreaY;
                      _hilightAreaWidth = hilightAreaWidth;
                      _hilightAreaHeight = hilightAreaHeight;

                      myCursor.visible=false;
                      myCursor.mouseEnabled=false;

                      //set up colour of highlighter pen
                      var setHilightColour:Color=new Color;
                      setHilightColour.setTint (hilightColour,1);
                      var setHilightColourStroke:Color=new Color;
                      setHilightColourStroke.setTint (hilightStrokeColour,1);

                      myCursor.hilighterColour.transform.colorTransform=setHilightColour;
                      myCursor.hilighterOutline.transform.colorTransform=setHilightColourStroke;


                      myCursor.stage.addEventListener (Event.MOUSE_LEAVE,mouseLeaveHandler);



                      function mouseMoveHandler (e:MouseEvent):void {
                      //if (shouldIHilight == true) {
                      Mouse.hide ();
                      myCursor.visible=true;
                      if (myCursor.stage.mouseX >= hilightAreaX && myCursor.stage.mouseX <=
                      hilightAreaX + hilightAreaWidth && myCursor.stage.mouseY >= hilightAreaY &&
                      myCursor.stage.mouseY <= hilightAreaY + hilightAreaHeight) {

                      myCursor.x=e.stageX;
                      myCursor.y=e.stageY;

                      if (doDraw ) {
                      shDrawing.graphics.lineTo (shDrawing.mouseX,shDrawing.mouseY);
                      }
                      e.updateAfterEvent ();
                      } else {
                      myCursor.visible=false;
                      Mouse.show ();
                      myCursor.stage.removeEventListener
                      (MouseEvent.MOUSE_MOVE,mouseMoveHandler);
                      }
                      //}
                      }
                      function mouseLeaveHandler (e:Event):void {
                      myCursor.visible=false;

                      }
                      //create a new sprite to draw into - size of the stage and invisible
                      this.spBoard=new Sprite;
                      hilightHolder.addChild (spBoard);

                      spBoard.x=0;
                      spBoard.y=0;
                      spBoard.graphics.beginFill (backgroundColour);
                      if (wantALine == true) {
                      spBoard.graphics.lineStyle (1,0x000000);
                      }
                      spBoard.graphics.drawRect
                      (hilightAreaX,hilightAreaY,hilightAreaWidth,hilightAreaHeight);
                      spBoard.graphics.endFill ();


                      //create a holder within spBoard that will actually hold the drawing art.
                      It's within spBoard so that it can be erased without erasing spBoard
                      this.shDrawing=new Shape;
                      spBoard.addChild (shDrawing);
                      shDrawing.alpha=.5;
                      shDrawing.graphics.lineStyle (_hilightLineSize,_hilightColour);

                      //record user interaction with spBoard
                      spBoard.stage.addEventListener (MouseEvent.MOUSE_DOWN,boardDown);
                      spBoard.stage.addEventListener (MouseEvent.MOUSE_UP,boardUp);

                      spBoard.addEventListener (MouseEvent.MOUSE_OVER, startHilighter);
                      spBoard.addEventListener (MouseEvent.ROLL_OVER, startHilighter);

                      function startHilighter (e:MouseEvent):void {
                      if (shouldIHilight == true) {
                      myCursor.stage.addEventListener
                      (MouseEvent.MOUSE_MOVE,mouseMoveHandler);
                      } else {
                      spBoard.stage.removeEventListener (MouseEvent.MOUSE_DOWN,boardDown);
                      spBoard.stage.removeEventListener (MouseEvent.MOUSE_UP,boardUp);
                      }
                      }

                      //erase Button
                      _nameOfEraseBtn.addEventListener (MouseEvent.CLICK,eraseClicked);

                      }
                      private function boardDown (e:MouseEvent):void {
                      if (_myCursor.visible==true) {
                      doDraw=true;
                      shDrawing.graphics.moveTo (shDrawing.mouseX,shDrawing.mouseY);
                      }
                      }

                      private function boardUp (e:MouseEvent):void {
                      if (doDraw && _myCursor.visible==true) {
                      shDrawing.graphics.lineTo (shDrawing.mouseX,shDrawing.mouseY);
                      }
                      doDraw=false;
                      }
                      //erase button functions
                      private function eraseClicked (e:MouseEvent):void {
                      shDrawing.graphics.clear ();
                      shDrawing.graphics.lineStyle (_hilightLineSize,_hilightColour);//reset
                      the hilight line size and colour after clearing it on previous line
                      }

                      //-------------------------------------------------------
                      //stop the hilighter by setting shouldIDraw = false
                      public function stopHighlighter () {
                      shouldIHilight=false;
                      }
                      }
                      }


                      • 8. Re: removing a class
                        Rothrock Level 5
                        At first blush that is a lot of code and it is hard for me to follow. When you share more than a couple lines it is generally good to cut it from your AS editor and use the Attach Code button to paste it into. That way it will keeps its indent formatting and line breaks. (You don't get the button with the quick reply you need to use the Reply link.)

                        Also I'm not as facile with AS3 off the top of me head the way I am with AS2. So I'm not seeing all the things right off. But I do see that you are creating at least one sprite inside your intance of Highlighter. So I think you will need to delete that in your destroy() method. Also you have a lot of mouse event listeners. I think you might need to remove those or perhaps change them to weak references.

                        I'm also a little confused by hiLighter and highlighter classes. Do you really need two classes with such similar names? Sounds like it could cause confusion down the road -- especially if you come back to this in six months and want to update or use it again!

                        Here are some other non-post related observations.

                        You are using the Color class, but that has been depreciated in favor of the ColorTransform class -- which you are also using. I would practice using the transform since you've already got it in there.

                        Having a lot of arguments in the constructor can be a pain, but sometimes it is also needed. I would change the bounding box to and instance of a Rectangle() perhaps. And only you know if some of them might be optional(ish). If there are some that you almost always send the same value think about putting them at the end and making them optional.

                        Also I'm not sure if this is true in AS3, but in AS2 if you are using the drawing API you need to clear() it between successive draws or else the player will bog down. I see you have a clear() in the eraseClicked method, but I don't see it anywhere else. I think it needs to be at the start of the MouseMoveHandler function.

                        And I think I would give MouseMoveHandler a better name. Six months from now will you remember that is only handling the MouseMoves during the time after you've startHighlighter and before you stopHighlighter? Something like addToHighlight or drawHighlight or some such seems more evocative to me. (Oh and if Classes always start capital, methods and properties always start lower case, which you have done.)

                        I'm really big on giving things good understandable names. So much trouble comes from poorly taxonomied code. For the most part I would say you've done quite well, better than you think you have.

                        • 9. Re: removing a class
                          Level 7
                          Hi Rothrock,

                          Thank you very much for taking the time to peruse my code. I know this is
                          true altruism on your part and I appreciate it.

                          All of your suggestions were taken to heart. Most of this is stuff here I
                          picked up from a variety of sources and tried to Frankenstein together.
                          That's why things like using the colorTransform and the Color class
                          together - 2 information sources.

                          Your naming suggestions (especially in reference to hiLighter and
                          highlighter) is well noted. I agree and it's on the list of things to
                          address.

                          The info on posting code is appreciated as well. I'm using the forums
                          through my outlook program. Next time I'll log on to the Adobe site and
                          post that way (I just find the Adobe site really slow so I tend to avoid
                          it).

                          Lastly, one last quick question. Your comment about deleting the created
                          Sprite is well noted. Any idea of the code required to do that?

                          This is how it's created:
                          this.spBoard=new Sprite;

                          How would I get rid of it? I know this doesn't work:
                          delete this.spBoard;

                          Would this suffice:
                          this.spBoard = null;

                          I'm pretty happy with how everything has turned out so far. It runs really
                          smoothly and has quite a bit of flexibility and that was the goal of the
                          exercise. Your suggestions (and Mr. Helpy's) have helped me to improve it
                          (and my coding in general) and for that, I thank you again.

                          Cheers,
                          Brock




                          "Rothrock" <webforumsuser@macromedia.com> wrote in message
                          news:g3dt4t$7a3$1@forums.macromedia.com...
                          > At first blush that is a lot of code and it is hard for me to follow. When
                          > you
                          > share more than a couple lines it is generally good to cut it from your AS
                          > editor and use the Attach Code button to paste it into. That way it will
                          > keeps
                          > its indent formatting and line breaks. (You don't get the button with the
                          > quick
                          > reply you need to use the Reply link.)
                          >
                          > Also I'm not as facile with AS3 off the top of me head the way I am with
                          > AS2.
                          > So I'm not seeing all the things right off. But I do see that you are
                          > creating
                          > at least one sprite inside your intance of Highlighter. So I think you
                          > will
                          > need to delete that in your destroy() method. Also you have a lot of mouse
                          > event listeners. I think you might need to remove those or perhaps change
                          > them
                          > to weak references.
                          >
                          > I'm also a little confused by hiLighter and highlighter classes. Do you
                          > really
                          > need two classes with such similar names? Sounds like it could cause
                          > confusion
                          > down the road -- especially if you come back to this in six months and
                          > want to
                          > update or use it again!
                          >
                          > Here are some other non-post related observations.
                          >
                          > You are using the Color class, but that has been depreciated in favor of
                          > the
                          > ColorTransform class -- which you are also using. I would practice using
                          > the
                          > transform since you've already got it in there.
                          >
                          > Having a lot of arguments in the constructor can be a pain, but sometimes
                          > it
                          > is also needed. I would change the bounding box to and instance of a
                          > Rectangle() perhaps. And only you know if some of them might be
                          > optional(ish).
                          > If there are some that you almost always send the same value think about
                          > putting them at the end and making them optional.
                          >
                          > Also I'm not sure if this is true in AS3, but in AS2 if you are using the
                          > drawing API you need to clear() it between successive draws or else the
                          > player
                          > will bog down. I see you have a clear() in the eraseClicked method, but I
                          > don't
                          > see it anywhere else. I think it needs to be at the start of the
                          > MouseMoveHandler function.
                          >
                          > And I think I would give MouseMoveHandler a better name. Six months from
                          > now
                          > will you remember that is only handling the MouseMoves during the time
                          > after
                          > you've startHighlighter and before you stopHighlighter? Something like
                          > addToHighlight or drawHighlight or some such seems more evocative to me.
                          > (Oh
                          > and if Classes always start capital, methods and properties always start
                          > lower
                          > case, which you have done.)
                          >
                          > I'm really big on giving things good understandable names. So much trouble
                          > comes from poorly taxonomied code. For the most part I would say you've
                          > done
                          > quite well, better than you think you have.
                          >
                          >
                          >


                          • 10. Re: removing a class
                            Rothrock Level 5
                            See that is one of those things I don't know off the top of my head. I'm guessing that you might have to remove the sprite from the display list and then you should be able to delete the instance. You might also need to remove any event listeners or have weak references, but I seem to recall there was some problem with that as well.

                            You might want to make a new post in the AS3 section just on the topic of removing Sprite instances.
                            • 11. Re: removing a class
                              Level 7
                              Fair 'nuff.

                              For my purposes, it's not too big a deal to delete the sprite because when I
                              delete the sprite, I'll delete the highlight that is drawn inside it and I
                              don't want to do that. Maybe another question for another project.

                              I'm not sure if you care (you might be sick of this whole thing by now!),
                              but I uploaded my working model so that you could get a sense of what it was
                              I was trying to create. The end use of this is to allow students with
                              learning disabilities to learn a specific study strategy that vastly
                              increases their study retention.
                              http://www.inlets.net/test/hilite/

                              In the mock-up, the hilighter colour, the size of the highlight area, the
                              text - everything can be controlled dynamically which allows it to work for
                              different grade levels and different subjects.

                              Again, thanks a bunch for all your help.

                              Cheers,
                              Brock



                              "Rothrock" <webforumsuser@macromedia.com> wrote in message
                              news:g3emvn$65o$1@forums.macromedia.com...
                              > See that is one of those things I don't know off the top of my head. I'm
                              > guessing that you might have to remove the sprite from the display list
                              > and
                              > then you should be able to delete the instance. You might also need to
                              > remove
                              > any event listeners or have weak references, but I seem to recall there
                              > was
                              > some problem with that as well.
                              >
                              > You might want to make a new post in the AS3 section just on the topic of
                              > removing Sprite instances.
                              >