Reputation: 101
I have the minion running after the bananas and it's being controlled by the arrow keys. I also have a life bar and I keep track of it based on if the minion drops the bananas. The problem that I have right now is that when a banana (from the array) hits the ground, my life bar drops entirely. I would like it to have it decreased gradually with each banana that hits the ground. I tried using a dispatch event but it doesn't work.
This is what I have in the 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;
var lives:Event= new Event("lifeDrops")
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);
}
if (info.hitTestObject(theBananas[i])){
dispatchEvent(lives);
health=health-10;
healthPercent=health/100;
trace(healthPercent);
life.scaleX= healthPercent;
text.text=String(health);
}
if (health<0){
gotoAndStop(5);
}
}
}
}
stop();
And in the 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;
}
}
}
Upvotes: 0
Views: 39
Reputation: 2008
You've simply failed to remove the banana from the array using. It needs to be removed from the array or it will keep checking if that banana is hitting info
, which it will be.
theBananas.removeAt(i); //works with most recent flash players and Adobe air
or
theBananas.splice(1,i); // works with all players and Adobe air versions
Both methods do the same thing but removeAt
is a bit faster and easier to type.
Of course you'll put this code right in with the rest of the block inside your info.hitTest, for example, it could go directly below text.text=String(health);
I would also go ahead and remove that banana from the stage with theBananas[i].parent.removeChild(theBananas[i];
. This will need to be done before you remove it from the array, of course.
Hope that helps.
Edit: @Cadin's answer is exactly right. How you get the banana to stop triggering the hitTest depends on how you want it to look and work. My answer is just one way to do that.
Upvotes: 1
Reputation: 4649
Your pickUpBananas
function runs every frame and checks every banana to see if it's touching the ground. So if a banana hits, it's going to decrement the health on that frame and every subsequent frame until it is no longer touching the ground.
You need to do something to prevent that same banana from repeatedly draining the health on subsequent frames.
The specific solution will depend on exactly how you want your game to work. You could immediately reset it's position so it's no longer touching the ground, but you might want it to stay sitting on the ground (or at least fade out before disappearing). In that case you might set a flag on the banana that indicates it's already been counted so it doesn't get counted again. Remember to clear that flag if you re-drop that banana.
Upvotes: 1