18 Replies Latest reply on Jun 27, 2006 6:24 PM by SPGAnne

    Class use of onRollOver

    SPGAnne
      I am a newbie, so pardon any stupidity up front. I've been studying Colin Moock's book Essential Action Script 2.0. I would like to enhance his Ball Class discussed in Chapter 4 so that it responds to onRollOver, onPress etc. (i.e. the button like methods for the MovieClip class). I have been successful in creating functions (outside of the class definition, on the timeline) where I have this code:
      creatBox ("my box prototype");

      function createBox(clipName:String) : Void {
      this.attachMovie(clipName, "box_mc", this.getNextHighestDepth());
      }

      box_mc.onRollOver = function() {
      trace ("roll over" );
      }

      But how do I put that onRollOver capability inside a class? I tried using the techniques described on page 208 about composition, but the Class doesn't ever get a onRollOver method called. I also tried using the technique described on page 85 of registering the Box class to receive Mouse events i.e., Mouse.addListener(this); However the Mouse object apparently doesn't have onRollOver, just MouseMove. Does that mean I have to write some fancy code to see if the Mouse is over me? Yuk, that must mean I also need to do stuff for all the other things like up inside, up outside etc. I hope there is a clever thing I am just missing.

      Thanks to anyone who can help me out.

        • 1. Re: Class use of onRollOver
          Peter Lorent Level 2
          You can use a method in the class like attached code. Pass the target mc to the method. If you want to write code on a frame, make the method public.
          • 2. Re: Class use of onRollOver
            SPGAnne Level 1
            Dear Mr./Ms. LuigiL,
            Thank you so much for your willingness to help me. Sorry to be dense, but who/how is the setRollOver method invoked? Specifically, my Box (sorry I called it Ball before) class looks something like this:

            class Box {
            // This is from Colin Moock's book Essential Action Script chapter 4 with modifications
            private var width:Number;
            private var height:Number;
            private var container_mc:MovieClip;

            // Constructor
            public function Box(w:Number, h:Number,
            x:Number, y:Number,
            target:MovieClip, depth:Number) {
            container_mc = target.attachMovie("M-Box Prototype","container_mc",depth);
            setWidth(w);
            setHeight(h);
            setX(x);
            setY(y);
            }
            // after this I have the methods for set/get Width, Height, X, Y and have now added your code for setRollOver
            }

            I put all of the above in a .as file called Box.as Then on my main timeline I have the following:

            var b:Box = new Box(20,20,1,1,this,this.getNextHighestDepth());

            When I run it in test mode, the box appears nicely on the mainstage, but when I roll my mouse over the box, nothing happens. Again, what I would like is for my nifty box object to automagically receive the onRollOver events associated with a movie clip. Since the ball is actually "drawn" by a movie clip (using the .attachMovie inside the constructor) I presume that movie clip is getting an onRollOver event, but I don't know how to get that to trigger another method inside the Class. Thanks again for your help.

            • 3. Re: Class use of onRollOver
              Peter Lorent Level 2
              Add the following line as the last line to the constructor:
              setRollOver(container_mc);


              public function Box(w:Number, h:Number,
              x:Number, y:Number,
              target:MovieClip, depth:Number) {
              container_mc = target.attachMovie("M-Box Prototype","container_mc",depth);
              setWidth(w);
              setHeight(h);
              setX(x);
              setY(y);
              setRollOver(container_mc);
              }
              • 4. Re: Class use of onRollOver
                SPGAnne Level 1
                Thanks very much! That did the trick. I'm not sure I really understand how events get passed around in Flash (seems a bit convoluted to me at this point), but I'll save that to ponder another time I guess.
                • 5. Re: Class use of onRollOver
                  SPGAnne Level 1


                  Ooops, another thing happened that I don't understand. I have my box responding to onRollOver fine now, but inside that function I don't seem to have access to the instance variables or instance methods. I have this code:
                  class Box {
                  // This is from Colin Moock's book Essential Action Script chapter 4 with modifications by agt
                  private var width:Number;
                  private var height:Number;
                  private var container_mc:MovieClip;

                  // Constructor
                  public function Box .....
                  ...
                  container_mc = target.attachMovie("M-Box Prototype","container_mc",depth);
                  setWidth(w);
                  setHeight(h);
                  setX(x);
                  setY(y);
                  setRollOver(container_mc);
                  }
                  ......
                  private function setRollOver(target:MovieClip):Void{
                  target.onRollOver=function():Void{
                  trace("You hit me!");
                  trace("my width and height are " + width + "," + height);
                  }

                  width and height are undefined. How can I still have access to the instance variables and methods from inside that onRollOver function?
                  • 6. Re: Class use of onRollOver
                    Peter Lorent Level 2
                    trace("my width and height are " + this.width + "," + this.height);
                    • 7. Re: Class use of onRollOver
                      SPGAnne Level 1
                      First, thank you again for your willingness to help me.

                      Bummer, I'm still getting undefined. When I just put trace (this); I get level0.container_mc. So it appears it thinks "this "is the movie clip, not the instance of the box class.

                      In case you want to copy and past all of the box class code, here it is.
                      class Box {
                      // This is from Colin Moock's book Essential Action Script chapter 4 with modifications by agt
                      private var width:Number;
                      private var height:Number;
                      private var container_mc:MovieClip;

                      // Constructor
                      public function Box(w:Number, h:Number,
                      x:Number, y:Number,
                      target:MovieClip, depth:Number) {
                      //container_mc = target.createEmptyMovieClip("boxcontainer" + depth, depth); // going to attach a movie clip instead
                      container_mc = target.attachMovie("M-Box Prototype","container_mc",depth); //my modification
                      trace ("trying to create a new box");
                      trace (container_mc);
                      setWidth(w);
                      setHeight(h);
                      setX(x);
                      setY(y);
                      setRollOver(container_mc);
                      }

                      public function getWidth():Number {
                      return width;
                      }
                      public function setWidth(w:Number):Void {
                      width = w;
                      //draw(); don't use because I'm using a movie clip instead
                      }
                      public function getHeight():Number {
                      return height;
                      }
                      public function setHeight(h:Number):Void {
                      height = h;
                      //draw(); don't use because I'm using a movie clip instead
                      }
                      public function getX():Number {
                      return container_mc._x;
                      }
                      public function setX(x:Number):Void {
                      container_mc._x = x;
                      }
                      public function getY():Number {
                      return container_mc._y;
                      }
                      public function setY(y:Number):Void {
                      container_mc._y = y;
                      }
                      public function draw():Void{
                      trace ("trying to draw a new box");
                      //container_mc.clear();
                      //container_mc.lineStyle(1, 0x000000);
                      //container_mc.moveTo(0,0);
                      //container_mc.lineTo(width,0);
                      }
                      private function setRollOver(target:MovieClip):Void{
                      target.onRollOver=function():Void{
                      trace("You hit me!");
                      trace (this);
                      trace("my width and height are " + this.width + "," + this.height);
                      }
                      }

                      }
                      • 8. Re: Class use of onRollOver
                        Peter Lorent Level 2
                        God is in the details:
                        trace("my width and height are " + this._width + "," + this._height);
                        ->underscores on the properties _width and _height
                        • 9. Class use of onRollOver
                          Peter Lorent Level 2
                          And to elaborate:

                          >>When I just put trace (this); I get level0.container_mc.
                          That's correct. attachMovie() returns a reference to the newly attached mc and you are saving that reference in the instance variable container_mc. Inside the onRollOver you're in the scope of 'target' so the keyword this returns _level0.container_mc

                          The getter/setter methods:
                          public function getWidth():Number {
                          return container_mc._width;
                          }
                          public function setWidth(w:Number):Void {
                          container_mc._width = w;
                          //draw(); don't use because I'm using a movie clip instead
                          }
                          public function getHeight():Number {
                          return container_mc._height;
                          }
                          public function setHeight(h:Number):Void {
                          container_mc._height = h;
                          //draw(); don't use because I'm using a movie clip instead
                          }

                          And then you can use:
                          private function setRollOver(target:MovieClip):Void{
                          // a local reference to the current class
                          var thisObj:Box=this;
                          target.onRollOver=function():Void{
                          trace("You hit me!");
                          trace (this);
                          trace("my width and height are " + thisObj.getWidth() + "," + thisObj.getHeight());
                          }
                          }
                          • 10. Re: Class use of onRollOver
                            SPGAnne Level 1

                            >>God is in the details.
                            AMEN!

                            Profuse Thanks LuigiL!! I'm really struggling to get some key concepts through my head, so if you don't mind my asking one more (I hope) question. Can you help me with a conceptual understanding of why the function inside the setOnRollOver function can't seem to be aware of the object (and the object's instance variables and methods) that it is ultimately inside? (I actually did a little experiment to see if I could declare the thisObj:Box variable as an instance variable and then initialize it to "this" inside the constructor. But again, the function inside the setOnRollOver function thought thisObj was undefined). Your continued help is very much appreciated.

                            a humble newbie,
                            SPGAnne
                            • 11. Class use of onRollOver
                              Peter Lorent Level 2
                              Inside call back functions - like onRollOver, onLoad - class members go out of scope (and for that reason you can't use an instance variable...). One way of solving this is to use a local variable - local to the function that is - with a reference to the current class (in effect the current object).
                              -> inside the setRollOver method I declare a local variable thisObj of datatype Box.
                              Now you can access the class members through the local variable thisObj.

                              In some cases you could use a class variable -> private static var thisObj:Box;
                              The static variable is the same for all instances of the class.

                              Another way to solve the problem is to use the Delegate Class which in effect is a static method that allows you to delegate events to specific scopes and functions.

                              On the side:
                              Colin's book is known as a hardcore AS-book. To fully comprehend the content, you will need a good understanding of the basics. If you are - as you mention - new to the language you might be in over your head with this book.
                              • 12. Re: Class use of onRollOver
                                SPGAnne Level 1
                                Thank you very much again for your very helpful reply.

                                WRT being in a little over my head. That is probably quite true. I also purchased Moock's Action Script 2.0, The Definitive Guide and read through most of that too. However, I found it a little difficult as it is less application oriented than the Essential Action Script book. All the other books I've seen in the stores have a lot of stuff about drawing , animation and such and simple interactions written on the timeline, and not that much about writing real applications. If you have a book in mind that you would recommend, I'm all ears. Again, I need to learn how to write applications that are fairly complex interactions - not just click a button and run an animation, and not corporate forms and database stuff. I write education products that simulate pretty complicated things. I express this through nice animations and user interactions but also have some complicated abstract stuff going on behind the scenes.

                                In my current tool set I rely heavily on a message broadcast capability which doesn't seem to be built into Flash. (Flash actually seems like a giant step backward for me in many ways, but that's a whole other discussion.) At any rate, I need to slam myself up the learning curve of how to do that. Thus Colin's book and the design pattern section at the back that describes all that. So, if you think there is another book that would help, I'd be much obiliged to hear your recommendation.

                                Thank you again for your willing help.
                                • 13. Re: Class use of onRollOver
                                  Peter Lorent Level 2
                                  >>Action Script 2.0, The Definitive Guide
                                  That should be: ActionScript, The Definitive Guide and that's not AS2 but AS1. And that can be confusing... when you are studying AS2.

                                  Of course I can't really judge if the book (Essential ActionScript that is) is the right choice at this time. I was solely responding to the word 'newbie' and the book is not for newbies, at least not in my opinion. But, if you have programming experience you might be able to work your way through the book. Without a doubt, you will profit from first covering the basics and in my opininion 'Flash MX 2004 actionscript' by Derek Franklin and Jobe Makar is an excellent - hands on - source.

                                  >>a message broadcast capability
                                  No. Flash has the EventDispatcher and ASBroadcaster classes (and you can achieve quite a lot with them) but not a full fledged delegation event model. Then you need to study Colin's book -> Design Patterns -> Delegation Event Model.
                                  • 14. Re: Class use of onRollOver
                                    SPGAnne Level 1
                                    I didn't have the book in front of me when I was replying (sorry about that) but it is called ActionScript for Flash MX, The Definitive Guide 2nd Edition. So is that still Action Script 1? I thought it was Actionscript 2 that shipped with Flash MX. So if not, I guess I am more confused than I thought. Ah well. I will look into the other book you have recommended and will continue to study the Moock books and try to understand the other classes you thought I might be able to use.

                                    Another thing that seems really strange to me is that they have a Key Class, but not a Cursor Class. It would seem that lots and lots of people would like to create custom cursors/change the appearance of the cursor. Kind of odd to be on release 8 of a product and not have something so basic as that (Am I missing something here?). At any rate, I am attempting to write such a Class. (I hope I can figure this one out on my own, and not have to pester all of you experts out there.) I need to be able to do such things as allow a user to pick up and put down various tools as well as make the cursor change it's appearance if you move it over certain items such as technical words where we provide pronunciations etc.
                                    • 15. Class use of onRollOver
                                      Peter Lorent Level 2
                                      AS2 shipped with MX 2004 (2004 being the important addition to the product name).

                                      >>(Am I missing something here?)
                                      Flash has a (toplevel) Mouse Class and you can (easily) change the appearance by using movieclips.
                                      • 16. Re: Class use of onRollOver
                                        scriptkiddie123
                                        The fact that SPGAnne cannot use the onRollOver just like every other method in a class, is a bad implementation of the Actionscript language. I see no conceptual reason why there should be a problem to do it, aside from the fact that they forgot to implement it in Macromedia.
                                        Now we need that ugly, clumsy workaround. Oh well.
                                        • 18. Re: Class use of onRollOver
                                          SPGAnne Level 1
                                          Well, I would agree it is a bummer to me. I have to say I'm struggling with the way Flash does stuff. It seems a lot more convoluted than other tools I've used. That's the bad news. The good news is that it has a really great community of people who have figured it out and are willing to help those of use stumbling around. I would say that LuigiL is most DEFINITELY one of those patient souls. And in my book, that's REALLY good news. I hope to eventually get through the knot hole and start thinking the way Flash likes to do stuff so that it's not such a struggle.

                                          a humble newbie,
                                          SPGAnne