0

プラットフォーマーを作っています。これが私がこれまでに持っているものです: http://megaswf.com/s/2486396(歩き回り、矢印キーでジャンプします)

ジャンプから着地すると、数ピクセル地面に沈み、押し上げられます (私はこれが好きではありません)。

丘を下ると、地面との接触が失われ、ぎくしゃくしたように見えます (私はこれが好きではありません)。

丘を登ると、非常にゆっくりと上に押し上げられます (私は特にこれが好きではありません)。

私は基本的に、プレーヤーが歩きながら丘の輪郭をたどることができるようにしたいと考えています (丘が急すぎる場合を除きます。後で解決すると思います)。どうすればこれを行うことができるかについてのアイデアはありますか?

これが私のコードです(実際にこれを行う方法を理解するまでは、タイムライン上にあるだけです。ステージには2つのムービークリップがあります-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 に答える