PDA

View Full Version : [AS2] why does my setInterval only work one time?


bigmartizzle
01-03-2009, 08:47 PM
I have a setInterval function that starts a game. It works perfectly the first time but that's it. You have to reopen the game to play again. This is really frustrating! Here's what I'm looking relative to the interval issue.

This is what I want to happen with the interval:

function three21() {
if (startCountdown._currentframe == 36) {
player1press();
player2press();
clearInterval(three21Interval);
}
}

This is what sets the whole thing in motion:


titleScreen.onRelease = function() {
startCountdown.gotoAndPlay(2);
var three21Interval:Number = setInterval (three21, 1000);
}

Thanks for your help.

atomic
01-03-2009, 09:10 PM
Are you sure the interval is cleared? Add a trace action, to see if it stops or not when the interval is supposedly cleared...

And don't define the interval within a function, but outside of it, so that it can be cleared...

var three21Interval:Number;

titleScreen.onRelease = function() {
startCountdown.gotoAndPlay(2);
three21Interval = setInterval (three21, 1000);
}

bigmartizzle
01-03-2009, 09:24 PM
Thanks for the insight on best practice. Being self taught and new to flash, I am definitely prone to those mistakes. I made the switch and defined the interval outside of the function. It appears that it is not clearing. However, it still does not work when you start the game a second time.

Any ideas on how to clear it?

atomic
01-03-2009, 09:40 PM
Can you attach your .fla?

bigmartizzle
01-04-2009, 03:54 PM
It doesn't seem to be letting me attach the fla. (It is a rather large 15Mb.) Here is the code in it's entirety. I will understand if it's too much of a disaster to figure out. Thanks for your effort.

match._visible = true;
match.gotoAndStop(5);
winGauge._visible = true;
winGauge.gotoAndStop(5);
playAgain._visible = false;
play1wins._visible = false;
play2wins._visible = false;


var three21Interval:Number;

function three21() {
if (startCountdown._currentframe == 36) {
playerOne = (Math.floor(Math.random() * 3)) + 1;
playerTwo = (Math.floor(Math.random() * 3)) + 7;
player1press();
player2press();
clearInterval(three21Interval);
}
}

titleScreen.onRelease = function() {
titleScreen._visible = false;
play1wins._visible = false;
play2wins._visible = false;
wrestlin = 0;
numFrames._visible = true;
startCountdown.gotoAndPlay(2);
match._visible = true;
match.gotoAndStop(5);
winGauge._visible = true;
winGauge.gotoAndStop(5);
playerOne = 0;
playerTwo = 0;
arniesName._visible = true;
maloneysName._visible = true;
player1press();
player2press();
var three21Interval = setInterval (three21, 1000);

}


function player1press() {
if (playerOne == 1) {
play1num.gotoAndStop(1);
} else if (playerOne == 2) {
play1num.gotoAndStop(2);
} else if (playerOne == 3) {
play1num.gotoAndStop(3);
} else if (playerOne == 0) {
play1num.gotoAndStop(4);
}
}

function player2press() {
if (playerTwo == 7) {
play2num.gotoAndStop(1);
} else if (playerTwo == 8) {
play2num.gotoAndStop(2);
} else if (playerTwo == 9) {
play2num.gotoAndStop(3);
} else if (playerTwo == 0) {
play2num.gotoAndStop(4);
}
}

p1Fs = 0;
p2Fs = 0;

keyListener = new Object ();
keyListener.onKeyDown = function() {
if ((Key.getAscii() == 49) && (playerOne == 1)) {
wrestlin ++;
matchFrame();
playerOne = (Math.floor(Math.random() * 3)) + 1;
Winning();
ending();
player1press();
}else if ((Key.getAscii() == 50) && (playerOne == 2)) {
wrestlin ++;
matchFrame();
playerOne = (Math.floor(Math.random() * 3)) + 1;
Winning();
ending();
player1press();
} else if ((Key.getAscii() == 51) && (playerOne ==3)) {
wrestlin ++;
matchFrame();
playerOne = (Math.floor(Math.random() * 3)) + 1;
Winning();
ending();
player1press();
} else if ((Key.getAscii() == 50) || (Key.getAscii() == 51) && (playerOne == 1)) {
wrestlin --;
matchFrame();
Winning();
ending();
} else if ((Key.getAscii() == 49) || (Key.getAscii() == 51) && (playerOne == 2)) {
wrestlin --;
matchFrame();
Winning();
ending();
} else if ((Key.getAscii() == 49) || (Key.getAscii() == 50) && (playerOne == 3 )) {
wrestlin --;
matchFrame();
Winning();
ending();
} else if ((Key.getAscii() == 55) && (playerTwo == 7)) {
wrestlin --;
matchFrame();
playerTwo = (Math.floor(Math.random() * 3)) + 7;
Winning();
ending();
player2press();
} else if ((Key.getAscii() == 56) && ( playerTwo == 8)) {
wrestlin --;
matchFrame();
playerTwo = (Math.floor(Math.random() * 3)) + 7;
Winning();
ending();
player2press();
} else if ((Key.getAscii() == 57) && (playerTwo == 9)) {
wrestlin --;
matchFrame();
playerTwo = (Math.floor(Math.random() * 3)) + 7;
Winning();
ending();
player2press();
} else if ((Key.getAscii() == 57) || (Key.getAscii() == 56) && (playerTwo == 7)) {
wrestlin ++;
matchFrame();
Winning();
ending();
} else if ((Key.getAscii() == 57) || (Key.getAscii() == 55) && (playerTwo == 8)) {
wrestlin ++;
matchFrame();
Winning();
ending();
} else if ((Key.getAscii() == 55) || (Key.getAscii() == 56) && (playerTwo == 9 )) {
wrestlin ++;
matchFrame();
Winning();
ending();
}
}

Key.addListener(keyListener);


function matchFrame() {
if (wrestlin == -4) {
match.gotoAndStop(1);
} else if (wrestlin == -3) {
match.gotoAndStop(2);
} else if (wrestlin == -2) {
match.gotoAndStop(3);
} else if (wrestlin == -1) {
match.gotoAndStop(4);
} else if (wrestlin == 0) {
match.gotoAndStop(5);
} else if (wrestlin == 1) {
match.gotoAndStop(6);
} else if (wrestlin == 2) {
match.gotoAndStop(7);
} else if (wrestlin == 3) {
match.gotoAndStop(8);
} else if (wrestlin == 4) {
match.gotoAndStop(9);
}
}

function Winning() {
if (wrestlin == -4) {
winGauge.gotoAndStop(9);
} else if (wrestlin == -4) {
winGauge.gotoAndStop(9);
} else if (wrestlin == -3) {
winGauge.gotoAndStop(8);
} else if (wrestlin == -2) {
winGauge.gotoAndStop(7);
} else if (wrestlin == -1) {
winGauge.gotoAndStop(6);
} else if (wrestlin == 0) {
winGauge.gotoAndStop(5);
} else if (wrestlin == 1) {
winGauge.gotoAndStop(4);
} else if (wrestlin == 2) {
winGauge.gotoAndStop(3);
} else if (wrestlin == 3) {
winGauge.gotoAndStop(2);
} else if (wrestlin == 4) {
winGauge.gotoAndStop(1);
}
}

function ending() {
if (wrestlin == -5) {
winGauge._visible = false;
numFrames._visible = false;
match._visible = false;
playAgain._visible = true;
play2wins._visible =true;
player2wins.play();
arniesName._visible = false;
maloneysName._visible = false;
play1num._visible = false;
play2num._visible = false;
} else if (wrestlin == 5) {
winGauge._visible = false;
numFrames._visible = false;
match._visible = false;
playAgain._visible = true;
play1wins._visible = true;
play1wins.play();
arniesName._visible = false;
maloneysName._visible = false;
play1num._visible = false;
play2num._visible = false;
}
}

playAgain.onRelease = function() {
playAgain._visible = false;
titleScreen._visible = true;
startCountdown.gotoAndStop(1);
}

atomic
01-04-2009, 04:24 PM
Use a file sharing site as www.rapidshare.com to upload your .fla, and then provide here or in a PM to me, if you don't want to make it public, the link to download it from there...

bigmartizzle
01-04-2009, 05:15 PM
Thanks for the info on uploading fla.'s. I happened to notice that I used the var label in two spots with to define the interval.
var three21Interval:Number;
var three21Interval = setInterval (three21, 1000);
When I erased the var label in the setInterval line of code it worked fine multiple times. Again thanks for all your help.

atomic
01-04-2009, 05:30 PM
Grrrrrrrrreat! ;)