Hi, I’m currently working on this game as part of a project. It basically comprises of navigating a character(box) to avoid falling blocks(targets) until the character is hit three times and then the game should end. The current code looks like this:
var steps:Number = 5; //distance of one horizontal movement increment
var spriteX:Number = 265; // box x coordinate
var spriteY:Number = 265; // box y coordinate
//--target variables--
var targetNum:Number = 4; //total amount of targets at one time
var i:Number = 0; //inital number of target instances: the increment starts at 0
var score:Number = 3; //inital score amount
var gameActive:Boolean = true; //game is initially on
//---- functions ----
function checkKeys() {
if (Key.isDown(Key.RIGHT) && spriteX<510) {
spriteX += steps;
} else if (Key.isDown(Key.LEFT) && spriteX>40) {
spriteX -= steps;
}
}
function updateBox() {
box._x = spriteX;
box._y = spriteY;
}
//--Manage target--
function initTargets() {
if(gameActive == true) {
for (i; i<targetNum; i++) {
attachMovie("target_mc", "target"+i, i);
targets = _root["target"+i];
updateTargets(targets);
targets.onEnterFrame = function() {
if (this.hitTest(box)) {
this._y += 250;
score -= 1;
if (score <= 0) {
gameActive = false;
}
}
else if(this._y > 0 && this._y < 300) {
this._y += this.velo;
}
else {
updateTargets(this);
}
}
if (this._y > 300) {
removeMovieClip(this);
}
}
}
}
function updateTargets(which) {
which.gotoAndStop(random(530));
which._x = random(530); //distance value
which._y = 5; //height
which.velo = random(10)+ 2; //speed
}
initTargets(); //start function
//--initiate functions--
this.onEnterFrame = function() {
checkKeys();
updateBox();
};
Currently the problem seems to be that even after the score is reduced to 0 and gameActive is set to false, the “for loop” continues to push out increments and the game does not stop.
I would guess I’ve made a logic error somewhere, but I’ve tried a few things and can’t figure it out.
Any help would be very much appreciated, thanks.