11 Replies Latest reply on Dec 15, 2011 4:33 AM by Ned Murphy

    hitTest question

    BillyShope

      I'm a newbie working on a simple Pong game in Flash5. I have only 2 layers. Ball and paddle are on the first layer. The second layer has a loop with the following action frame:

       

      ball._x += xSpeed;

      ball._y += ySpeed;

      if(ball._x>620 || ball._x<-90){

      xSpeed=-xSpeed;

      }

      if(ball._y,hitTest(_parent.paddle) || ball._y<0){

      ySpeed=-ySpeed;

      }

      startDrag("paddle",true);

      Mouse.hide();

      _root.paddle.startDrag(-50,390,650,390);

       

      Obviously (to you), the ball passes right through the paddle. What am I doing wrong?

       

      Also, how can I copy code into this forum without retyping? (I'm certain this is obvious, but I've missed it.)

        • 1. Re: hitTest question
          Ned Murphy Adobe Community Professional & MVP

          Spend some time thinking thru this line, particularly the first half of it before the || ...

           

          if(ball._y,hitTest(_parent.paddle) || ball._y<0){

          • 2. Re: hitTest question
            Ned Murphy Adobe Community Professional & MVP

            Another thing, you do not reverse the _x direction of travel, so the ball will not reverse direction when it hits the paddle.

             

            To paste code into your posting, if you are using a PC you can use Ctrl-v after copying (Ctrl-c).  Don't know what to tell you for a mac equivalent.

            • 3. Re: hitTest question
              BillyShope Level 1

              It's evidently wrong, but I don't know where. Believe me, I've considered the problem for some time. I don't find consistency among online usages of "hitTest." Some place a comma before "hitTest," others place a period.

               

              I've got my Pong lying on its side. The ball would fall out the bottom, so I don't want to reverse the _x direction of travel.

               

              Thanks for the posting tip. Why does this forum have to be different from all the others? Rhetorical question.

              • 4. Re: hitTest question
                Ned Murphy Adobe Community Professional & MVP

                Periods are the correct syntax, but hittesting a _y property is not going to get you anything, and the paddle probably doesn't have a _parent relative to the timeline your code is in.

                 

                Not sure what your comment meant regarding this and other forums.

                • 5. Re: hitTest question
                  BillyShope Level 1

                  Okay, changed the "," to a "." but that didn't help.

                   

                  If I don't do the hitTest on _y, on what do I do it? The ball bounces off the top boundary just fine. Again, the paddle is horizontal and keeps the ball from dropping out the bottom. I don't want to disturb the _x.

                   

                  It doesn't work at all without the _parent.

                   

                  Disregard my forum comments. It's just that...on all other forums I ever used...a simple copy/paste worked.

                  • 6. Re: hitTest question
                    Ned Murphy Adobe Community Professional & MVP

                    If ball, paddle, and the code are on the same timeline, try:

                     

                    if(ball.hitTest(paddle) || ball._y<0){

                    • 7. Re: hitTest question
                      BillyShope Level 1

                      if(ball.hitTest(_parent.paddle) || ball._y<0){

                       

                      The above didn't work. Also tried it without the "_parent."

                       

                      The ball and paddle are on layer 1 (3 frames). Layer 2 also has 3 frames, with the third frame directing back to frame 2, where the code exists.

                       

                      Sorry to keep bothering you with such a simple problem. I bought a "dummies" book, but it was evidently written for different Flash and ActionScript versions. It has been of very little help.

                      • 8. Re: hitTest question
                        Ned Murphy Adobe Community Professional & MVP

                        What I showed fior the conditions stated will be correct.  I cannot answer for your version that uses _parent.  It does not make sense to use it, but you are not explaining your entire design and are not showing all ofgf your code.

                         

                        What I suggest is you isolate getting a hitTest to work in a new file, forgetting all other functionality.

                        • 9. Re: hitTest question
                          BillyShope Level 1

                          Except for the "go to" statement in the 3rd frame, I've shown you all the code.

                           

                          I can't recall if I tried an even simpler hitTest. I'll go do it right now.

                           

                          Thanks.

                          • 10. Re: hitTest question
                            BillyShope Level 1

                            Once I realized the ball and paddle have to be on different layers, the following simple code works:

                             

                            if(ball.hitTest(paddle) || ball._y<0){

                             

                            Sorry I didn't realize this earlier.

                             

                            Thanks for your help.

                             

                            Consider this matter resolved.

                            • 11. Re: hitTest question
                              Ned Murphy Adobe Community Professional & MVP

                              They do not need to be on different layers if you are using code to animate them (which you are), but if you have it working, it's best left alone for now.