I’m creating a game for a rich media banner and it has a bunch of bubbles in a grid. The bubbles then randomly get activated so that the player can click on them. Once a bubble is activated there is a countdown before that bubble becomes no longer clickable. Everything in my code is working find the first time it runs but when I try and start a new level or restart the game it seems like my setIntervals(); are doubling up on each other. I’ve tried deleting the intervals and I’ve also tried clearInterval();.
Is there no way to just remove the intervals completely so that when it runs through the code again it re initiates them?
Here is my code for the game. Sorry it’s a little confusing but I’ve tried to comment it as much as possible.
Thanks in advanced. Any help would be greatly appreciated!
// create the array for the colour of the bubblesvar colourBubble:Array = new Array("red_bubble", "blue_bubble", "purple_bubble");
//create an array for the bubbles that are currently active in level
var liveBubbles:Array = new Array();
//create an array for the amount of lives that the player has
var livesLeft:Array = new Array();
//the number of colors to choose from the array it is set to 2 because 0 counts as 1 - so its 0, 1, 2
var mySetting = 2;
var clickTime = 0;
var bubbleEnabled = 0;
// initial starting positions for the first bubble
var initX = 0;
var initY = 0;
var lives = 5;
var youMissed = 0;
tryAgain._visible = false;
blackOverlay._visible = false;
//find the specific bubble you clicked on
Array.prototype.getPos = function (item){
for (i = 0; i < this.length; ++i){
if (this* == item){
return i;
}
}
return null;
};
//records a missed bubble
missedBubble = function(bubbleRemoved) {
//checks to see if you have any lives left
if (youMissed == lives) {
clearInterval(myInterval2);
clearInterval(myInterval);
for(s in levels){
if (typeof (levels[s]) == "movieclip"){
levels[s].swapDepths(100);
levels[s].removeMovieClip();
//
}
}
gameOver();
}
//remove a life and disable the missed bubble/removes it from the array
else {
//remove a life
youMissed++;
bubbleRemoved.gotoAndPlay(6);
bubbleRemoved.enabled = false;
//find the bubble in the array
removeBubble = liveBubbles.getPos(bubbleRemoved);
//remove the element from the array
liveBubbles.splice(removeBubble, 1);
}
}
//updates the countdown for each bubble before they become unpoppable
updateTimer = function(bubble) {
if(int(bubble.pop_timer.text) > 0) {
bubble.pop_timer.text = int(bubble.pop_timer.text) - 1;
}
else if (bubble.pop_timer.text == 0) {
trace(bubble.pop_timer.text);
missedBubble(bubble);
}
}
function startNewLevel(){
// counter to keep track of bubbles and assign them individual names
var counter = 0;
for (var i = 1; i<=4; i++) {
for (var j = 1; j<=5; j++) {
//get random number to choose a bubble color
myNumber = random(mySetting);
while (myNumber == myTemp) {
myNumber = random(mySetting)+1;
}
myTemp = myNumber;
//myDisplay will be the random bubble colour
myDisplay = colourBubble[myNumber];
counter++;
// attach the movie clip on the stage
levels.attachMovie(myDisplay, "bubble"+counter, counter);
//push new bubble to the liveBubbles Array to record how many bubbles are active
liveBubbles.push(levels["bubble"+counter]);
// assign a _x position to the bubble
levels["bubble"+counter]._x = initX;
// assign a _y position to the bubble
levels["bubble"+counter]._y = initY;
// disable all the bubbles to start
levels["bubble"+counter].enabled = false;
// assign mouse functions to the bubbles
myBubble = levels["bubble"+counter];
myInterval = setInterval(1000);
//set this to what ever you want to count down from
levels["bubble"+counter].pop_timer.text = random(5)+1;
levels["bubble"+counter].onRelease = function() {
//start the animation for the bubble pop
this.gotoAndPlay(11);
this.enabled = false;
//find the bubble in the array
removeBubble = liveBubbles.getPos(this);
//remove the movie clip of this bubble from the stage
this.removeMovieClip();
//remove the element from the array
liveBubbles.splice(removeBubble, 1);
//trace(liveBubbles);
bubblesLeft = liveBubbles.length;
};
//trace("bubble"+counter);
initX += 55;
}
initY += 50;
initX = 0;
}
myInterval2 = setInterval(updateTimer2, 500);
}
function updateTimer2() {
//set a random time for new bubbles to enable
clickTime2 = random(5);
//check to see if there are bubbles left before running the timer
if (int(liveBubbles.length) > 0) {
clickTime2 = int(clickTime2) - 1;
bubblesLeft = liveBubbles.length;
bubbleEnabled = random(bubblesLeft);
// check to see if the bubble has already been activated. If it has then it will find a new one to enable
if (liveBubbles[bubbleEnabled].enabled == true){
//do nothing
}
//enable the next button to pop
else {
liveBubbles[bubbleEnabled].enabled = true;
liveBubbles[bubbleEnabled].gotoAndPlay(2);
myInterval = setInterval(updateTimer, 1000, liveBubbles[bubbleEnabled]);
}
}
//the player has completed the level
else {
trace("Level Complete!");
//clearInterval(myInterval2);
myInterval2.reset();
}
}
function gameOver(){
tryAgain._visible = true;
blackOverlay._visible = true;
}
tryAgain.onRelease = function() {
tryAgain._visible = false;
blackOverlay._visible = false;
// create the array for the colour of the bubbles
//create an array for the bubbles that are currently active in level
liveBubbles.splice(0);
//create an array for the amount of lives that the player has
livesLeft.splice(0);
//the number of colors to choose from the array it is set to 2 because 0 counts as 1 - so its 0, 1, 2
mySetting = 2;
clickTime = 0;
bubbleEnabled = 0;
// initial starting positions for the first bubble
initX = 0;
initY = 0;
lives = 5;
youMissed = 0;
startNewLevel();
}
startNewLevel();
stop();