9 Replies Latest reply on Mar 31, 2007 8:39 AM by kglad

    collision detection for attached movie clips

    psylova
      Hello,

      I have a difficulty that I need help overcoming. To give some background information, here are the movie clips used:

      1 movie clip called 'skater'
      1 movie clip called 'token'
      In the actionscript, 'token' is incremented to become 'token1', 'token2', etc.

      I am using attachMovie to place the new 'token(i)' at different points on the stage. I then want my 'skater' to be able to collect them using a hitTest. I know my code works for other things as I tried it by creating another movie clip called 'obstacle' and substituting it in place of 'token(i)' and it worked perfectly.

      I don't understand why my code isn't detecting the attached movie clips.

      If anyone can point me in the right direction, I would really appreciate it. I have attached my code for you to look at.

      Looking forward to an answer soon.

      Cheers
      Psylova
        • 1. Re: collision detection for attached movie clips
          kglad Adobe Community Professional & MVP
          you made the newname string local to tokens() when you prefixed it with "var". outside tokens() (and in particular, inside collectToken() ), newname is undefined.

          to remedy, remove the "var" prefix your newname assignment statement.
          • 2. Re: collision detection for attached movie clips
            psylova Level 1
            Thank you kglad for your reply.

            Unfortunately, when I alter the code according to the instructions above I get the following:

            The collectToken function seems to execute automatically. I think this because even though the tokens don't 'appear' on stage (I think they are being removed as soon as they are created) the score increments each time one is created even though my skater mc is nowhere near it.

            I thought it might be a problem with my 'if statement'

            if (_root.skater, hitTest(_root[newname]))

            but altering it doesn't produce the desired result.

            If you or anyone else could help me through again, I would greatly appreciate it.

            Cheers.

            Psylova
            • 3. Re: collision detection for attached movie clips
              kglad Adobe Community Professional & MVP
              there's nothing calling the collectToken() function in the code you posted. so, you must have code elsewhere causing that problem.

              however, your hitTest() syntax is incorrect. use

              • 4. Re: collision detection for attached movie clips
                psylova Level 1
                Thank you again for your response kglad

                I'd previously substituted the above code in the actionscript before I posted the reply so the problem may be the way I call the function. (Substituting the above code gives me the original problem - no hitTest detection.)

                I've posted the function calling the collectToken function code as it isn't that long for you to look at.

                _root.hp = 100;
                var numToken = 3;
                var timeSinceLastToken = 0;
                score = 0;

                function bail()
                {
                _root.skater.gotoAndStop(7);
                // _root.gotoAndPlay("gameOver")
                }

                _root.onEnterFrame = function()
                {
                moveHero(5);
                gutter._x -=5; // moving the gutter
                gutterHit();
                tokens();
                collectToken();
                }


                once again, I've probably just structured it wrong but I figured it's worth you looking at it.

                Please note that the 'bail' function is the game over side of things

                Thanks again - I truly appreciate the help

                Cheers
                Psylova
                • 5. Re: collision detection for attached movie clips
                  kglad Adobe Community Professional & MVP
                  that looks a little screwy. every 200 loops or so of your onEnterFrame loop you're creating a new token and then checking a hitTest on that one, just created, token. is that what you want?
                  • 6. Re: collision detection for attached movie clips
                    psylova Level 1
                    No!!!!

                    What I'm trying to do is build a simple platform game about a skater, who rolls over tokens to collect them to add points. I basically want the following:

                    1. The tokens randomly appear at the far right of the screen.
                    2. As they appear, they "move" towards the left of the screen (this is in line with the background, which also moves across the screen giving the skating movement illusion. (I haven't gotten to this bit yet!)
                    3. As soon as any token is touched by the skater, it is removed from the stage and the score is incremented.
                    4. if the skater misses a token and it goes off the other side of the screen it is 'lost' - you can't go back and get it.

                    Could you please if it's not too much trouble just explain to me where in the code I've told it to check just that one token?

                    Thanks so much for taking the time to look and answer.

                    Cheers

                    Psylova
                    • 7. Re: collision detection for attached movie clips
                      kglad Adobe Community Professional & MVP
                      collectToken() is only checking one hitTest(). it checks frequently but it's only checking the last created token. if that token is "lost", it's still checking. if there is more than one token on-screen, it only checks the most recently created one.
                      • 8. Re: collision detection for attached movie clips
                        psylova Level 1
                        Thanks so much kglad. I guess I have to go back and rethink my code. Dammit!!!!

                        I appreciate you taking the time to answer my questions. If you have a suggestion on code structure (some sort of pseudocode) that could point me in the right direction, I'd also appreciate it.

                        Thanks again for all your assistance.

                        cheers

                        Psylova
                        • 9. Re: collision detection for attached movie clips
                          kglad Adobe Community Professional & MVP
                          you're welcome.