Groups | Blog | Home
all groups > flash actionscript > january 2006 >

flash actionscript : setInterval being called more than once...


Stevie_R
1/12/2006 10:20:02 PM
I have created a lab where students need to manipulate beakers, wires,
electrodes, etc. They do this by pressing buttons which cause the various
components to appear or disappear (using setInterval) on the stage.

The problem is not the first time the students do it (for the first beaker),
nor is it the second. After two beaker's worth, students return to the
beginning of the lab and choose another sample. When they get to the step where
they need to manipulate the beakers, etc., again, the intervals all of a sudden
go INSANE--none of them do any clearing at all, and the alpha value of the
beaker and its label fluctuate up to 12,000+. It makes absolutely no sense to
me at all. When I step through the code using the debugger, it clearly hits the
"clearInterval" call, but yet continues to run. The other interesting thing
that happens is that when the beaker is chosen, it seems to run the
intervalID=setInterval lines twice before it jumps to the function. Code is
attached and any help understanding how setInterval actually works would be
appreciated.

File to play with is at
http://www.personal.psu.edu/sxr133/flash/turf435_lab4.html
FLA file (if you want to look at HORRIBLE code) is at
http://www.personal.psu.edu/sxr133/flash/turf435_lab4_01-12-06sr.fla

Stevie

CODE BELOW:

//set up the listener object to "watch" the radio buttons showing which beaker
is being chosen
var oListener2:Object = new Object();

//when either radio button is clicked, the highlights appear (or disappear)
//and the word "Active" appears in the correct spot.
oListener2.click = function(oEvent:Object):Void {
if (oEvent.target.selectedData == "Beaker 1") {

//set the button variable to pressed
beakerButton1Pressed = true;
beakerButton2Pressed = false;
//put the blue highlight behind the button
beakerHighlight._visible = true;
beaker2Highlight._visible = false;
//make the word "active" visible where it belongs
active1._visible = true;
active2._visible = false;
//disable the other choice so that it's the only one that can be chosen
until all steps are complete
beakerChoice2.enabled = false;
//make the active beaker appear with the correct label on it
activeLabel.beakerLabel_txt.text = "Beaker 1: .1N solution";
} else {
//set the correct beaker button to be pressed
beakerButton2Pressed = true;
beakerButton1Pressed = false;
//put the blue highlight behind the button
beakerHighlight._visible = true;
beaker2Highlight._visible = true;
//make the word "active" visible where it belongs
active1._visible = false;
active2._visible = true;
//disable the other choice so that it's the only one that can be chosen
until all steps are complete
beakerChoice1.enabled = false;
//make the active beaker appear with the correct label on it
activeLabel.beakerLabel_txt.text = "Beaker 2: 1.0N solution";

}
//make the beaker and its label appear--they are separate so that the
electrode can
//look like it's going "in" to the beaker

//THE TWO LINES THAT FOLLOW APPEAR TO RUN TWICE WHEN THE RADIO BUTTON
IS CLICKED
nIntervalAppear = setInterval(makeItAppear, 50, activeLabel,
"nIntervalAppear");
nIntervalAppear2 = setInterval(makeItAppear,50,
activeBeaker,"nIntervalAppear2");

};

//add the listener to the beaker group
beakerGroup2.addEventListener("click", oListener2);

//FUNCTIONS USED IN THIS FRAME
function makeItAppear(mcItem:MovieClip, intName:String):Void {
mcItem._alpha += 10;
if (mcItem._alpha>=100) {

//This is, I think, where the issue lies, but unfortunately, it doesn't
//have a problem the first two times you use it on the beaker and label
// i.e., when you do the two beakers for the first soil. I have also traced
the
// intName string for clearing the interval and it also seems to be okay.
//this next line is a trace message
_root.message_txt.text = "the item "+mcItem+" has an alpha of
"+mcItem._alpha;

//THE FOLLOWING SECTION RUNS WHEN ALPHA > 100, BUT THEN THE
FUNCTION CONTINUES TO BE CALLED THE SECOND TIME THROUGH THE LAB
if (intName == "nIntervalAppear") {
clearInterval(nIntervalAppear);
nIntervalAppear = 0;
} else if (intName == "nIntervalAppear2") {
clearInterval(nIntervalAppear2);
nIntervalAppear2 = 0;
}
}
updateAfterEvent();
}

function makeItDisappear(mcItem:MovieClip):Void {
mcItem._alpha -= 10;
if (mcItem._alpha<=0) {
clearInterval(nIntervalDisappear);
nIntervalDisappear = 0;
if (mcItem == mcRinser) {
rinsed = true;
}
}
updateAfterEvent();
}
kglad
1/13/2006 2:11:21 AM
you're not clearing those intervals. you shouldn't have nIntervalAppear in
quotes inside those conditional statements. try:

if (intName == nIntervalAppear) {
clearInterval(nIntervalAppear);
nIntervalAppear = 0;
} else if (intName == nIntervalAppear2) {
clearInterval(nIntervalAppear2);
nIntervalAppear2 = 0;
}

if you want to test for data type in addition to equal variable values use
===, but that doesn't appear necessary in this situation.
AddThis Social Bookmark Button