-
[CS3] OnEnterFrame in a class
Hey, this is my first but probably not my last post here so just wanna say hi first
Anyways I just started working with classes in actionscript 2 and I got a problem with onEnterFrame, is the only way to work with onEnterFrame in a class to call a function in the class every frame?
Like this:
Code:
var test:MyClass = new MyClass();
this.onEnterFrame = function() {
test.frameFunction();
}
When I use onEnterFrame in a class I just get an error.
-
Total Universe Mod
Hi Dennis, welcome to the board.
onEnterFrame is a method you can only run on instances of the MovieClip class. All classes decend from the Object class but they each supply their own properties and methods such as onEnterFrame.
Your object needs a reference to the main stage (which can receive movieclip methods) upon which to assign onEnterFrame or create a movieclip and assign onEnterFrame to that.
It's bad to have a class find its way to the stage so you'll want to pass in a reference to the target to your class instance and assign to the reference.
Code:
//myclass.as
class MyClass{
private var targetClip:MovieClip;
public function MyClass(target:MovieClip){
this.targetClip = target;
this.targetClip.onEnterFrame = function(){
//enter frame code
}
}
}
Notice, MyClass object instance assigns onEnterFrame to a movieclip passed to it, not itself
Code:
var test:MyClass = new MyClass(someTarget_mc);
-
Thanks that worked inside a normal function but if I placed the onEnterFrame inside a onPress/release function like this:
Code:
class MyClass{
private var targetClip:MovieClip;
public function MyClass(target:MovieClip){
this.targetClip = target;
this.targetClip.onPress = mDown;
}
public function mDown() {
this.targetClip.onEnterFrame = function(){
trace("this is a frame");
}
}
}
the onEnterFrame doesn't work..
Last edited by Dennis_gull; 03-16-2008 at 01:36 PM.
-
Total Universe Mod
It's a scope issue. the keyword this refers to the object that called the function. In your case, targetClip calls mDown, not MyClass. So this is actually referring to targetClip, which causes flash to look for a property called targetClip on targetClip itself. You should be able to just take out the word targetClip.
Code:
class MyClass{
private var targetClip:MovieClip;
public function MyClass(target:MovieClip){
this.targetClip = target;
this.targetClip.onPress = mDown;
}
public function mDown() {
this.onEnterFrame = function(){
trace("this is a frame");
}
}
}
-
I just get an error saying "There is no property with the name 'onEnterFrame'" if I just write this.onEnterFrame.
-
I think something else is wrong, I cant even use the variables in the onRelease/onPress functions... Do I have to import something for it to work?
For example:
Code:
class aClass{
private var targetClip:MovieClip;
private var nr:Number;
public function aClass(target:MovieClip){
this.targetClip = target;
nr = 10;
this.targetClip.onPress = mDown;
}
public function mDown() {
trace(nr); // return undefined
}
}
Why cant I even trace the nr variable in the onPress function?
Last edited by Dennis_gull; 03-16-2008 at 03:55 PM.
-
I found out that I had to use Delegate, like this:
Code:
import mx.utils.Delegate;
and then when creating the onPress/onRelease/onEnterFrame function:
Code:
targetClip.onRelease = Delegate.create(this,mDown);
-
Total Universe Mod
Originally Posted by Dennis_gull
I just get an error saying "There is no property with the name 'onEnterFrame'" if I just write this.onEnterFrame.
That is a compiler error (my bad). The flash compiler see's this.onEnterFrame within a class file and assumes that onEnterFrame is a property of that class hence throwing the error.
You can 'work around' this by evaluating the name as a string rather than inserting it. My code should have looked like this:
Code:
class MyClass{
private var targetClip:MovieClip;
public function MyClass(target:MovieClip){
this.targetClip = target;
this.targetClip.onPress = mDown;
}
public function mDown() {
this["onEnterFrame"] = function(){
trace("this is a frame");
}
}
}
I was going to suggest Delegate which is far better and doesnt have to lie to the compiler but didn't want to confuse you further. Good work.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|