A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Help with clearInterval

  1. #1
    Junior Member
    Join Date
    May 2006
    Location
    London
    Posts
    4

    Help with clearInterval

    Hello there,

    I'm trying to code a fade in/out function to decrease/increase the opacity of a background movieclip. When the user clicks on the play button the background should fade out a bit and when the video finishes playing the background should fade in back to 100.
    The thing is that it seems that clearInterval is not working properly and as a beginner I can't see why. I've debbuged the code and the interval is not cleared so it keeps coming back to it over and over again. The 1st time it goes to fadeOut function, keeps decreasing the opacity of the movie clip and when it reaches a value less than 40 and it jumps to the clearInterval part. But it keeps going there all the time, so when the video is played the NetStream.Play.Stop part is executed so it calls the fadeIn function to start the process of increasing the opacity back to 100. But it does not work properly as because the fadeOut function is still running. So now both functions add/subtract a value from the alpha of the movieclip which at this point just keeps flickering.
    Help me please!!!

    Please take a look at the code:

    stop();


    // Fade In/Out

    mWIPPic2._alpha=100;

    function fadeOut (mClip:MovieClip):Void {
    if (mClip._alpha > 40){
    mClip._alpha=mClip._alpha-5;
    trace (mClip._alpha);
    updateAfterEvent();
    }
    else {
    clearInterval(nFadeOutInt);
    }
    };

    function fadeIn (mClip2:MovieClip):Void {
    if (mClip2._alpha < 100){
    mClip2._alpha=mClip2._alpha+1;
    trace (mClip2._alpha);
    updateAfterEvent();
    }
    else {
    clearInterval(nFadeInInt); }
    };


    // Create a NetConnection object
    var my_nc:NetConnection = new NetConnection();
    // Create a local streaming connection
    my_nc.connect(null);
    // Create a NetStream object and define an onStatus() function
    var my_ns:NetStream = new NetStream(my_nc);
    // Attach the NetStream video feed to the Video object
    my_video.attachVideo(my_ns);

    // Buffering

    // Set the buffer time
    my_ns.setBufferTime(20);

    my_ns.onStatus = function(info:Object) {

    if (info.code == "NetStream.Play.Start") {
    bufferclip._visible = false;
    playclip._visible = true;
    }

    if (info.code == "NetStream.Buffer.Full") {
    bufferclip._visible = false;
    playclip._visible = false;
    }

    if (info.code == "NetStream.Buffer.Empty") {
    bufferclip._visible = true;
    }

    if (info.code == "NetStream.Play.Stop") {
    my_ns.seek(0);
    my_ns.pause(true);
    bufferclip._visible = false;
    playclip._visible = true;
    var nFadeInInt:Number = setInterval(fadeIn,100,mWIPPic2); }

    trace (info.code);
    }

    // Begin playing the FLV file
    my_ns.play("WIP.flv");

    my_ns.pause();

    // Controls
    btRewind.onRelease = function() {
    my_ns.seek(0);
    };

    btPlay.onRelease = function() {
    var nFadeOutInt:Number = setInterval(fadeOut,100,mWIPPic2);
    my_video._visible = true;
    my_ns.pause(false);
    };

    btPause.onRelease = function() {
    my_ns.pause(true);
    };

    Thanks

    Marcello

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    This has to do with variable scope. The problem is how you define the interval.

    Here's a simple example to help you understand:
    code:

    this.onMouseDown = function()
    {
    var myInterval = setInterval(myFunction, 1000);
    };
    function myFunction()
    {
    trace("called once?");
    trace('myInterval ' + myInterval);
    clearInterval(myInterval);
    }


    In this case the interval isn't cleared, and the interval is undefined.

    Now this:
    code:

    var myInterval;
    this.onMouseDown = function()
    {
    myInterval = setInterval(myFunction, 1000);
    };
    function myFunction()
    {
    trace("called once?");
    trace('myInterval ' + myInterval);
    clearInterval(myInterval);
    }


    Now it works as expected.
    Why?
    Because you were using var that was creating a local variable inside the method. This variable isn't accessible from anywhere else, so, the interval can't be cleared.

    Please, use AS tags.

  3. #3
    Junior Member
    Join Date
    May 2006
    Location
    London
    Posts
    4
    Oh yeah, this solved my problem. Thanks very much!!!!

    http://www.ThreeCellos.co.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center