3 Replies Latest reply on Oct 28, 2011 9:35 PM by kglad

    Problems with hitTestObject

    RensStudent

      Hey,

       

      this is my first year of flash and we have an assignment where we must drive with a car to a finishline.

      making the car move isnt a problem, but there has to be 6 objects you must avoid when driving around.

       

      when i drive around and i hit my object i should avoid, it stops and i cant move around. and instead i want to be able to corrigate my direction.

       

      some of my terms are in dutch i'll try to make some commentary in english.

       

      visual idea how it looks like

       

      adobe help.png

      i made 4 car objects that look a direction

      up = Autoboven

      down = Autoonder

      left = Autolinks

      right = Autoright

       

       

      import flash.display.MovieClip;
      import flash.utils.Timer;
      import flash.events.TimerEvent;
      import flash.text.TextField;
      
       // kegel = cone
      var clipArray:Array = [kegel1,kegel2,kegel3,kegel4,kegel5,kegel6];
      var auto:MovieClip = new Autolinks();
      var finish:MovieClip = new Finish();
      addChild(auto);
      auto.x= 490;
      auto.y= 80;
      var richting:int = 0;
      //richting  = direction
      
      //tijd = time
      var tijd:int = 0;
      var txtFld:TextField = new TextField();
      addChild(txtFld);
      txtFld.x = 500;
      txtFld.y = 370;
      var timer:Timer = new Timer(1000);
      timer.addEventListener(TimerEvent.TIMER, onTimer);
      timer.start();
      
      
      function onTimer(evt:TimerEvent):void{
                tijd +=1;
                txtFld.text = (tijd) + " sec";
      }
      
      
                var clip:MovieClip;
      
      stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyPressed);
      function onKeyPressed(evt:KeyboardEvent):void{
                switch (evt.keyCode){
                          case Keyboard.LEFT:
                                    changeLeft();
                          break;
      
                          case Keyboard.RIGHT:
                                     changeRight();
                          break;
      
                          case Keyboard.DOWN:
                                    changeDown();
                          break;
      
                          case Keyboard.UP:
                                    changeUp();
                          break;
                }
      }
      
      
      function changeLeft()
      {
                          if (richting != 0)
                          {
                                    clip= new Autolinks();
                                    changeRichting(clip);
                                    richting = 0;
                          }
                          if(auto.x > 100 && !( isHitting() ) )
                          {
                                    auto.x -= 10;
                          }
                           else if( isHitting() )
                          { 
      
      
                          }
                          else if (auto.x <= 100){
                                    stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
                                    removeChild(auto);
                                    finish.x = 225;
                                    finish.y = 200;
                                    addChild(finish);
                                    timer.stop();
                          }
      }
      
      
      
      
      function changeRight()
      {
                          if (richting != 1)
                          {          clip= new Autorechts();
                                    changeRichting(clip);
                                    richting = 1;}
      
                          if (!(auto.x < (stage.stageWidth-auto.width)) && !(isHitting()))
                          {          auto.x += 10; }
      
      }
      function changeUp()
      {
                          if (richting != 2)
                          {
                                    clip = new Autoboven();
                                    changeRichting(clip);
                                    richting = 2;
                          }
                          if ((auto.y > 0)&& !(isHitting()))
                          {
                                    auto.y -= 10;
                          }
      }
      function changeDown()
      {
               if (richting != 3)
                {
                     clip= new Autoonder();
                     changeRichting(clip);
                     richting = 3;
                } 
                if ((auto.y < stage.stageHeight) && !(isHitting()))
                { 
                     auto.y += 10; 
                }
      }
      
      
      function changeRichting(clip:MovieClip)
      {
                var xpos:int = auto.x;
                var ypos:int = auto.y;
                removeChild(auto);
                auto = clip;
                addChild(auto);
                auto.x = xpos;
                auto.y = ypos;
      }
      
      
      function isHitting():Boolean 
      {
           for (var i:int = 0; i < clipArray.length; i++){
                          if( auto.hitTestObject( clipArray[i] )){
                                    return true;
                          }
        }
        return false
      }
      

       

      i dont have  big experience in programming in flash, and this code can be very hard to understand

       

      any help or tips will be appreciated much!

       

      Greetz Rens

        • 1. Re: Problems with hitTestObject
          kglad Adobe Community Professional & MVP

          you're not doing anything when isHitting() returns true.  why don't you add some code to that part of your app.

          • 2. Re: Problems with hitTestObject
            RensStudent Level 1

            I know, but i cant figure out how i'm able to do it.

             

            so when i hit an object when i pres the left key , i want to be able to only use up/down/right to corrigate.

             

            do u have any ideas how i'm able to start solving that?

             

             

            thnx

            • 3. Re: Problems with hitTestObject
              kglad Adobe Community Professional & MVP

              here's changeLeft().  the rest are all similar:

               

               

               

              import flash.display.MovieClip;
              import flash.utils.Timer;
              import flash.events.TimerEvent;
              import flash.text.TextField;
              
               // kegel = cone
              var clipArray:Array = [kegel1,kegel2,kegel3,kegel4,kegel5,kegel6];
              var auto:MovieClip = new Autolinks();
              var finish:MovieClip = new Finish();
              addChild(auto);
              auto.x= 490;
              auto.y= 80;
              var richting:int = 0;
              //richting  = direction
              
              //tijd = time
              var tijd:int = 0;
              var txtFld:TextField = new TextField();
              addChild(txtFld);
              txtFld.x = 500;
              txtFld.y = 370;
              var timer:Timer = new Timer(1000);
              timer.addEventListener(TimerEvent.TIMER, onTimer);
              timer.start();
              
              
              function onTimer(evt:TimerEvent):void{
                        tijd +=1;
                        txtFld.text = (tijd) + " sec";
              }
              
              
                        var clip:MovieClip;
              
              stage.addEventListener(KeyboardEvent.KEY_DOWN , onKeyPressed);
              function onKeyPressed(evt:KeyboardEvent):void{
                        switch (evt.keyCode){
                                  case Keyboard.LEFT:
                                            changeLeft();
                                  break;
              
                                  case Keyboard.RIGHT:
                                             changeRight();
                                  break;
              
                                  case Keyboard.DOWN:
                                            changeDown();
                                  break;
              
                                  case Keyboard.UP:
                                            changeUp();
                                  break;
                        }
              }
              
              
              function changeLeft()
              {
                                  if (richting != 0)
                                  {
                                            clip= new Autolinks();
                                            changeRichting(clip);
                                            richting = 0;
                                  }
                                  if(auto.x > 100 && !( isHitting() ) )
                                  {
                                            auto.x -= 10;
                                  }
                                   
                                  else if (auto.x <= 100){
                                            stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
                                            removeChild(auto);
                                            finish.x = 225;
                                            finish.y = 200;
                                            addChild(finish);
                                            timer.stop();
                                  }
              if(isHitting()){
              auto.x+=10;
              }

              }