Hi everyone,
I’m trying to make a Class for a slideshow. I have a problem using the setInterval and clearInterval methods. See my code below. In the showPic function I have a conditional clearing the interval (repeating the showPic function). At least, it should do so, but it’s not working. The interval continues. I’m using “this” so that’s not the problem. Maybe it has to do with the scope of the variable to which I assigned the interval? Can anybody help on this? Thanx a lot.
class Box extends MovieClip {
private var mc:MovieClip;
private var mc2:MovieClip;
private var width_adapt: Number;
private var height_adapt: Number;
public var showPicTimer:Number;
public var resizeBoxTimer: Number;
public var checkLoadTimer:Number;
public var displayPicTimer:Number;
private var bytesloaded: Number;
private var totalloaded: Number;
// constructor
public function Box() {
_root.createEmptyMovieClip("box", 10);
mc = _level0.box;
_root.createEmptyMovieClip("pic", 2);
mc2 = _level0.pic;
}
// on initialise, draw a basic box
public function drawBox():Void {
mc.lineStyle(0, 0xFFFFFF);
mc.moveTo(0, 0);
mc.lineTo(150, 0);
mc.lineTo(150, 100);
mc.lineTo(0, 100);
mc.lineTo(0, 0);
mc._x = (Stage.width - _level0.box._width) / 2;
mc._y = (Stage.height - _level0.box._height) / 2;
loadPic();
}
// load pic but don't show
private function loadPic():Void {
mc2.loadMovie("http://www.educared.org.ar/tamtam/kmages/La-muerte-de-L-Junio-Bruto.pg.jpg");
mc2._alpha = 0;
checkLoadTimer = setInterval(this, "checkLoad", 5);
}
// check whether load is complete
private function checkLoad():Void {
bytesloaded = mc2.getBytesLoaded()/1024;
totalloaded = mc2.getBytesTotal()/1024;
if (bytesloaded == totalloaded) {
width_adapt = Math.abs((_level0.pic._width - _level0.box._width)/100);
height_adapt = Math.abs((_level0.pic._height - _level0.box._height)/100);
resizeBoxTimer = setInterval(this, "resizeBox", 1);
}
}
// resize box according to pic dimensions
private function resizeBox():Void {
clearInterval(checkLoadTimer);
if((mc2._width - mc._width) < 1 && (mc2._width - mc._width)>=0){
_level0.box._x = (Stage.width - _level0.box._width) / 2;
_level0.box._y = (Stage.height - _level0.box._height) / 2;
clearInterval(resizeBoxTimer);
mc2._x = mc._x;
mc2._y = mc._y;
mc._width = mc2._width;
mc._height = mc2._height;
showPicTimer = setInterval(this, "showPic", 50);
}
else{
_level0.box._width += width_adapt;
_level0.box._height += height_adapt;
_level0.box._x = (Stage.width - _level0.box._width) / 2;
_level0.box._y = (Stage.height - _level0.box._height) / 2;
}
}
// now show the pic
private function showPic():Void {
if(mc2._alpha < 20){
mc2._alpha += 2;
}
else {
displayPicTimer = setInterval(this, "displayPic", 5000);
clearInterval(showPicTimer);
}
}
// not finished yet: timer for showing the pic - evacuate pic - resize box back to initial dimensions
private function displayPic():Void {
mc2._alpha = 2;
clearInterval(displayPicTimer);
}
}