Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've been working on a 2D platformer and have gotten the collision working, except for one tiny problem. My games a tile based platformer and whenever the player is in between two tiles, he falls down. Here is my code, it's fire off using an ENTER_FRAME event. It's only for collision from the bottom for now.

        var i:int;
        var j:int;
        var platform:Platform;
        var playerX:int = player.x/20;
        var playerY:int = player.y/20;
        var xLoopStart:int = (player.x - player.width)/20;
        var yLoopStart:int = (player.y  - player.height)/20;
        var xLoopEnd:int = (player.x + player.width)/20;
        var yLoopEnd:int = (player.y + player.height)/20;
        var vy:Number = player.vy/20;
        var hitDirection:String;
        for(i = yLoopStart; i <= yLoopEnd; i++)
        {
            for(j = xLoopStart; j <= xLoopStart; j++)
            {
                if(platforms[i*36 + j] != null && platforms[i*36 + j] != 0)
                {
                    platform = platforms[i*36 + j];
                    if(player.hitTestObject(platform) && i >= playerY)
                    {
                        hitDirection = "bottom";
                    }
                }
            }
        }

This isn't the final version, going to replace hitTest with something more reliable , but this is an interesting problem and I'd like to know whats happening. Is my code just slow? Would firing off the code with a TIMER event fix it? Any information would be great.

share|improve this question
    
Is that the full code? Is hitDirection ever set to anything else? –  Byte56 Jun 29 '12 at 18:31
    
No, like i said this was just some test code. I was testing to see if lowering the number of tiles checked would affect performance and i ran into this bug –  inzombiak Jun 29 '12 at 18:42
    
Well, it's possible you're only testing the tile directly under the player. So when you're crossing the boundary between tiles, the new tile doesn't get checked until it's too late. Try using your bounds (the loopstart/loopend variables) to draw a rectangle around where you're testing, or highlight the platforms that are tested, or best of all, step through the code with a debugger and find out why some platforms are not being checked. –  Byte56 Jun 29 '12 at 18:54
    
if you mean the i >= thing, it doesn't seem to change much. –  inzombiak Jun 29 '12 at 19:10
    
I mean you should step through your code with the debugger. –  Byte56 Jun 29 '12 at 19:29
add comment

1 Answer

up vote 2 down vote accepted

The initialiser and loop condition are both using xLoopStart

for(j = xLoopStart; j <= xLoopStart; j++)

Should be xLoopEnd for second parameter

for(j = xLoopStart; j <= xLoopEnd; j++)
share|improve this answer
    
that did it, and i feel extra stupid. –  inzombiak Jun 29 '12 at 19:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.