14 Replies Latest reply on Apr 27, 2011 5:37 AM by Ned Murphy

    Hit Test Dosent Work

    LukeElChul Level 1

      im making a type of game where you send your troops out to battle when you click the button to buy them

      this is my script what gets put in to the troops so thay stop and attack the target.

       

      onClipEvent (enterFrame) {

           if (this.hitTest(enemyHouse)) {

               _x+=0.0;

               _root.giant_mc.giant_walking.gotoAndPlay(9);

           } else {

              _x+=0.6;

          }

      }

       

      so your troop what in this case is a giant(giant_mc) walks when he spawns untill he hits the enemyHouse then he stops and goes to frame 9 his attacking frames, but that doesent happen he just walks past the enemyHouse and he dosent stop and animate.

        • 1. Re: Hit Test Dosent Work
          Ned Murphy Adobe Community Professional & MVP

          Learn to use the trace function to see the status of things.  For example, check to make sure "this" and "enemyHouse" are what you think they are...

           

          onClipEvent (enterFrame) {

               trace(this);

               trace(enemyHouse);

               if (this.hitTest(enemyHouse)) {

                   _x+=0.0;

                   _root.giant_mc.giant_walking.gotoAndPlay(9);

               } else {

                  _x+=0.6;

              }

          }

          1 person found this helpful
          • 2. Re: Hit Test Dosent Work
            LukeElChul Level 1

            that didn't work but it helped me find the problem, the problem is that when giant_mc gets attached to the scene then duplicated it doesn't have instance name. so is there a way to call the symbol name in a hit test instead of the instance name are can I give giant_mc a instance name in this script.

             

            on (release) {

                 if (cash>=140) {

                     this.attachMovie("giant_mc","giant_mc",this.getNextHighestDepth, {_x:95.1,_y:485});

                    _root.giant_mc.duplicateMovieClip("giant_mc_"+"x", x);

                     setProperty("giant_mc_"+"x", _x, 95.1);

                     setProperty("giant_mc_"+"x", _y, 485);

                     x++;

                     cash-=140;

                } else {

                    cash-=0;

                 }

            }

            • 3. Re: Hit Test Dosent Work
              Ned Murphy Adobe Community Professional & MVP

              That what using the trace function is supposed to do... help you find problems.

               

              Don't use the duplicateMovieClip method.  Just use the attachMovie, and the second argument in the attachMovie call is the instance name, so use it.

              1 person found this helpful
              • 4. Re: Hit Test Dosent Work
                LukeElChul Level 1
                it still doesn't work
                it comes up with this

                 

                undefined _level0.giant_mc.giant_walking

                alright im going to tell u an overview so it becomes clearer what im triying to work out
                so what needs to happen is that when you click a btn called giant_btn it spawns a giant you can spawn as many giants is you want until you run out of cash
                that's what this scripts for

                on (release) {

                     if (cash>=140) {

                        this.attachMovie("giant_mc","giant_mc",this.getNextHighestDepth, {_x:95.1,_y:485});

                        _root.giant_mc.duplicateMovieClip("giant_mc_"+"x", x);

                        setProperty("giant_mc_"+"x", _x, 95.1);

                        setProperty("giant_mc_"+"x", _y, 485);

                         x++;

                       cash-=140;

                    } else {

                      cash-=0;

                     }

                }

                 

                but you told me to take the duplicateMovie line out, but when that's out it only spawns one giant and when you click the button to spawn another one it resets the position of the giant instead of spawning another one.
                so I decided to leave the duplicate line in just for now until I can wright a better, script because it works ok
                the giant is deleted off the scene and is waiting in the library and it is ticked export for action script
                now for the hit test the giants are made up from giant_mc inside that is giant_walking and inside that is the animation for walking and for fighting so what I want is when a giant toches the enemy house(its a cave) I want it to stop walking and gotoAndPlay on frame 9 of giant_mc.giant_walking that's where the fighting animation starts, im not worrying about damage receiving yet
                but as soon is I start it up and click the button to create a giant it just comes up that error what i put at the top of the post
                and one more thing ive changed the script to this because I needed to but it in giant_walking because when i put it in giant_mc when a giant got created, when I played the game giant_mc acted like it had no script attached to it.

                onClipEvent (enterFrame) {

                     trace(this);

                     trace(enemyHouse);

                    if (this.hitTest(enemyHouse)) {

                         _x += 0.0;

                         gotoAndPlay(2);

                    } else {

                        _x += 0.6;

                     }

                }

                 

                and here is a screen shot to give you an idea of my scene

                game img.gif

                • 5. Re: Hit Test Dosent Work
                  Ned Murphy Adobe Community Professional & MVP

                  Your design is failing because of the way you are coding it.  Settling for doing things because they work isn't helping you solve the problems of things that don't work.

                   

                  I have to recommend that you stop placing code "on" objects and start using the timeline for all of your code.  Placing code on objects is bad practice.

                   

                  Here is code  you can use that replaces all you've shown so far and it gets placed on the timeline.  I have removed unecessary do-nothing code (like cash -= 0;).  For this code, you need to assign an instance name to your button (I use "btn" in what I show)...

                   

                  var giants = 0;

                   

                  function addGiant() {
                     if (cash>=140) {

                         var giant = attachMovie("giant_mc", "giant_mc"+giants, _level0.getNextHighestDepth(), {_x:95.1,_y:485});
                         giant.onEnterFrame = moveGiant;
                         giants += 1;

                         cash-=140;

                     }

                  }

                   

                  btn.onRelease = addGiant;

                   

                  function moveGiant(){
                      if (this.hitTest(enemyHouse)) {
                          delete this.onEnterFrame;
                          this.giant_walking.gotoAndPlay(9);
                      } else {
                          this._x+=0.6;
                      }
                  }

                  • 6. Re: Hit Test Dosent Work
                    LukeElChul Level 1

                    thank you for your time that worked perfectly i am going to take your advice and make all my scripts time line scripts

                    i will make a new post soon with all them changed scripts on so people can get them.

                    • 7. Re: Hit Test Dosent Work
                      Ned Murphy Adobe Community Professional & MVP

                      You're welcome

                      • 8. Re: Hit Test Dosent Work
                        LukeElChul Level 1

                        var giant = 0;

                        function addgiant() {

                             if (cash>=140) {

                                var giant = attachMovie("giant_mc", "giant_mc"+giants, _level0.getNextHighestDepth(), {_x:95.1, _y:485});

                                 giant.onEnterFrame = movegiant;

                                 giants += 1;

                                cash -= 140;

                             }

                        }

                        btngiant.onRelease = addgiant; function movegiant() {

                             if (this.hitTest(enemyHouse)) {

                                delete this.onEnterFrame;

                                this.gotoAndPlay(3);

                            } else { 

                               this._x += 0.8;

                            }

                        }

                        var spearman = 0;

                        function addspearman() {

                             if (cash>=35) { 

                               var spearman= attachMovie("spearman_mc", "spearman_mc"+spearmans, _level0.getNextHighestDepth(), {_x:95.1, _y:485});     

                          spearman.onEnterFrame = movespearman; 

                               spearmans += 1;

                                cash -= 35;

                             }

                        }

                        btnSpearman.onRelease = addspearman; function movespearman() {

                             if (this.hitTest(enemyHouse)) {

                                delete this.onEnterFrame;

                                this.gotoAndPlay(3);

                             } else {

                                this._x += 0.5;

                             }

                        }

                         

                        it works but when i added another 1 called spearman_mc i can create a spearman but when i press the button to create the giant the spearman diserpers and the giant gets created and viceversa how could i stop this

                        • 9. Re: Hit Test Dosent Work
                          Ned Murphy Adobe Community Professional & MVP

                          Except for using:

                           

                          var spearman = 0;  // wrong

                           

                          instead of:

                           

                          var spearmans = 0; // correct

                           

                          The rest of the code should be fine.  I don't know what else you might have done that causes objects to replace each other.

                          • 10. Re: Hit Test Dosent Work
                            LukeElChul Level 1

                            that works i can now create 1 spearman and 1 giant but if i wanted to create more then one giant would i have to put a duplicate line in that code

                            • 11. Re: Hit Test Dosent Work
                              Ned Murphy Adobe Community Professional & MVP

                              No.  The attachMovie line (and enough cash) is all you need to create new instances.... that is its purpose in life.

                              • 12. Re: Hit Test Dosent Work
                                LukeElChul Level 1

                                it only creates one giant ive mest around with it by creating dynamic texts so i can see if its working but when i click the button to create the giant it creates one and then the text goes up by 1 but when i click it again the giant goes back to the position were it gets attached and every time i press it it goes to the original position again and again until the cash runs out and it doesnt duplicate but the dynamic text goes up by +1 every time i press it.

                                 

                                and just to tell u i only have your script in my project so i can test more accurately and theres no other scripts to mess it up

                                • 13. Re: Hit Test Dosent Work
                                  Ned Murphy Adobe Community Professional & MVP

                                  I cannot tell what you might have done differently.  Below is a link to a file that uses the code you provided with a slight difference for the gotoAndPlay line since you removed the giant_walking element and this was originally created before that.  You can keep creating objects until you run out of cash.

                                   

                                  http://www.nedwebs.com/Flash/AS2_Giants.fla

                                   

                                  I won't be doing anything else to try to help you on this, so study it to see where your design differs and then adjust your design or the code accordingly. But using the duplicateMovie line is not the correct solution.  attachMovie is the proper way to add new instances.

                                  • 14. Re: Hit Test Dosent Work
                                    LukeElChul Level 1

                                    thank u for your help