Skip to main content
Participating Frequently
August 4, 2011
Answered

Movieclip(root) doesn't work inside a function

  • August 4, 2011
  • 2 replies
  • 1721 views

Hey everyone,

I'm having this problem.

In fact, when i publish my FLA with player 9, there is no prob.

When i publish with selecting player 10, there is this problem.

I paste my code :

var leng:int = MovieClip(root).program.websiteXML.pages.titlePage.length();
trace ("leng"+leng);
     
setInterval(function () : void {

          if (MovieClip(root).program.autoCh==true){
                      
           trace("CHANGE");
                            
          
          MovieClip(root).allContent.controllMenu2['itemButton_' + MovieClip(root).program.linkPic].gotoAndPlay("s4");
          
          if (MovieClip(root).program.linkPic < int(leng-1)){
                    MovieClip(root).program.linkPic++;
                       }else{
                           //MovieClip(root).allContent.play ();
                         MovieClip(root).program.autoCh=false;
                         MovieClip(root).program.linkPic=1;
                         }
                      
               
                         MovieClip(root).allContent.controllMenu2['itemButton_' + MovieClip(root).program.linkPic].gotoAndPlay("s4");
                         

                         
                         
                         for (var z:uint=1; z<(MovieClip(root).program.linkPic+1); z++) {
                              var tempMC:MovieClip = MovieClip(MovieClip(root).allContent.controllMenu2)["itemButton_"+z];
                              Tweener.addTween(tempMC, { x:(z-1)*(45+1)-353, time:1} );     
                              
                         }
                         
                         for (var j:uint=(MovieClip(root).program.linkPic+1); j<7; j++) {
                              var tempMC2:MovieClip = MovieClip(MovieClip(root).allContent.controllMenu2)["itemButton_"+j];
                              Tweener.addTween(tempMC2, { x:(j-1)*(45+1)+352, time:1} );     
                              
                         }
                         
                         MovieClip(root).allContent.controllMenu2['itemButton_' + MovieClip(root).program.linkPic].gotoAndPlay("s3");
                         
                    

                   }}, 4000);

For leng variable, there is no problem, the value is taken.

But it's inside SetIntervall, at

MovieClip(root).program.autoCh

it tells me that :

TypeError: Error #1009: Il est impossible d'accéder à la propriété ou à la méthode d'une référence d'objet nul.
    at Function/<anonymous>()[main6_cs3_fla._00button_136::frame1:32]
    at Function/http://adobe.com/AS3/2006/builtin::apply()
    at SetIntervalTimer/onTimer()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

And if I put the value outside the function :

var autoChtest = MovieClip(root).program.autoCh;

And use it :

if (autoChtest==true)

There is no errors but my function doesn't work correctly, as autoCh can be "false" in another action.

Well, I hope this will not scare you, and someone will try to help me, please.

If someone know anything about this issue or having same problem please tell me.

Thanks!

This topic has been closed for replies.
Correct answer kglad

try:

:

var leng:int = MovieClip(root).program.websiteXML.pages.titlePage.length();
trace ("leng"+leng);


var rootVar:MovieClip=MovieClip(root);
     setInterval(function () : void {           if (rootVar.program.autoCh==true){                                   trace("CHANGE");                                                rootVar.allContent.controllMenu2['itemButton_' + rootVar.program.linkPic].gotoAndPlay("s4");                     if (rootVar.program.linkPic < int(leng-1)){                    rootVar.program.linkPic++;                        }else{                            //MovieClip(root).allContent.play ();                          rootVar.program.autoCh=false;                          rootVar.program.linkPic=1;                          }                                                               rootVar.allContent.controllMenu2['itemButton_' + rootVar.program.linkPic].gotoAndPlay("s4");                                                                                                     for (var z:uint=1; z<(MovieClip(root).program.linkPic+1); z++) {                               var tempMC:MovieClip = rootVar.allContent.controllMenu2)["itemButton_"+z];                               Tweener.addTween(tempMC, { x:(z-1)*(45+1)-353, time:1} );                                                            }                                                   for (var j:uint=rootVar.program.linkPic+1; j<7; j++) {                               var tempMC2:MovieClip = rootVar.allContent.controllMenu2)["itemButton_"+j];                               Tweener.addTween(tempMC2, { x:(j-1)*(45+1)+352, time:1} );                                                            }                                                  rootVar.allContent.controllMenu2['itemButton_' + rootVar.program.linkPic].gotoAndPlay("s3");                                                                 }}, 4000);


2 replies

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
August 5, 2011

try:

:

var leng:int = MovieClip(root).program.websiteXML.pages.titlePage.length();
trace ("leng"+leng);


var rootVar:MovieClip=MovieClip(root);
     setInterval(function () : void {           if (rootVar.program.autoCh==true){                                   trace("CHANGE");                                                rootVar.allContent.controllMenu2['itemButton_' + rootVar.program.linkPic].gotoAndPlay("s4");                     if (rootVar.program.linkPic < int(leng-1)){                    rootVar.program.linkPic++;                        }else{                            //MovieClip(root).allContent.play ();                          rootVar.program.autoCh=false;                          rootVar.program.linkPic=1;                          }                                                               rootVar.allContent.controllMenu2['itemButton_' + rootVar.program.linkPic].gotoAndPlay("s4");                                                                                                     for (var z:uint=1; z<(MovieClip(root).program.linkPic+1); z++) {                               var tempMC:MovieClip = rootVar.allContent.controllMenu2)["itemButton_"+z];                               Tweener.addTween(tempMC, { x:(z-1)*(45+1)-353, time:1} );                                                            }                                                   for (var j:uint=rootVar.program.linkPic+1; j<7; j++) {                               var tempMC2:MovieClip = rootVar.allContent.controllMenu2)["itemButton_"+j];                               Tweener.addTween(tempMC2, { x:(j-1)*(45+1)+352, time:1} );                                                            }                                                  rootVar.allContent.controllMenu2['itemButton_' + rootVar.program.linkPic].gotoAndPlay("s3");                                                                 }}, 4000);


GDOnizukaAuthor
Participating Frequently
August 6, 2011

That's what i tried in my previous message, that's what i thought there was perhaps a better way to fix my problem.

But if you tell me to do this, it's surely the best way.

Anyway thanks for your answers, it works now !

But just to know, can you tell me why in this function root is null ?

Thanks!

kglad
Community Expert
Community Expert
August 6, 2011

when you use setInterval(), you're losing scope.

in general, you shouldn't use setInterval().  i use it when i'm in a hurry but i consider myself an expert in its use and quirks.  for anybody else, it has too many quirks. further, you should never use it the way you're using it as in-line function:  it makes your code difficult to read and difficult to debug.

because as3 has the timer class that does everything setInterval can do (and more), you should use the timer class.  it doesn't loose scope in the called/listener function and is much less likely to lead to run-away loops the do nothing useful and eat cpu cycles.

kglad
Community Expert
Community Expert
August 5, 2011

what's frame 1, line 32?

GDOnizukaAuthor
Participating Frequently
August 5, 2011

Sorry,

frame 1, line 32 is that line :

if (MovieClip(root).program.autoCh==true){


And I believe that this is "MovieClip(root)" syntaxic form that cause errors.

kglad
Community Expert
Community Expert
August 5, 2011

i doubt it (unless that code is in a class file) but you can test.  above line 32, add:

trace(MovieClip(root));

trace(MovieClip(root).program);

//

you'll probably find you have no program object on your root timeline when that code executes.