0
votes

I have a life bar in my game and whenever the minion doesn't catch the bananas, the life bar decreases. It works fine, but the problem is that the life bar decreases before it touches the ground. Do I need to add another hitTest in the ground and have it in an && statement? Main program:

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();

And banana class:

    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

1 Answers

1
votes

Your problem is basically in the else block. You say if the banana hits, do x, else do y. That's the problem. You can keep your first part if banana hits the minion, do x and then add if banana hits bottom do y.

Get rid of the else.

Hope that made sense.

Edit:

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;
}

The return statement is so that if you remove a banana, you exit that function instead of going on to the next if statement and checking for a banana that has already been removed.