ActionScript 3 - hitTestObject错误

时间:2014-06-04 20:41:38

标签: actionscript-3 flash game-physics

首先,当我运行电影时,我认为hitTestObject直接无法正常工作。但是,当我修改一下时,我看到代码只有在电影开始运行时播放器(mcMain)碰到墙壁时才有效。

我希望每次玩家触摸到墙壁时都会追踪“是”,但如果玩家没有从一开始就碰到墙壁,那么进入它就什么都不做。

有人请帮忙。

源代码(hitTestObject方法调用位于最底层。)

//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 15;
//the current speed of the jump;
var jumpSpeed:Number = jumpSpeedLimit;
//jumping function
function mainJump():void{
    //if main isn't already jumping
    if(!mainJumping){
        //then start jumping
        mainJumping = true;
        jumpSpeed = jumpSpeedLimit*-1;
        mcMain.y += jumpSpeed;
    } else {
        //then continue jumping if already in the air
        //crazy math that I won't explain
        if(jumpSpeed < 0){
            jumpSpeed *= 1 - jumpSpeedLimit/75;
            if(jumpSpeed > -jumpSpeedLimit/5){
                jumpSpeed *= -1;
            }
        }
        if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
            jumpSpeed *= 1 + jumpSpeedLimit/50;
        }
        mcMain.y += jumpSpeed;
        //if main hits the floor, then stop jumping
        //of course, we'll change this once we create the level
        if(mcMain.y >= stage.stageHeight - mcMain.height){
            mainJumping = false;
            mcMain.y = stage.stageHeight - mcMain.height;
        }
    }
}


//These variables will note which keys are down
//We don't need the up or down key just yet
//but we will later
var leftKeyDown:Boolean = false;
var upKeyDown:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;

//adding a listener to mcMain which will make it move
//based on the key strokes that are down
mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
    //if certain keys are down then move the character
    if(leftKeyDown){
        mcMain.x -= mainSpeed;
    }
    if(rightKeyDown){
        mcMain.x += mainSpeed;
    }
    if(upKeyDown || mainJumping){
        mainJump();
    }
}
//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
    //making the booleans true based on the keycode
    //WASD Keys or arrow keys
    if(event.keyCode == 65){
        leftKeyDown = true;
    }
    if(event.keyCode == 87){
        upKeyDown = true;
    }
    if(event.keyCode == 68){
        rightKeyDown = true;
    }
    if(event.keyCode == 83){
        downKeyDown = true;
    }
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
    //making the booleans false based on the keycode
    if(event.keyCode == 65){
        leftKeyDown = false;
    }
    if(event.keyCode == 87){
        upKeyDown = false;
    }
    if(event.keyCode == 68){
        rightKeyDown = false;
    }
    if(event.keyCode == 83){
        downKeyDown = false;
    }
}

if(mcMain.hitTestObject(wall1)){
    trace("yes");
}

1 个答案:

答案 0 :(得分:1)

在您发布的代码中,看起来您的hitTest代码不属于任何类型的功能。它可能应该在每次玩家移动时检查的功能中。像这样:

function checkHitWall():void{
    if(mcMain.hitTestObject(wall1)){
        trace("yes");
    }
}
.....
function moveChar(event:Event):void{
    //if certain keys are down then move the character
    if(leftKeyDown){
        mcMain.x -= mainSpeed;
    }
    if(rightKeyDown){
        mcMain.x += mainSpeed;
    }
    if(upKeyDown || mainJumping){
        mainJump();
    }
    checkHitWall();
}