在 AS3 中使用 BitmapData 进行测试 - 我的播放器沉入平台



我正在做一个平台游戏。这是我到目前为止所拥有的:http://megaswf.com/s/2486396(走来走去,用箭头跳跃)

当你从跳跃中着陆时,你会沉入地面几个像素并被推上去(我不喜欢这样)。

当你走下山坡时,你与地面失去联系,看起来很生涩(我不喜欢这样)。

当你走上山坡时,你会很慢地被推到山顶(我特别不喜欢这样)。

基本上希望玩家能够在行走时跟随山丘的轮廓(除非山坡太陡峭,我想我稍后会解决这个问题)。关于我该如何做到这一点的任何想法?

这是我的代码(它只是在时间线上,直到我真正弄清楚如何做到这一点。舞台上有两个影片剪辑 - playerClip 和 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;
    }
}

我认为您希望更改以使玩家在表面上行走而不是漂浮起来的代码是以下代码:

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

您要做的是这样的:

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++;

最新更新