当我的角色掉到平台上时,我可以移动一切,一切正常。唯一的问题是当我跳跃时,它只允许它跳一次,然后在那之后不响应任何upKey事件。
我想知道如何解决我的代码问题。我希望每次按下向上箭头时我的角色都能跳跃。
继承我的代码:
package {
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class GameCode extends MovieClip {
var upKey:Boolean;
var leftKey:Boolean;
var rightKey:Boolean;
var jump:Boolean = false;
var xvelocity:int = 10;
var yvelocity:int = 0;
var gravity:Number = 1;
var jumpspeed:int = -10;
var onPlatform:Boolean;
var startPosY:int;
var startPosX:int;
var lastPosY:int;
var lastPosX:int;
public function GameCode() {
// constructor code
}
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
}
function update(evt:Event){
moveCharacter();
yvelocity += gravity;
if (!platform.hitTestObject(player)){
player.y += yvelocity;
onPlatform = false;
}
for (var i:int = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
yvelocity = 0;
player.y = platform.y - 1;
onPlatform = true;
}
}
}
function moveCharacter(){
lastPosY = player.y;
lastPosX = player.x;
if (leftKey == true){
player.x -= xvelocity;
}
if (rightKey == true){
player.x += xvelocity;
}
if (upKey == true && onPlatform == true){
yvelocity = jumpspeed;
player.y += yvelocity;
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.UP){
upKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.UP){
upKey = false;
}
}
}
}
答案 0 :(得分:0)
我用一些痕迹运行你的代码。当你跳跃和降落时,onPlatform
继续解决为假。这是因为,当您循环遍历i
的10次迭代时,您根本不在循环中使用i
。通过将玩家定位在平台上方1个像素,并且因为您只检查玩家的碰撞精确坐标,该循环将永远不会检测到命中。改变......
if (platform.hitTestPoint(player.x, player.y, true)){
为...
if (platform.hitTestPoint(player.x, player.y + i, true)){
答案 1 :(得分:-1)
不确定礼仪是什么 - 我基本上只是想让OP知道我弄清楚了他的问题。我认为另一个答案可能会记录在他的活动'饲料'上。所以...
当你跳跃和降落时,onPlatform继续解决为假。这是因为,当你遍历10次'i'迭代时,你根本不在循环中使用'i'。通过将玩家定位在平台上方1个像素,并且因为您只检查玩家的碰撞精确坐标,该循环将永远不会检测到命中。改变......
if (platform.hitTestPoint(player.x, player.y, true)){
为...
if (platform.hitTestPoint(player.x, player.y + i, true)){
......它有效。