A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [F9 AS 2.0] BIG function access problem

  1. #1
    Senior Member olias32's Avatar
    Join Date
    Jan 2003
    Location
    Romania
    Posts
    126

    [F9 AS 2.0] BIG function access problem

    This happens in F8 as well, so its a macromedia thing

    OK i have a huge problem:
    A while back while learning about how functions work in AS 2.0 i understood that i can call a function before declaring it and it should work.

    This works within the same frame:

    Code:
    test_function();
    
    function test_function() {
      trace("test");
    }
    What i dont get is if i have 2 layers and i declare the function on the bottom layer and call it in the top layer (the one above), it doesn't work.
    I have to switch the layer order to get this working

    Is this a bug i have or just macromedia stupidity?

    How can flash not recongnize a function if its on the same frame in the timeline, just a different layer?

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    Flash runs actionscripts in the frames in the order: top to bottom.

    So it will parse and run your top frame actions, then parse the lower layer actions and run them and so on...

    So in the above case since flash hasnt already parsed the function (in the bottom layer) it will not work..

    heres an example which may make it clearer, put this in your top frame:

    trace("top");
    for (var i:Number = 0; i<10; i++) {
    trace(i);
    }

    and put this in your bottom layer:

    trace("bottom layer");
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Senior Member olias32's Avatar
    Join Date
    Jan 2003
    Location
    Romania
    Posts
    126
    Well i got the idea.
    What seems strange to me is this:

    Inside a single frame, even though flash reads one line at a time, and the following code
    Code:
    trace("A");
    trace("B");
    will have the following output
    Code:
    A
    B
    if i call a function that has not yet been declared, it works, so flash reads ahead inside a single frame and this outputs correctly
    Code:
    test(5);
    function test(i) {
    trace(i);
    }
    traces:
    Code:
    5
    So in this case, flash does not read top to bottom, it kinda reads all ahead and then starts compiling.
    It seems strange that it does not do that for different layers also.
    It would be more logic, if it does read ahead, to read ahead everything, including other layers, initiate all the functions and then start the linear compiling.

    Am i wrong?

  4. #4
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    I know what you mean ..

    but actually flash doesnt actually read ahead as such.. when you define a function in this format

    Code:
    test();
    
    function test(){
     trace("in test")
    }
    it parses these functions before executing any code, so all the functions are 'ready' when called...

    on the other hand if you define the function using this format (even on the same frame)

    Code:
    test();
    
    test = function(){
       trace("in test");
    }
    it will not trace out, because flash does not parse functions defined in this way initially...

    anyway i would recommend not to split your actionscript across different layers (it will most definitely cause complications)

    P.S (although i know what you mean..it would be good if at compilation, the layers were 'flattened w.r.t the AS' and the actionscript read from top-down across the layers)
    Last edited by silentweed; 09-03-2007 at 10:22 AM.
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  5. #5
    Senior Member olias32's Avatar
    Join Date
    Jan 2003
    Location
    Romania
    Posts
    126
    Thanks alot for the replies silentweed

    Will remeber the function format issue, as i may run into it in the future. I guess you saved a few hours of banging my head against the desk with that.

    Thanks
    Cheers

  6. #6
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    your welcome, glad i could help
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

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