Search code examples
actionscript-3flashcollision-detectiongame-physicsbitmapdata

AS3 - Finding the highest y-coordinate for a given x-coordinate in BitmapData?


I'm making a platformer. Here is what I have so far: http://megaswf.com/s/2486396 (move around and jump with arrowkeys).

I'm using the ground's bitmap data to test collisions with the player's coordinates, and the player is sinking into the ground a few pixels (especially when walking uphill). To prevent this, I want the player's y coordinate to instantly change so that the player is sitting on top of the ground. Here is what I'm working on:

    if (groundClipBmpData.hitTest(rLoc, 255, bLocFuture)) {
        playerClip.y = ???
    }

I want the player's y-coordinate to change to become the highest pixel of the groundClipBmpData at the player's x coordinate.

How can I do this?


Solution

  • Like I said in the other question:

    Use while instead of if

    Like this:

    while(groundClipBmpData.hitTest(rLoc, 255, playerClipBmpData, bLocFuture, 255))
    {
            playerClip.y--;
    }
    playerClip.y++;
    

    The fastest way would be to calculate and store the highest coordinate values in advance inside an array and then use the 'x' position of the player to determine the 'y' of the ground.