我的Flash游戏包含以下代码(http://pastie.org/9248528)
当我跑步时,玩家只是摔倒并且在他击中平台时不会停止。 我试过调试它并且我在使用moveCharacter的计时器时出错,但我不知道这是否是主要问题。
我将播放器放入墙内并使用断点对其进行调试,并且没有检测到播放器在墙内,跳过将其移到墙外。
任何人对我的代码有什么问题都有任何想法吗?
答案 0 :(得分:4)
问题在于此代码:
// Check if character falls off any platform
for (var i:int = 0; i < platform.length; i++) {
if (player.x < platform[i].x || player.x > platform[i].x + platform[i].width) {
onPlatform = false;
}
}
由于玩家不能同时在每个平台上,他的x位置几乎可以保证超出至少1个平台的范围,这将使平台变为假。相反,你需要保持对玩家所在平台的引用,如下所示:
var lastPlatform:Sprite; //holds reference to last platform player was on
// Function to move character
function moveCharacter(evt:TimerEvent):void {
....
// Check if character falls off the platform he was last on
if (lastPlatform != null && (player.x < lastPlatform.x || player.x > lastPlatform.x + lastPlatform.width)) {
onPlatform = false;
}
}
function detectCollisions():void {
// Check for collisions with platforms
for (var p:int = 0; p < platform.length; p++) {
// Adjust character to platform level if with landing depth of the platform
if (!onPlatform && player.hitTestObject(platform[p]) && lastPosY < platform[p].y) {
lastPlatform = platform[p]; //save reference
player.y = platform[p].y;
jumping = false;
onPlatform = true;
dy = 0;
// Prevent character from dropping sideways into platforms
} else if (!onPlatform && player.hitTestObject(platform[p])) {
player.x = lastPosX;
}
}
.....
}
这应该更好,但它仍然不是最面向对象的方式。希望这有帮助!