1 Reply Latest reply: Apr 13, 2012 4:21 AM by Ned Murphy RSS

    Hit Test Object..

    AnimatedCircle

      Hi ii want my character to walk down the screen and hit the objectWall and then a movieclip then gets added to the stage and plays but it doesnt seem to work and ii dont know why.

       

      import flash.display.MovieClip;

      import flash.ui.Keyboard;

      import flash.events.Event;

       

       

      stop();

       

       

      //1. Set booleans for the status of the arrow presses

       

       

      var downArrow:Boolean = false;

       

       

      //2. Add event listeners for the keys

       

       

      stage.addEventListener(KeyboardEvent.KEY_UP, nokeyHit);

      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHit);

      stage.addEventListener(Event.ENTER_FRAME, moveObject);

       

       

      //3. Function to detect if a key has been hit

       

       

      function keyHit(event:KeyboardEvent):void

      {

                switch (event.keyCode)

                {

                          case Keyboard.DOWN :

                                    downArrow = true;

                                    break;

                }

      }

       

       

      //4. Function to detect if a key has not been hit

       

       

      function nokeyHit(event:KeyboardEvent):void

      {

                switch (event.keyCode)

                {

                          case Keyboard.DOWN :

                                    downArrow = false;

                                    break;

                }

      }

       

       

      //5. Function to move the object

       

       

      function moveObject(event:Event):void

      {

                var speed:Number = 2;

                if (downArrow)

                {

                          objectMove.y +=  speed;

                }

                hitWalls();

      }

       

       

      //6. Function to test hitting the walls

       

       

      function hitWalls():void

      {

       

       

                var left:Number = 0;

                var right:Number = stage.stageWidth;

                var top:Number = 0;

                var bottom:Number = stage.stageHeight;

       

       

       

       

                if (objectMove.x  - (objectMove.width / 2) > right)

                {

                          objectMove.x = left - objectMove.width / 2;

                }

                else if (objectMove.x + objectMove.width / 2 < left)

                {

                          objectMove.x = right + objectMove.width / 2;

                }

       

       

                if (objectMove.y + (objectMove.height / 2) > bottom)

                {

                          objectMove.y = bottom - (objectMove.height / 2);

                }

                else if (objectMove.y - (objectMove.height / 2) < top)

                {

                          objectMove.y = top + (objectMove.height / 2);

                }

      }

       

       

      objectMove.addEventListener(Event.ENTER_FRAME, testHit);

       

       

      function testHit(event:Event):void

      {

                var Attack:MovieClip = new MovieClip;

       

                if (objectMove.hitTestObject(objectWall))

                {

                          objectMove.removeEventListener(Event.ENTER_FRAME, testHit);

                          addChild(Attack);

                }

      }

       

      Any Help Would Be Great Thanks.


        • 1. Re: Hit Test Object..
          Ned Murphy MVP

          The problem is in this line...

           

              var Attack:MovieClip = new MovieClip;

           

          You are just adding a MovieClip object.  It will be an invisible thing because it contains nothing.  If you happen to have some other object that you have assigned a class name, maybe like AttackMC that sits in the library waiting to be added, you would use...

           

          var Attack:AttackMC = new AttackMC();

           

          Nore: It is standard practice to only use Capitalized names for classes, not variables... it just helps someone else understand your code easier.