A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Redefining a function on the fly? Possible?

  1. #1
    Game Developer
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    292

    Question 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

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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;

  3. #3
    Senior Member
    Join Date
    Jul 2006
    Location
    San Jose, CA
    Posts
    334
    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!

  4. #4
    Game Developer
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    292
    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

  5. #5
    AS3 Mod
    Join Date
    Sep 2007
    Location
    O-H-I-O
    Posts
    2,385
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center