A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Check if function exists ?

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

    Check if function exists ?

    Is there a way in flash to check to see if a function/method exists before calling it?

    I tried something like if(typeof object.methodname == "function") and it didn't like it.

    Thanks!

    Ryan

  2. #2
    a.k.a gltovar deadlock32's Avatar
    Join Date
    May 2001
    Location
    Naperville,IL
    Posts
    489
    EDIT 2010: rainair just showed a situation (scroll down) where my crazy way works when dealing with private functions, and in essence might be the better way to go in general. Thanks = D

    Quote Originally Posted by senocular View Post
    you can also use in.

    Code:
    if ("methodname" in object) object.methodname();
    EDIT 2008: use the above, it's way better. ( can't believe i wrote the code below *face palm* ) ^_^

    yep ( i think this works)
    Code:
    try
    {
    var tempFunction:Function = this["functionString"]();
    }catch (err:Error)
    {
    trace("lol no function");
    }
    this["someString"]() 'this' can be substituted with objects as well.
    Last edited by deadlock32; 08-10-2010 at 01:00 PM. Reason: so people will use the better answers when google searching for this issue = D

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

    resolved

    works great. Thanx man!

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,358
    you can also use in.

    Code:
    if ("methodname" in object) object.methodname();

  5. #5
    Junior Member
    Join Date
    Sep 2008
    Posts
    1
    Hi there,

    I'm trying to implement the same thing in as2

    I'm trying to check if there is a function inside a movieclip called square called 'specialScale' is this possible.

    I tried:
    var tempFunction:Function = this.du2["specialScale"](); but it didn't seem to work.

    Any help would ber greatly appreciated!

  6. #6
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Why are you assigning the result of the function call: this['functionCall']() to a variable tempFunction that has been casted to a Function? That makes no sense.

    I know what you're trying to do, but it'd be better if used like this:
    PHP Code:
    var tempFunction:Function = this['functionCall'] as Function;
    tempFunction(); 
    It may even fix your problem tbear.

  7. #7
    Junior Member
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    2

    can't believe i wrote the code below

    While looking for a better solution on the same issue I came across this page
    ref 1: quote: deadlock32
    EDIT: use the above, it's way better. ( can't believe i wrote the code below) while referring to the try catch solution on this["methodname"]
    ref 2: quote: senocular:
    you can also use in
    if ("methodname" in object) object.methodname();
    Maybe things were slightly different then (I can't recall) but I found that both ways bear some problems. If you use senocular's suggestion, and you dealing with a private method, you are out of luck. Lets say the method onCompleteHandler() exists in a Class x and it's public:
    Code:
    trace("onCompleteHandler" in this); => TRUE
    trace(this["onCompleteHandler"] != null); => TRUE
    Now lets make the onCompleteHandler private!
    Code:
    trace("onCompleteHandler" in this); => FALSE
    trace(this["onCompleteHandler"] != null); => TRUE
    By removing the method:
    Code:
    trace("onCompleteHandler" in this); => FALSE
    trace(this["onCompleteHandler"] != null); => throws an Error
    What worked for me was exactly what deadlock32 couldn't belive he wrote it !!
    Code:
    var methodExist:Boolean;
    try {
        methodExist = this["onCompleteHandler"] !=null;
    } catch (e:Error) {
       methodExist = false;
    } finally {
    }
    Ergo: deadlock32.s "( can't believe i wrote the code below)" wasn't that bad after all. Its the only foolproof solution as far as can see.

    Rainer
    Last edited by rainair; 08-10-2010 at 12:31 PM.
    RG

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