0

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

}
4

2 回答 2

0

我已经用一些痕迹运行了你的代码。当你跳跃和着陆时,onPlatform继续解析为假。这是因为,当您循环 10 次迭代时i,您根本不会i在循环中使用。通过将玩家定位在平台上方 1 个像素处,并且因为您只检查玩家的确切坐标是否有碰撞,该循环将永远不会检测到命中。改变...

if (platform.hitTestPoint(player.x, player.y, true)){

至...

if (platform.hitTestPoint(player.x, player.y + i, true)){
于 2013-11-11T10:47:02.480 回答
-1

Not sure what the etiquette is - I'm basically just trying to let the OP know I figured out his question. I figure another answer might register on his activity 'feed'. So...

When you jump and land, onPlatform continues to resolve to false. This is because, when you loop through 10 iterations of 'i', you don't use 'i' in the loop at all. By positioning the player 1 pixel above the platform and because you only ever check the player's exact coordinates for collision, that loop will never detect a hit. Change...

if (platform.hitTestPoint(player.x, player.y, true)){

to...

if (platform.hitTestPoint(player.x, player.y + i, true)){

...and it works.

于 2013-11-20T09:06:59.263 回答