7 Replies Latest reply on Jun 12, 2014 2:59 PM by Ned Murphy

    Checking for hitTests


      Its me again with my daily questions


      So I am trying to add a scoring system to my game. I have these coins that are added to the stage using code (the class name for them is Coin) and my character has an instance name of "player"

      So my question is how do I check for hitTests between the player and the coins?

        • 1. Re: Checking for hitTests
          Ned Murphy Adobe Community Professional & MVP

          It depends on which version of Actionscript you are using.  If it's AS2 it could be something like...




          if it is AS3 it could be something like...




          Have you tried using Google as a reference tool for your daily questions?  If you were to search using terms like "AS# hitTest tutorial" (replace # with 2 or 3) you'd probably find all you need.


          • 2. Re: Checking for hitTests
            DeathAngle Level 1

            When i do

            if(player.hitTestObject(Coin)) {

            I get error 1067  "Implicit coercion of a value of type Class to an unrelated type flash.display:DisplayObject."

            Now what the hell that means beats me. I am trying to find the answer on google right now but i also posted on this forum just to see which is quicker, so far no luck with google


            Again "player" is the instance name for my main character and Coin is the class/the Coin.as file that is suppose to dynamically add the coins to the game

            • 3. Re: Checking for hitTests
              Ned Murphy Adobe Community Professional & MVP

              If Coin is the class name then the error should be clear for you to understand.  You use instance names (or references to the instances) to target objects in code, not their class names.

              1 person found this helpful
              • 4. Re: Checking for hitTests
                DeathAngle Level 1

                I realize that but im not sure how to give it an instance name because it isint actually on the stage until it gets placed by code


                here is my Coin.as file


                package  {
                        import flash.display.MovieClip;
                        import flash.events.Event;

                        public class Coin extends MovieClip {
                            public function Coin(xLocation:int, yLocation:int) {
                             // constructor code
                                x = xLocation;
                                y = yLocation;


                            public function removeSelf():void {
                                trace("remove coin");



                And then on the timeline in the actions layer I have the coordinates to place the coin, it gets placed correctly I just don't know how to make it registor for hits (AS3)

                • 5. Re: Checking for hitTests
                  Ned Murphy Adobe Community Professional & MVP

                  Where do you plan to do the hitTest,in your main file?  If so, then the code you'll need to use will depend on how you add the coins.  Usually if there will be a number of them then you would fill an array with references to them and loop thru the array to check for hitTests.  That might be something like the following...


                  var coinArray = new Array(); // somewhere more global in your main code


                  // then in the code where you add a new Coin instance

                  var coin:Coin = new Coin();



                  then your hitTests would use the array...


                  for(var i:int=0; i<coinArray.length; i++){




                  • 6. Re: Checking for hitTests
                    DeathAngle Level 1

                    Thankyou very much, it works perfect now, I have almost 500 lines of code now so keeping it all organized is starting to be a pain in the ***

                    • 7. Re: Checking for hitTests
                      Ned Murphy Adobe Community Professional & MVP

                      You're welcome