Re: [Flashcoders] SetInterval Magic
Dhiraj Girdhar wrote: Hi Guys, I am using second option of setInterval, but I am not able to understand the scope issue here. If you pass a function, the function is not called as part of its original class. An example: class myClass { private var myNumber = 5; public function doSomething() { trace("myNumber is " + myNumber); } } With setInterval(doSomething, 1000), you'll trace "myNumber is undefined" once a second, because the function is not being called as a method of myClass, so it doesn't have access to the other members of the class. With setInterval(this, doSomething, 1000), it works. Or setInterval(someOtherClassObject, otherFunction, 1000). Imagine that if you don't specify the parent object of the method, the method is "uprooted" from the object, and forced to exist as an anonymous function. Is there any solution other than Delegates? As, I am working for Flash 6 also. Before Flash 7, I think people wrote their own Delegate classes. Perhaps someone can provide sample code. I know that when I worked with Flash 5 and 6, I didn't know about Delegates at all, so I cursed the scope rules daily. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] SetInterval Magic
Hi Guys, I am using second option of setInterval, but I am not able to understand the scope issue here. Is there any solution other than Delegates? As, I am working for Flash 6 also. 'D' -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Alias Sent: Wednesday, January 11, 2006 6:09 PM To: Flashcoders mailing list Subject: Re: [Flashcoders] SetInterval Magic Hi guys, It's important to realise that setInterval has two different ways to call it. The first, and most commonly used, is this: setInterval(functionReference:Function, interval:Number, [param1:Object, param2, ..., paramN]) : Number The second, which is less well known, but substantially more reliable, is this: setInterval(objectReference:Object, methodName:String, interval:Number, [param1:Object, param2, ..., paramN]) : Number Although the first example will work, the function will execute in the wrong scope. The second, however, because it allows you to specify the scope in which it executes, is far more reliable. For example, if I'm inside a class, and want to call a public method of that same class every 1000 ms, I would use: setInterval(this,"myFunction",1000); What's important to understand here is the difference between passing the name of the function, and *the function itself* - which is what's happening in the first example. There has been a lot of confusion about this over the past few years, mostly due to ambiguous documentation. Have a look here for a well written piece of documentation on the subject: http://livedocs.macromedia.com/flash/8/main/1766.html Hope this helps, Alias On 1/11/06, Sönke Rohde <[EMAIL PROTECTED]> wrote: > Hi, > This is a scope-issue. Try > > import mx.utils.Delegate > ... > setInterval(Delegate.create(this, func), 2000); > > Cheers, > Sönke > > > Hi Coders, > > > > In following code, I am creating a new dynamic movie clip (in > > function N1 of script object) and associating the handler for unload > > event. Next I am deleting the same movie clip in function N3 of script > > object. But if we are calling the function N3 through setInterval > > function, movieclip unload event is not called. > > > > Now if we are calling the same function N3 directly without > > setInterval, > > unload event will be called. > > > > Someone please tell me what is happening here? > > > > > > > > script = new Object(); > > > > Delay = new Object(); > > > > > > > > // Delay Object > > > > Delay.delay = function(obj, func) > > > > { > > > > this.mObj = obj; > > > > this.mFunc = func; > > > > // Calling function after an interval of 2 seconds. > > > > setInterval(this.func, 2000, this); > > > > } > > > > > > > > Delay.func = function(obj) > > > > { > > > > var temp = obj; > > > > // Calling function N3 of class script. > > > > temp.mObj[temp.mFunc](); > > > > } > > > > > > > > // Script Object > > > > script.N1 = function() > > > > { > > > > // Creating a new movie clip. > > > > _root.createEmptyMovieClip("Dhiraj", 1); > > > > > > > > // Associating unload event with it. > > > > _root.Dhiraj.onUnload = function() > > > > { > > > > trace("Dhiraj Unload"); > > > > } > > > > > > > > // Storing the movie clip instance in member variable. > > > > this.mMC = _root.Dhiraj; > > > > > > > > // Calling next function of script. > > > > this.N2(); > > > > } > > > > > > > > script.N2 = function() > > > > { > > > > // Calling N3 after some delay. > > > > Delay.delay(this, "N3"); > > > > } > > > > > > > > script.N3 = function() > > > > { > > > > this.mMC.removeMovieClip(); > > > > } > > > > > > > > script.N1(); // Calling first function of script. > > > > > > > > Regards: > > > > Dhiraj > > > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] SetInterval Magic
Hi guys, It's important to realise that setInterval has two different ways to call it. The first, and most commonly used, is this: setInterval(functionReference:Function, interval:Number, [param1:Object, param2, ..., paramN]) : Number The second, which is less well known, but substantially more reliable, is this: setInterval(objectReference:Object, methodName:String, interval:Number, [param1:Object, param2, ..., paramN]) : Number Although the first example will work, the function will execute in the wrong scope. The second, however, because it allows you to specify the scope in which it executes, is far more reliable. For example, if I'm inside a class, and want to call a public method of that same class every 1000 ms, I would use: setInterval(this,"myFunction",1000); What's important to understand here is the difference between passing the name of the function, and *the function itself* - which is what's happening in the first example. There has been a lot of confusion about this over the past few years, mostly due to ambiguous documentation. Have a look here for a well written piece of documentation on the subject: http://livedocs.macromedia.com/flash/8/main/1766.html Hope this helps, Alias On 1/11/06, Sönke Rohde <[EMAIL PROTECTED]> wrote: > Hi, > This is a scope-issue. Try > > import mx.utils.Delegate > ... > setInterval(Delegate.create(this, func), 2000); > > Cheers, > Sönke > > > Hi Coders, > > > > In following code, I am creating a new dynamic movie clip (in > > function N1 of script object) and associating the handler for unload > > event. Next I am deleting the same movie clip in function N3 of script > > object. But if we are calling the function N3 through setInterval > > function, movieclip unload event is not called. > > > > Now if we are calling the same function N3 directly without > > setInterval, > > unload event will be called. > > > > Someone please tell me what is happening here? > > > > > > > > script = new Object(); > > > > Delay = new Object(); > > > > > > > > // Delay Object > > > > Delay.delay = function(obj, func) > > > > { > > > > this.mObj = obj; > > > > this.mFunc = func; > > > > // Calling function after an interval of 2 seconds. > > > > setInterval(this.func, 2000, this); > > > > } > > > > > > > > Delay.func = function(obj) > > > > { > > > > var temp = obj; > > > > // Calling function N3 of class script. > > > > temp.mObj[temp.mFunc](); > > > > } > > > > > > > > // Script Object > > > > script.N1 = function() > > > > { > > > > // Creating a new movie clip. > > > > _root.createEmptyMovieClip("Dhiraj", 1); > > > > > > > > // Associating unload event with it. > > > > _root.Dhiraj.onUnload = function() > > > > { > > > > trace("Dhiraj Unload"); > > > > } > > > > > > > > // Storing the movie clip instance in member variable. > > > > this.mMC = _root.Dhiraj; > > > > > > > > // Calling next function of script. > > > > this.N2(); > > > > } > > > > > > > > script.N2 = function() > > > > { > > > > // Calling N3 after some delay. > > > > Delay.delay(this, "N3"); > > > > } > > > > > > > > script.N3 = function() > > > > { > > > > this.mMC.removeMovieClip(); > > > > } > > > > > > > > script.N1(); // Calling first function of script. > > > > > > > > Regards: > > > > Dhiraj > > > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] SetInterval Magic
Hi, This is a scope-issue. Try import mx.utils.Delegate ... setInterval(Delegate.create(this, func), 2000); Cheers, Sönke > Hi Coders, > > In following code, I am creating a new dynamic movie clip (in > function N1 of script object) and associating the handler for unload > event. Next I am deleting the same movie clip in function N3 of script > object. But if we are calling the function N3 through setInterval > function, movieclip unload event is not called. > > Now if we are calling the same function N3 directly without > setInterval, > unload event will be called. > > Someone please tell me what is happening here? > > > > script = new Object(); > > Delay = new Object(); > > > > // Delay Object > > Delay.delay = function(obj, func) > > { > > this.mObj = obj; > > this.mFunc = func; > > // Calling function after an interval of 2 seconds. > > setInterval(this.func, 2000, this); > > } > > > > Delay.func = function(obj) > > { > > var temp = obj; > > // Calling function N3 of class script. > > temp.mObj[temp.mFunc](); > > } > > > > // Script Object > > script.N1 = function() > > { > > // Creating a new movie clip. > > _root.createEmptyMovieClip("Dhiraj", 1); > > > > // Associating unload event with it. > > _root.Dhiraj.onUnload = function() > > { > > trace("Dhiraj Unload"); > > } > > > > // Storing the movie clip instance in member variable. > > this.mMC = _root.Dhiraj; > > > > // Calling next function of script. > > this.N2(); > > } > > > > script.N2 = function() > > { > > // Calling N3 after some delay. > > Delay.delay(this, "N3"); > > } > > > > script.N3 = function() > > { > > this.mMC.removeMovieClip(); > > } > > > > script.N1(); // Calling first function of script. > > > > Regards: > > Dhiraj > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders