flash actionscript:
With your function start() you are calling the constructor of the class. To make that work you need a reference to the current class which you are setting with this.o=this; Maybe this is not what you want to accomplish but that is what is happening now.
not sure what u mean. i make an instance like so: new Interval(); this will run the Class Instance constructor, which will run start, which starts the interval... Now i know i need a refrence to the class instance for setInterval. Thats why i pass it this. What i dont understand is why i have to define a property (randomly namend o) on the class and stuff a refernce to the class instance in it to make the this i pass in the setInterval work as it should. What i am trying to figure out here is why the this in setInterval by itsels does not work as it should according to the AS specs...
[quoted text, click to view] >his will run the Class Instance constructor, which will run start, which
starts the interval.. ...which calls the constructor repeatedly using setInterval by referring to 'this' in the first parameter (which works because you are setting a local reference to the class). I guess in your mind you are referencing something else with 'this' in your setInterval().
yeah, i am thinking this in setInterval is referencing the instance not the constructor. So why then is the this.o = this; working? How does callBack (or setInterval) know where to look for the right property that contains a reference to the instance...
Maybe this will help you: In OOP it can be confusing to understand if the this keyword refers to the class or to another object, especially if you have a class method that uses a callback function (like an onLoad method) An example: let's create a contructor to load XML: function LoadXML(targetXML:String) { var content_xml:XML = new XML(); content_xml.ignoreWhite = true; content_xml.onLoad = function(success:Boolean) { if(success) { // call another function in the class to do something this.parseContent(); } else { // do something else } }; Now the call this.parseContent(); won't work because the keyword this is referring to the newly created xml-object content_xml and not to the current class. So you repair this with a local reference to the class like so: function LoadXML(targetXML:String) { // create a reference to the current class var thisObj:LoadXML = this; // assuming the classname is LoadXML var content_xml:XML = new XML(); content_xml.ignoreWhite = true; content_xml.onLoad = function(success:Boolean) { if(success) { // call another function in the class to do something and now we are referring to the current class thisObj.parseContent(); } else { // do something else } }; Hope this will help you.
Hi Luigi, Thanks for all ur input. Highly appriciated. Ur last message makes perfect sense. In ur sample you are using thisObj to reference the class name and later to find parseContent(). That is basicaly what i am doing in my sample. Now, what i still dont get is that by setting a reference to thisObj i can then just use the key word this in the setInterval and all works well... from ur sample i would gather i would need to use thisObj to pass through setInterval. Which i agree makes sense. But i dont need to do this... if i do it works also... Jyst by setting thisObj to this in the constructor, this in setInterval seems to change... now that is what throws me off... does that make any sense...? Just curious... thats all ;-)
I stink at beeing a teacher! Specifically for setInterval() in classes you have to use the keyword this because setInterval doesn't have any access to classmembers. The problem lies in the rest of your coding. Only because you have set a local reference your callback() gets found. I am on a deadline but if I have any time left, I will try to elaborate on this topic.
this should work: function function_name (argument:string):Void{ trace(argument) } this.interval_ID = setInterval (this, "function_name", 1000, "hello") clearInterval(this.interval_ID) PS not a good idea to use a reserved key word as a function name IE change the name of your stop() function, as it is a meathod of the movieclip object. It won't help your code to work
using setInterval does not work as expected. (I have it working though) Anyone know why this happens? I use the sample code below in a class file. in fla i call new Interval(); Result nothing happens. But when i uncomment the line this.o = this; in the constructor, the callBack gets called... My question is why do i need to stuff a reference in an arbitrary property on the same instance to get this to work? Note: i dont pass this.o, just this in setInterval. So how setInterval or the callBack know where to look for the right reference to the scope? -- class Interval { var intervalID; var o; function Interval() { //this.o = this; start(); } function start() { intervalID = setInterval(this, "callBack", 500); } function stop() { clearInterval(intervalID); } function callBack() { trace("hi"); this.stop(); } }
I use setInterval this way: id = setInterval( function(caller){ caller.callBack(); }, 500, this ); - allways works.
sorry for being so slow. thanks again, hope u made ur deadline! thanks,
Thanks. Still working on the project and stresslevels are still rising. It was so hot here that we needed to shut down our macs for a while and let them cool. About OOP: it's just that you need to program differently. It's quite something else and it took me a while to understand the concept. After presentation of our job I will try and get back to you.
Hi Luigi, I think i may have figured it out. firstly there is setInterval(obj, method, interval) where the obj is the object the method will be called on (and will be passed as this to the method) So in my example class it should be legal to use this as the object in setInterval and setInterval should be able to call the method on this. Provided of course 'this' exists somewhere... and in my sample that is not the case. Remember i call the class like so: new Interval(); I dont store it in a variable. So it only exists for a brief moment just to call the start() and setInterval functions but than the instance is gone and the this in setInterval refers to nothing... and therefor the class doesnt work... What to do? As you more or less suggested store the instance in a property of the instance. Now the instance is kept alive and 'this' has become a vaild reference in setInterval. As a byproduct, it doesn matter what i use now in setInterval. I can use this or the property i stored the reference to the instance in... An other whay of course is to store the instance in a variable when i instantiate te class like so: var myInterval = new Interval(); Now the instance is kept alive and inside the instance this becomes also a valid reference in setInterval(). Anyway, thats my theory wether right or wrong, it works ;-) Greetingzz, J
Don't see what you're looking for? Try a search.
|