5 Replies Latest reply: Apr 11, 2012 5:55 PM by Ned Murphy RSS

    hitTestObject HEADACHE!

    le_mac_man Community Member

      I have a vision of a game I would love to create but I'm new to Actionscript 3, and I have been working my way through an online tutorial, trying to use the code given and adapt it to a game I am working on, which kind of has similarities, but now I'm completely stuck. (I've probably bitten off more than I can chew, but I don't want to give up on my game idea)

       

      The online tutorial is for creating a vertically scrolling shooting game where you control a Space ship and you shoot at enemy ships which fall vertically downwards randomly.

      The hitTestObject on the online game is between bullets shot from your ship and the enemy ship itself.

       

      My game is a horizontal scrolling scene where the user controls a flying superhero, which starts at the left, and items move across the screen from right to left towards the superhero and you have to collect ("hit") certain items. My hit test is to be between the superhero himself and these moving items. He doesn't fire anything.

       

      Here's a screenshot of MY game (not the online tutorial game):

      game.jpg

       

       

       

       

      This is the section of the tutorial that I am stuck on, as my game does not involve anything "shooting" anything, rather just a character that hits objects, no weapons. And the online tutorial uses the weapon's bullets for doing the hitTestObject, so I don't know how to use the code on my superhero as opposed to "bullets".

       

      http://asgamer.com/2009/as3-flash-games-for-beginners-registering-hit-tests

       

       

      I think the tutorial is excellently explained, but as I'm trying to adapt it to a different game it's very complicated and confusing for me, and being a beginner at Actionscript3 doesn't help.

       

      If anyone can tell me what I can show here or explain better here to receive help then please let me know, because I'm determined to do it.

       

      ALL and ANY help and advice is extremely welcomed!!!

      Many many thanks in advance.

        • 1. Re: hitTestObject HEADACHE!
          Ned Murphy CommunityMVP

          The thing you could show would be the code that is giving you a headache.  In general, if there is only one hero and many objects to hit him, have each of the objects do its own hitTestObject against the hero as they move.

          • 2. Re: hitTestObject HEADACHE!
            le_mac_man Community Member

            Thanks Ned. I understand that I need to do a hitTestObject for each object against the superhero, but I just don't know what code I need to do a hitTestObject and where to put, that's the problem. Here are the 2 sets of code below. First one is for the Superhero Character, and the second piece of code is for the Carrot object that I want the Superhero to be able to hit in order to collect points.

             

            1. This is the code for the superhero character. File name is Flying.as

             

            package

            {

             

                import flash.display.MovieClip;

                import flash.display.Stage;

                import com.senocular.utils.KeyObject;

                import flash.ui.Keyboard;

                import flash.events.Event;

             

                public class Flying extends MovieClip

                {

                   

                    private var stageRef:Stage;

                    private var key:KeyObject;

                    private var speed:Number = 0.5;

                    private var vx:Number = 0;

                    private var vy:Number = 0;

                    private var friction:Number = 0.93;

                    private var maxspeed:Number = 10;

                   

                   

                    public function Flying(stageRef:Stage)

                    {

                        this.stageRef = stageRef;

                        key = new KeyObject(stageRef);

                       

                        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

                    }

                   

                    public function loop(e:Event) : void

                {

                   

                    //keypresses

                    if (key.isDown(Keyboard.LEFT))

                            vx -= speed;

                    else if (key.isDown(Keyboard.RIGHT))

                            vx += speed;

                    else     vx *= friction;

                           

                           

                    if (key.isDown(Keyboard.UP))

                            vy -= speed;

                    else if (key.isDown(Keyboard.DOWN))

                            vy += speed;

                    else    vy *=friction;

                           

                    //update position

                    x += vx;

                    y += vy;

                   

                   

                    //speed adjustment

                    if         (vx > maxspeed)

                             vx = maxspeed;

                    else if (vx < -maxspeed)

                             vx = -maxspeed;

                            

                    if        (vy > maxspeed)

                             vy = maxspeed;

                    else if    (vy < -maxspeed)

                             vy = -maxspeed;

                           

                    //speed appearance

                    rotation = vx;

                   

                    //stay inside screen

                    if        (x > stageRef.stageWidth)

                    {

                             x = stageRef.stageWidth;

                             vx = -vx;

                    }

                   

                    else if (x < 0)

                    {

                             x = 0;

                             vx = -vx;

                    }

                            

                    if        (y > stageRef.stageHeight)

                    {

                             y = stageRef.stageHeight;

                             vy = -vy;

                    }

                   

                    else if (y < 0)

                    {        

                             y = 0;

                             vy = -vy;

                    }

             

             

                    }

             

                }

             

            }

             

             

             

             

            2. And this is the code for the carrot object which I want to be able to be hit by the Superhero character. File name is Carrot.as

             

             

            package

            {

             

                import flash.display.MovieClip;

                import flash.display.Stage;

                import flash.events.Event;

             

                public class Carrot extends MovieClip

                {

             

                    private var stageRef:Stage;

                    private var vx:Number = 3; //y velocity

                    private var ax:Number = 0; //y acceleration

                    private var target:Flying;

             

                    public function Carrot(stageRef:Stage, target:Flying) : void

                    {

                        this.stageRef = stageRef;

                        this.target = target;

             

                        y = Math.random() * stageRef.stageHeight;

                        x = 1000;

             

                        addEventListener(Event.ENTER_FRAME, loop, false, 0, true);

                    }

             

                    private function loop(e:Event) : void

                    {

                        vx -= ax;

                        x -= vx;

             

                        if (y > stageRef.stageHeight)

                            removeSelf();

                    }

             

                    private function removeSelf() : void {

             

                        removeEventListener(Event.ENTER_FRAME, loop);

             

                        if (stageRef.contains(this))

                            stageRef.removeChild(this);

                    }

             

                }

             

            }

            • 3. Re: hitTestObject HEADACHE!
              Ned Murphy CommunityMVP

              As I said, you can do the hitTest in the Carrot objects as they move.  You move them in the loop function, so in that function is where you should do the hitTest.

              • 4. Re: hitTestObject HEADACHE!
                le_mac_man Community Member

                Thank you very much Ned. I don't really know or understand completely why it worked this time, but I have the carrots disappearing when I hit them with the superhero.

                 

                Thanks for your time!

                • 5. Re: hitTestObject HEADACHE!
                  Ned Murphy CommunityMVP

                  You're welcome