AS3 hitTestObject == false

时间:2017-04-23 16:51:03

标签: actionscript-3 flash

我的游戏中有一个生命栏,每当奴才不捕捉香蕉时,生命栏就会减少。它工作正常,但问题是生命条在接触地面之前会减少。我是否需要在地面添加另一个hitTest并将其放入&&声明? 主程序:

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;

var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
var downKey:Boolean;
var jump:Boolean = false;
var xvelocity:int = 9;
var yvelocity:int = 3;
var gravity:Number = 7;


stage.addEventListener(Event.ENTER_FRAME, changeVelocity);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);

  function changeVelocity(evt:Event){
        moveMinion();
        yvelocity += gravity;
    }

    function moveMinion(){

        if (leftKey == true){
           sideMinion.x -= xvelocity;
            sideMinion.left();

        }
        if (rightKey == true){
            sideMinion.x += xvelocity;
            sideMinion.right();
        }

    }

    function checkKeyDown(e:KeyboardEvent){
        if (e.keyCode == Keyboard.LEFT){
            leftKey = true;
        }
        else if (e.keyCode == Keyboard.RIGHT){
            rightKey = true;
        }

    }

    function checkKeyUp(e:KeyboardEvent){
        if (e.keyCode == Keyboard.LEFT){
            leftKey = false;
        }
        else if (e.keyCode == Keyboard.RIGHT){
            rightKey = false;
        }

    }

    btnStart.addEventListener(MouseEvent.CLICK, makeItFall);

    function makeItFall(e:MouseEvent){



    var numBananas = 6;
    var theBananas: Array = new Array();
    theBananas = [];
    for (var i = 0; i < numBananas; i++) {
    var aBanana: banana = new banana();
    theBananas.push(aBanana);
    btnStart.visible=false;
    aBanana.y=100;
    theBananas[i].setSpot(Math.random()*450,Math.random()*200);
    theBananas[i].setSpeed((Math.random()), 1);
    stage.addChild(aBanana);

}   

var health: Number= 100;
var healthPercent: Number= 1;

addEventListener(Event.ENTER_FRAME, pickUpBananas);

function pickUpBananas(event:Event){

    for (var i:int = theBananas.length-1; i>-1; i--){

            if (sideMinion.hitTestObject(theBananas[i])){
                stage.removeChild(theBananas[i]);
                theBananas.splice(i,1);

        }
        else if (sideMinion.hitTestObject(theBananas[i])==false){
            health=health-10;
            healthPercent=health/100;
            trace(healthPercent);
            life.scaleX= healthPercent;

        }
    }
}

    }


stop();

香蕉班:

    package {
    import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.events.Event;

    public class banana extends MovieClip {

        var velX: Number = 0;
        var velY: Number = 0;
        var falling: Boolean = false;
        var gravity: Number = 2;

        public function banana() {
            var timing: Timer = new Timer(25, 0);
            timing.addEventListener(TimerEvent.TIMER, moveMe);
            timing.start();

        }

         private function moveMe(event: TimerEvent)
    {
        x += velX;
        y += velY;

        if (falling) velY += gravity;

       /* trace("[BANANA] position:", x, y, "speed:", velX, velY);*/
    }


        public function setSpeed(dx,dy) {
            velX = dx;
            velY = dy;
        }

            public function setSpot(atX,atY){
            this.x=atX;
            this.y=atY;
        }


        public function makeItFall (){
            falling=true;
        }


    }

}

1 个答案:

答案 0 :(得分:1)

您的问题基本上在else区块中。你说如果香蕉击中,做x,else做y。那就是问题所在。您可以保留第一部分if banana hits the minion, do x,然后添加if banana hits bottom do y

摆脱else

希望有道理。

编辑:

if (the banana is hitting the minion){
    // add health or whatever
    // remove banana from array
    return;
}
if (the banana hits the floor){
    // lose health or whatever
    // remove banana from array
    return;
}

return语句是这样的,如果你删除了一个香蕉,你就退出该函数而不是继续下一个if语句并检查已经删除的香蕉。