PDA

View Full Version : Help with Objects and setInterval


bboese
07-31-2005, 02:29 AM
I'm trying to create a class for use in an image gallery. This class would be used in a thumbnail image to control the fadein/out of the displayed image. Inside my thumbnail I create a new instance of the "fader" class and call the fade function. The clip that is referenced then fades out. See the code below.

Once the alpha of the clip hits zero then it tries to clear fadeInterval, and this is where the problem is showing up. The function fadeOut keeps executing infinitely even after the clearInterval call is made. I watched the fadeInterval variable using trace and noticed that it always seems to be undefined when referenced inside the fadeOut function. I assume I'm missing something with the inheritance, but can't seem to figure out why I can't clear the Interval. Any help on this would be greatly appreciated.

In the root Movie

fader = function() {}
fader.prototype.fadeInterval;
fader.prototype.delay = 10;
fader.prototype.clip;

fader.prototype.fadeOut = function (clip)
{
if(clip._alpha > 0) {
clip._alpha -=5;
}
if(clip._alpha <= 0) {
clearInterval(this.fadeInterval);
}
}

fader.prototype.fade = function ()
{
this.fadeInterval = setInterval(this.fadeOut, this.delay, this.clip);
}



In the thumbnail


on (release) {
myfade = new _root.fader;
myfade.clip=_root.test1;
myfade.fade();
}

deathcloud33
07-31-2005, 07:45 AM
In the thumbnail


on (release) {
myfade = new _root.fader;
myfade.clip=_root.test1;
myfade.fade();
}

don't you need to use some sort of initianization function to use the "new whatever();" code?
for example, it's "new Object();" not "new Object;"

bboese
07-31-2005, 03:26 PM
The parenthesis don't seem to be required, but to be consistent I should have included them. I used myfade = new _root.fader; and myfade = new _root.fader(); and both give the same result. I attempted to trace the code by inserting trace(this.fadeInterval) right after

this.fadeInterval = setInterval(this.fadeOut, this.delay, this.clip);

When I do this the value of fadeInterval is 1, but inside the fadeOut function it is undefined. It doesn't seem to be referencing the same variable even thought I'm telling it to. Perhaps I'm missing something and simply calling this.fadeInterval isn't sufficient???

bboese
07-31-2005, 03:43 PM
I figured I would post what I figured out that fixed the problem. Inside each function I use "this.fadeInterval" to reference the functions fadeInterval variable. I have a feeling that each time I used "this." I was creating a new instance of fadeInterval that was associated with that function. Basically clearInterval was trying to clear the interval associated with the fadeOut function and not the parent fader() class.

To make the function work corectly I change all references of "this.fadeInterval" to "fader.fadeInterval" and it worked like I had wanted and properly references the correct variables.