-
Redefining a function on the fly? Possible?
Hey all,
I have a method/function inside my class called "doSomething()". This function gets called once a frame.
I am trying to figure out if it's possible to redefine that function on the fly with code.
For example, For the first 100 frames of my move I want "doSomething()" to look something like this :
Code:
public function doSelected():void {
trace("hello");
}
and then after 100 frames has passed I'd like "doSomething()" to be redefine to look like this :
Code:
public function doSelected():void {
trace("bye");
}
Is this possible?
thanks for any advice you can give!
Ryan
-
Get ready for a very subtle difference. You cannot redefine a method, which is what you have there, but you can re-assign a variable of type Function. So you could do something like this:
Code:
public var doSelected:Function = doSelected1;
public function doSelected1():void{
trace("hello");
}
public function doSelected2():void{
trace("bye");
}
at some point, you can re-assign doSelected to point to doSelected2:
Code:
doSelected = doSelected2;
-
Too fast 5. Too fast.
If you're going to just change up the trace() output, or something similar, I'd just pass an argument to the function.
Code:
function doSelected(_sayWhat:String):void {
trace(_sayWhat);
}
doSelected("hello"); //output hello
doSelected("bye"); //output bye
I (Love | Hate) Flash.
----
Save a version back so others may help you!
-
Thanks guys. What 5TonsOfFlax said was what i was looking for.
Phix, I had simplified what i was doing just for the example. Passing a variable into the method won't work for what i am actually doing. But thanks anyways
-
Also why not a simple if:
PHP Code:
public function doSelected():void { if(currentFrame <= 100) { trace("hello"); }else{ trace("bye"); } }
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
|