MY first AS 2.0 problem

Hi
I’ve recreated the problem as a simple example to demonstrate it best.
It’s bizarre. Here’s the class:


class MyClass 
{
	// For setInterval:
	public var _intervalID:Number;

	public function MyClass()
	{
		init();
	}
	public function init():Void
	{
		trace("init");
		_intervalID = setInterval(this, "update", 20);
	}
	public function update():Void
	{
		trace("update");
	}
}

If i try to create an instance of the class inside an event handler, the setInterval function within the class never executes. The trace statement should repeatedly display “update” in the output panel.


box.onPress = function()
{
	var test:MyClass = new MyClass();
}

Why? Thanks so much if you can help.

Dene

Well, first of all, I don’t think setInterval() returns anything, so your _intervalID doesn’t ever get defined. You could basically get rid of it. Also, since you’re not setting the interval from outside of the object, you don’t need to pass it an object reference in the arguments. You would call it just like you would have if you were calling it from a function you’ve created on a frame.

Basically your class should look something like this:
[AS]
class MyClass {
public function MyClass() {
init();
}
public function init():Void {
setInterval(update, 500);
}
public function update():Void {

	trace("update");
}

}
[/AS]

That should do it for ya.

Hi,

Because the test variable is declared inside the button event handler using var, it is local to that function and hence it is deleted when the function has executed and before the interval has had chance to run. you could solve this by declaring the variable outside the button event handler like this,

[AS]
var test:MyClass;
box.onPress = function() {
test = new MyClass();
};
[/AS]

Hm… No, that’s not true. The act of running the function will create an instance of his class. The only way to get rid of that instance would be to either kill the movie, delete the instance, or set it’s value to “undefined”. Basically every single time the user clicks the button or symbol or whatever fires the function, a new object with the type of “MyClass” is going to be created, thus causing multiple intervals being fired.

Basically, if his class looked something like this:
[AS]
class MyClass {

public function MyClass(int:Number) {
	setInterval(update, 1000, int);
}
public function update(int):Void {
	trace("update from object #" + int);
}

}
[/AS]

And add a click counter to his main movie like this:

[AS]
var clicks:Number = 0;

box.onPress = function() {
clicks++;
var test:MyClass = new MyClass(clicks);
}
[/AS]

And then clicked “box” three times, his output would look like this:

update from object #1
update from object #2
update from object #3

Also, putting the instantiation of an object only used within a function outside of the function is poor encapsulation, and generally not best practice.

If he alters his class to implement the changes I’ve made, (the ones in my previous post) his code will work without changing his main movie.

Cool thanks a lot both of you. I think i have it sussed now. So…

Because the variable perished after the function call, so did the instace of the class. But the setInterval didn’t perish as it can only be deleted with clearInterval. However the setInterval function failed to work as the referenced object ‘this’ was no longer around.

_intervalID = setInterval(update, 50);

I’ll go back to using the simple version of setInterval inside my classes.
Cheers!
Dene

So does it work now? Cause I’ve had problem where I was trying to call MovieClip methods without realizing I should specify a timeline (unlike in mx)… try extending the MovieClip class in your code and see if that runs :slight_smile:

No problem! If you have any more trouble with it, let us know… =)

Just thought i’d post some feedback:

Using the form of setInterval() without the reference to ‘this’ meant that the function called by the interval didn’t have access to the properties of the class.

Back to using the full version of setInterval() !

something like this should work okay,

[AS]
class MyClass {
private var intervalID:Number;
public function MyClass() {
init();
}
public function init():Void {
intervalID = setInterval(this, “update”, 20);
}
private function update():Void {
trace(“update”);
}
public function clearUpdate():Void { // assuming something doesn’t clear the interval internally add a method so the movie can stop it
clearInterval(intervalID);
}
}
[/AS]

then in the movie,

[AS]
var test:MyClass;
start_btn.onPress = function() {
test = new MyClass();
};
stop_btn.onPress = function() {
test.clearUpdate();
};
[/AS]