0

我正在制作一个平台游戏。这是我到目前为止所拥有的:http: //megaswf.com/s/2486396(四处走动并用箭头键跳跃)

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

当你下山时,你会失去与地面的接触,而且看起来很生涩(我不喜欢这样)。

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

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

这是我的代码(目前只是在时间线上,直到我真正弄清楚如何做到这一点。舞台上有两个 MovieClip - 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;
    }
}
4

1 回答 1

0

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

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++;
于 2012-10-06T07:05:52.157 回答