Search code examples
actionscript-3flashcollision-detectiongame-physicshittest

hitTesting with BitmapData in AS3 - my player sinks into platforms


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

When you land from a jump, you sink a few pixels into the ground and get pushed up (I don't like this).

When you walk down a hill, you lose contact with the ground and it looks jerky (I don't like this).

When you walk up a hill, you get pushed to the top very slowly (I especially don't like this).

I basically want the player to be able to follow the contour of the hill while walking (except if the hill is way too steep, which I think I'll work out later). Any ideas on how I can do this?

Here is my code (It's just on the timeline for the moment, until I actually work out how to do this. There are two MovieClips on the stage - playerClip and groundClip):

import flash.geom.Rectangle;
import flash.display.BitmapData;
import flash.events.Event;
import flash.geom.Point;
import flash.events.KeyboardEvent;

// Bitmap data stuff
var groundRect:Rectangle = groundClip.getBounds(this);
var groundClipBmpData = new BitmapData(groundRect.width,groundRect.height,true,0);
groundClipBmpData.draw(groundClip);

var playerRect:Rectangle = playerClip.getBounds(this);
var playerClipBmpData = new BitmapData(playerRect.width,playerRect.height,true,0);
playerClipBmpData.draw(playerClip);


// Player movement variables
var jump:Boolean = false;
var grounded:Boolean = false;

var moveRight:Boolean = false;
var moveLeft:Boolean = false;

var xMove = 0;
var yMove = 0;

const jumpStrength = 10;
const xMax = 5;
const xAccel = 1;
const GRAVITY = 1;

addEventListener(Event.ENTER_FRAME, enterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease);

function enterFrame(e:Event):void {
    /*---- PLAYER MOVEMENT ----*/
    // Movement Acceleration
    if (moveLeft == true) {
        xMove -= xAccel;
    } else if (moveRight == true) {
        xMove += xAccel;
    } else if (xMove > 0) {
        xMove -= xAccel;
    } else if (xMove < 0) {
        xMove += xAccel;
    }

    // Maximum Walk Speed
    if (xMove > xMax) {
        xMove = xMax;
    } else if (xMove < -xMax) {
        xMove = -xMax;
    }

    // Jumping
    if (jump == true) {
        grounded = false;
        yMove = -jumpStrength;
        jump = false;
    }

    /*---- BITMAP DATA STUFF ----*/
    var playerRect:Rectangle = playerClip.getBounds(this);
    var playerOffset:Matrix = playerClip.transform.matrix;

    playerOffset.tx = playerClip.x - playerRect.x;
    playerOffset.ty = playerClip.y - playerRect.y;

    var playerClipBmpData = new BitmapData(playerRect.width,playerRect.height,true,0);
    playerClipBmpData.draw(playerClip, playerOffset);

    var groundRect:Rectangle = groundClip.getBounds(this);
    var groundClipBmpData = new BitmapData(groundRect.width,groundRect.height,true,0);
    var groundOffset:Matrix = groundClip.transform.matrix;

    groundOffset.tx = groundClip.x - groundRect.x;
    groundOffset.ty = groundClip.y - groundRect.y;
    groundClipBmpData.draw(groundClip, groundOffset);


    // COORDINATES
    var rLoc:Point = new Point(groundRect.x,groundRect.y);
    var bLoc:Point = new Point(playerRect.x, playerRect.y);

    // FUTURE COORDINATE OF THE PLAYER
    var bLocFuture:Point = new Point(playerRect.x + xMove,playerRect.y + yMove);

    // HIT TEST GROUND WITH FUTURE COORDINATE OF PLAYER
    if (groundClipBmpData.hitTest(rLoc,
    255,
    playerClipBmpData,
    bLocFuture,
    255
      )) {
        grounded = true;
        playerClip.y--;
        yMove = 0;
    } else {
        yMove += GRAVITY;
    }

    // MOVE THE PLAYER
    playerClip.y += yMove;
    playerClip.x += xMove;


    // delete useless bitmap data to save memory
    playerClipBmpData.dispose();
    groundClipBmpData.dispose();
}

function onKeyPress(e:KeyboardEvent):void {
    if (e.keyCode == 37) {
        moveLeft = true;
        moveRight = false;
    } else if (e.keyCode == 39) {
        moveRight = true;
        moveLeft = false;
    } else if (e.keyCode == 38) {
        if (grounded == true) {
            jump = true;
        }
    }

}

function onKeyRelease(e:KeyboardEvent):void {
    if (e.keyCode == 37) {
        moveLeft = false;
    } else if (e.keyCode == 39) {
        moveRight = false;
    }
}

Solution

  • I think what you wish to change to make the player walk on the surface instead of floating up is this code:

    if (groundClipBmpData.hitTest(rLoc,
        255,
        playerClipBmpData,
        bLocFuture,
        255
          )) {
            grounded = true;
            playerClip.y--;
            yMove = 0;
        } else {
            yMove += GRAVITY;
        }
    

    What you want to do is something like this:

    if(groundClipBmpData.hitTest(rLoc, 255, playerClipBmpData, bLocFuture, 255))
    {
        yMove += GRAVITY;
    } 
    else
    {
        grounded = true;
        yMove = 0;
    }
    
    while(groundClipBmpData.hitTest(rLoc, 255, playerClipBmpData, bLocFuture, 255))
    {
        playerClip.y--;
    }
    playerClip.y++;