14 Replies Latest reply on Apr 12, 2010 6:48 PM by kamlam4

    use of hitTestObject()

    kamlam4

      In my program,  the movieclips are randomly added on the stage,  If I want to check whether two movieclips from different classes is colllided.

      As far as all the moveclips are of the different classes which are randomly added on the stage in my program,

      for the use of hitTestObject(),

       

      I find that I need to have two movieclips identified so that the command will be:

      mc1.hitTestObject(mc2)

       

      maybe I may ask, before I add a mc on the stage "addChild(mc)",

      I will call a function to do the hitTestObject command "mc.addEventListener(Event.ENTER_FRAME, hitTest)"

      so that I can call evt.target to be mc1, is that true or not?

      I am confused what should I replace mc1 and mc2?

       

      Please give me some advice~thank you very much=]

        • 1. Re: use of hitTestObject()
          kglad Adobe Community Professional & MVP

          you don't need an enterframe loop.  just call a function to execute the hittests.

          • 2. Re: use of hitTestObject()
            kamlam4 Level 1

            but what should I put in A and B?

             

            A.hitTestObject(B)

            • 3. Re: use of hitTestObject()
              kglad Adobe Community Professional & MVP

              one will be the movieclip you just added (and may want to move or remove) and the other will loop (using a for-loop) through the other on-stage objects.

              • 4. Re: use of hitTestObject()
                kamlam4 Level 1

                therefore, I just need to write like this:

                A.hitTestObject()

                 

                as A is the movieclip I just added?=]

                • 5. Re: use of hitTestObject()
                  kglad Adobe Community Professional & MVP

                  you'll do something like:


                  A.x=
                  A.y=

                  addChild(A);


                  if(checkForOverlapF(A)){

                  // do whatever.  possible remove or repositionA

                  }


                  function checkForOverLapF(dobj:DisplayObject):Boolean{

                  for(var i:uint=0;i<numChildren){

                  if(getChildAt(i)!=dobj){

                  if(dobj.hitTestObject(getChildAt(i))){

                  return true;

                  }

                  }

                  }

                  return false;

                  }

                  • 6. Re: use of hitTestObject()
                    kamlam4 Level 1

                    This is part of my funcrion:

                     

                    function addSmallFish(evt:MouseEvent):void{
                        var classNames:Array = ["bigfish", "smallfish"];
                        var ClassRef:Class = Class(getDefinitionByName(classNames[Math.floor(Math.random()*classNames.length)]));
                        var classInstance:* = new ClassRef();
                        addChild(classInstance);
                        classInstance.addEventListener(Event.ENTER_FRAME, isKill);
                    }

                     


                    function isKill(evt:Event):void{
                        if(checkForOverlapF(evt)){   This function contain an error 1180:call to possibly undefined method
                            trace("Yes");
                            }
                    }


                    function checkForOverLapF(dobj:DisplayObject):Boolean{
                        for(var i:uint=0;i<numChildren;i++)
                        {
                            if(getChildAt(i)!=dobj)
                            {
                                if(dobj.hitTestObject(getChildAt(i)))
                                {
                                    return true;
                                }
                            }
                        }return false;
                    }

                     

                    Is there something wrong with this?=]

                    • 7. Re: use of hitTestObject()
                      kglad Adobe Community Professional & MVP

                      again, you shouldn't be using an enterframe loop.

                       

                      and an event is not a displayobject.

                       

                      use:

                       

                      function addSmallFish(evt:MouseEvent):void {
                          var classNames:Array = ["bigfish", "smallfish"];
                          var ClassRef:Class = Class(getDefinitionByName(classNames[Math.floor(Math.random()*classNames.length )]));
                          var classInstance:* = new ClassRef();
                          addChild(classInstance);
                          checkForOverLapF(classInstance);
                      }


                      function checkForOverLapF(dobj:DisplayObject):Boolean {
                          for (var i:uint=0; i<numChildren; i++) {
                              if (getChildAt(i)!=dobj) {
                                  if (dobj.hitTestObject(getChildAt(i))) {
                                      return true;
                                  }
                              }
                          }
                          return false;
                      }

                      • 8. Re: use of hitTestObject()
                        kamlam4 Level 1

                        thank you very much for telling me the mistakes =] anyway, I find that I cannot see any outputs since I have added a trace function

                        and may I ask the use of "dobj"? and if I cannot get any output "in", may it be the matter of my movieclips (I have add an evetlistener to my movieclip so that they can swim automatically)?

                         

                        function checkForOverLapF(dobj:DisplayObject):Boolean {
                            for (var i:uint=0; i<numChildren; i++) {
                                if (getChildAt(i)!=dobj) {
                                    if (dobj.hitTestObject(getChildAt(i))) {

                                    trace("in");
                                        return true;
                                    }
                                }
                            }
                            return false;
                        }

                        • 9. Re: use of hitTestObject()
                          kglad Adobe Community Professional & MVP

                          that hit test only executes once when the objects are created.  have you seen an overlap when the objects are created and FAILED to see your trace?

                          • 10. Re: use of hitTestObject()
                            kamlam4 Level 1

                            I see they overlap but no trace

                            how should I improve my codes?

                            • 11. Re: use of hitTestObject()
                              kglad Adobe Community Professional & MVP

                              have you seen an overlap when the objects are created and FAILED to see  your trace?

                              • 12. Re: use of hitTestObject()
                                kamlam4 Level 1

                                yes~so I am confused what is wrong with the code

                                • 13. Re: use of hitTestObject()
                                  kglad Adobe Community Professional & MVP

                                  if that code is part of the document class or attached to a timeline, there should be a positive hittest when the 2nd object is created.  if it's anywhere else, like some non-document class, that would cause a problem.

                                  • 14. Re: use of hitTestObject()
                                    kamlam4 Level 1

                                    what is document class mean?@@"

                                    and may I ask as the movieclips have inserted eventlistener to do the move and rotation

                                    if I want to make them do the move and rotation in the main function but not inside the movieclip

                                    what should I do?

                                     

                                    these are the codes in the movieclip:

                                    stop();
                                    var targetX:Number = Math.random()*stage.stageWidth;
                                    var targetY:Number = Math.random()*stage.stageHeight;
                                    var speed:Number = Math.random()/10;
                                    var targetAlpha = Math.random()*1;
                                    var targetScale = Math.random()*0.8+0.2;
                                    this.alpha = targetAlpha;
                                    this.scaleX = this.scaleY = targetScale;
                                    this.x = stage.stageWidth/2;  //add the object at the center
                                    this.y = stage.stageHeight/2;
                                       
                                    this.addEventListener(Event.ENTER_FRAME,onEnterFrameEvent_SF);
                                    function onEnterFrameEvent_SF(e:Event) {   
                                        if (Math.ceil(Math.random()*100)== 1) {
                                            targetX = Math.random()*stage.stageWidth;
                                            targetY = Math.random()*stage.stageHeight;
                                            speed = Math.random()/50;      
                                            targetAlpha = Math.random()*1;     
                                           
                                        }
                                        this.rotation = (Math.atan2(targetY-this.y, targetX-this.x )/Math.PI)*180;
                                        this.x += (targetX-x)*speed;
                                        this.y += (targetY-y)*speed;
                                        this.alpha += (targetAlpha-alpha)*speed;
                                    }