A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] trouble calling function

  1. #1
    Member
    Join Date
    Sep 2003
    Location
    canada
    Posts
    70

    resolved [RESOLVED] trouble calling function

    Hello,

    I am using Flash CS3 with actionScript 2.

    I have created a funtion that works fine, but I've decided to call it from a different location and it is not working.

    Here is the code I am using:

    PHP Code:
    //create a string
    var hiliname:String = new String("");
    //give the string a value
    hiliname "ib1";
    //trace the string - this works 
    trace(hiliname);

    //find out if there is an a or b or c in the string 
    if (hiliname.substr(1,1) == "a") {
        
    trace ("found an a");
        
    expand1();
            
    } else if (
    hiliname.substr(1,1) == "b") {
    //trace to show the string found a 'b' - this works
        
    trace ("found a b");
    //call the funtion - here is where the problem lies
        
    expand2();
            
    } else if (
    hiliname.substr(1,1) == "c") {
        
    trace ("found a c");
        
    expand3();
    }

    expand2 = function () {
    //trace to show the function is called
        
    trace ("called expand2");

    this is what traces:

    PHP Code:
    ib1
    found a b 
    I also call the expand2 function from a button, and that works (traces "called expand 2") but it doesn't work with the above if statement. I am confused by this because the trace in the if statement works.

    All of my code is in the same location, at the root level.

    Can anybody help ?

  2. #2
    Member
    Join Date
    Sep 2003
    Location
    canada
    Posts
    70
    Ok - I've figured it out. I need to declare what my function is before trying to call it - otherwise flash doesn't know what I'm talking about. In case anyone else had a problem like this, here is how the code should be written:

    PHP Code:
    //create new string variable
    var hiliname:String = new String("");

    //declare what the variable is
    hiliname "ib1";


    //declare function called expand2
    expand2 = function () {
        
    trace ("called expand2");
    }

    //check to see if the variable contains an a,b or c
    if (hiliname.substr(1,1) == "a") {
        
    trace ("found an a");
        
    expand1();
            
    } else if (
    hiliname.substr(1,1) == "b") {
        
    trace ("found a b");
    //call the function created above if a 'b' is in the variable name
        
    expand2();
            
    } else if (
    hiliname.substr(1,1) == "c") {
        
    trace ("found a c");
        
    expand3();


  3. #3
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864
    You know, there's another way of declaring a function:

    Actionscript Code:
    function expand2(){
        trace ("called expand2");
    }

    Then, no matter where your code is, before or after the function, it will be called
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

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