当我的角色掉到平台上时,我可以四处走动,一切正常,一切正常。唯一的问题是当我跳跃时,它只允许它跳跃一次,然后不再响应任何 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;
}
}
}
}