我正在製作一臺平臺遊戲機。以下是我迄今爲止: http://megaswf.com/s/2486396(四處走動,並與arrowkeys跳)hit3用AS3中的BitmapData進行測試 - 我的播放器沉入平臺
當你從一個跳躍土地,你匯幾像素到地面,並得到推高(我不喜歡這個)。
當你走下山坡時,你會失去與地面的接觸,它看起來很生澀(我不喜歡這樣)。
當你爬上一座小山時,你會很慢地被推到頂部(我特別不喜歡這樣)。
我基本上希望玩家能夠在行走時跟隨山的輪廓(除非山坡太陡峭,我認爲我會在稍後工作)。 關於如何做到這一點的任何想法?
這裏是我的代碼(這只是在時間軸上的那一刻,直到我真正工作,如何做到這一點上有第二階段的影片剪輯 - 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;
}
}
只需注意一下,如果(xMove> xMax)xMove = xMax有一種優雅的方式。 xMove = Math.max(xMove,xMax); //我現在會回答 - 您的代碼評論得很好 – zehelvion