0
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ Is that the full code? Is hitDirection ever set to anything else? \$\endgroup\$
    – House
    Commented Jun 29, 2012 at 18:31
  • \$\begingroup\$ 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 \$\endgroup\$
    – inzombiak
    Commented Jun 29, 2012 at 18:42
  • \$\begingroup\$ 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. \$\endgroup\$
    – House
    Commented Jun 29, 2012 at 18:54
  • \$\begingroup\$ if you mean the i >= thing, it doesn't seem to change much. \$\endgroup\$
    – inzombiak
    Commented Jun 29, 2012 at 19:10
  • \$\begingroup\$ I mean you should step through your code with the debugger. \$\endgroup\$
    – House
    Commented Jun 29, 2012 at 19:29

1 Answer 1

2
\$\begingroup\$

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++)
\$\endgroup\$
1
  • \$\begingroup\$ that did it, and i feel extra stupid. \$\endgroup\$
    – inzombiak
    Commented Jun 29, 2012 at 19:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .