Skip to main content
Inspiring
April 23, 2013
Answered

Collision detection problem

  • April 23, 2013
  • 1 reply
  • 614 views

Hey guys,

So I'm practicing creating a platformer game. I've got a movement system working, but when trying to add collision with platforms I have a problem.

What I did was add each platform to an array, and then check for collision between the player and each platform, since I just have a bunch of nameless instances of the platform symbol on my stage. The issue with this is that for my jumping code I use a boolean "onGround", which determines whether the player is 'on the ground' or not. And since my 'for each' loop runs through EVERY platform, even if the player is on a certain platform, there are other ones he won't be on and so it returns the boolean onGround as false.

This is a problem with my jumping code in general, I'm just wondering where I should go from here. If I need to change my jumping code, or change the collision, or what. Any ideas would be great. Here is my code :

package 

{

    import flash.display.MovieClip;

    import flash.events.Event;

    import flash.events.KeyboardEvent;

    import flash.display.DisplayObject;

    public class Main extends MovieClip

    {

        var arrowUp:Boolean = false;

        var arrowDown:Boolean = false;

        var arrowLeft:Boolean = false;

        var arrowRight:Boolean = false;

        var arrowUpReleased:Boolean = true;

        var arrowDownReleased:Boolean = true;

        var arrowLeftReleased:Boolean = true;

        var arrowRightReleased:Boolean = true;

        var onGround:Boolean = undefined;

        var isJumping:Boolean = undefined;

        var vx:Number = 0;

        var vy:Number = 0;

        var acceleration:Number = 1;

        var gravity:Number = 0.3;

        var jumpForce:Number = 7;

        var speedLimit:uint = 6;

        var platformArray:Array = new Array();

        var platformNumber:uint = 0;

        var thePlatform:MovieClip;

        public function Main()

        {

            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

            stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);

            stage.addEventListener(Event.ENTER_FRAME, update);

            for (var i:uint = 0; i < numChildren; i++)

                 {

                     if(getChildAt(i) is Platform)

                     {

                         platformArray.push(getChildAt(i));

                     }

                 }

            for each (var platform:DisplayObject in platformArray)

            {

                if (platform.hitTestPoint(player.x, player.y + 20, true))

                    {

                        vy = 0;

                        onGround = true;

                    }

            }

        }

        function keyPressed(event:KeyboardEvent):void

        {

            if (event.keyCode == 37)

            {

                arrowLeft = true;

                arrowLeftReleased = false;

            }

            if (event.keyCode == 38)

            {

                arrowUp = true;

                arrowUpReleased = false;

                isJumping = true;

            }

            if (event.keyCode == 39)

            {

                arrowRight = true;

                arrowRightReleased = false;

            }

            if (event.keyCode == 40)

            {

                arrowDown = true;

                arrowDownReleased = false;

            }

        }

        function keyReleased(event:KeyboardEvent):void

        {

            if (event.keyCode == 37)

            {

                arrowLeft = false;

                arrowLeftReleased = true;

            }

            if (event.keyCode == 38)

            {

                arrowUp = false;

                arrowUpReleased = true;

                isJumping = false;

            }

            if (event.keyCode == 39)

            {

                arrowRight = false;

                arrowRightReleased = true;

            }

            if (event.keyCode == 40)

            {

                arrowDown = false;

                arrowDownReleased = true;

            }

        }

        function update (event:Event):void

        {

            //ACCELERATION

            if (arrowLeft)

            {

                if (vx > 0)

                {

                    vx = 0;

                }

                if ((Math.abs(vx)) < speedLimit)

                {

                    vx -= acceleration/2;

                    player.x += vx;

                }

                else

                {

                    player.x += vx;

                }

            }

            if (arrowRight)

            {

                if (vx < 0)

                {

                    vx = 0;

                }

                if (vx < speedLimit)

                {

                    vx += acceleration/2;

                    player.x += vx;

                }

                else

                {

                    player.x += vx;

                }

            }

            if (arrowUp && onGround)

            {

                vy = -jumpForce;

            }

            //DECELERATION

            if (arrowLeftReleased)

            {

                if (vx < 0)

                {

                    vx += acceleration;

                    player.x += vx;

                }

            }

            if (arrowRightReleased)

            {

                if (vx > 0)

                {

                    vx -= acceleration;

                    player.x += vx;

                }

            }

            //GRAVITY

            player.y += vy;

            if (!onGround)

            {

                vy += gravity;

            }

            if (onGround && !isJumping)

            {

                vy = 0;

            }

            if (vy > 0)

            {

                onGround = false;

            }

            else

            {

                onGround = true;

            }

            //COLLISION

            for each (var platform:MovieClip in platformArray)

            {

                if (platform.hitTestPoint(player.x, player.y + 20, true))

                {

                    vy = 0;

                    onGround = true;

                }

                else

                {

                    onGround = false;

                }

            }

            trace (onGround);

        }

    }

}

Thanks

P.S. what happens at the moment with that code is that the player hits the platform and stops, but onGround remains false

This topic has been closed for replies.
Correct answer

Likely there's too much code for anyone to really wade through. However, from what you said about your for loop:

for each (var platform:DisplayObject in platformArray){

if (platform.hitTestPoint(player.x, player.y + 20, true)){

   vy = 0;

   onGround = true;

}

}

I think what you might want is to put a break; in there right after onGround = true; - ie stop processing when you find a ground plane.

Also, there's a couple of good collision detection systems out there that might make this a bit easier and robust. Have a look at Collision Detection Kit (CDK):

https://code.google.com/p/collisiondetectionkit/

1 reply

Correct answer
April 23, 2013

Likely there's too much code for anyone to really wade through. However, from what you said about your for loop:

for each (var platform:DisplayObject in platformArray){

if (platform.hitTestPoint(player.x, player.y + 20, true)){

   vy = 0;

   onGround = true;

}

}

I think what you might want is to put a break; in there right after onGround = true; - ie stop processing when you find a ground plane.

Also, there's a couple of good collision detection systems out there that might make this a bit easier and robust. Have a look at Collision Detection Kit (CDK):

https://code.google.com/p/collisiondetectionkit/

BawlarAuthor
Inspiring
April 24, 2013

Awesome, that works perfectly thanks .

Yeah I know there's a ton of collision systems out there already, but I wanted to make my own for practice. best way to learn