# MY first AS 2.0 problem

**URL:** <https://forum.kirupa.com/t/my-first-as-2-0-problem/35095>\
**Category:** Uncategorized\
**Created:** [November 10, 2003, 2:16pm UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095 "2003-11-10T14:16:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dene](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@dene](https://forum.kirupa.com/u/dene)\
**Post date:** [November 10, 2003, 2:16pm UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/1 "2003-11-10T14:16:08Z")

</div>

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

```auto

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.

```auto

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

```

Why? Thanks so much if you can help.

Dene

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2003, 5:13pm UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/2 "2003-11-12T17:13:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2003, 5:48pm UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/3 "2003-11-12T17:48:16Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 12, 2003, 7:04pm UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/4 "2003-11-12T19:04:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 13, 2003, 12:59am UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/5 "2003-11-13T00:59:37Z")

</div>

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.

```auto
_intervalID = setInterval(update, 50);

```

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 13, 2003, 2:35am UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/6 "2003-11-13T02:35:01Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 13, 2003, 1:59pm UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/7 "2003-11-13T13:59:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 14, 2003, 2:00am UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/8 "2003-11-14T02:00:26Z")

</div>

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() !

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [November 14, 2003, 2:17am UTC](https://forum.kirupa.com/t/my-first-as-2-0-problem/35095/9 "2003-11-14T02:17:34Z")

</div>

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]
