A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: Functions on different Layers.

  1. #1
    Mental Ward Patient Blips's Avatar
    Join Date
    May 2005
    Posts
    482

    Functions on different Layers.

    I've just started working on my engine again, but it seems i cant call functions from different layers, which is VERY annoying considering i like to organize my code on different layers. Is this true or am i just doing something wrong?

  2. #2
    smile for the camera! jesserules's Avatar
    Join Date
    Dec 2004
    Location
    a dark place where people have trouble in flash
    Posts
    720
    well, to call functions from another layer, it has to be a layer that is on top of the layer thats calling it, otherwise the function doesnt exist yet.
    My site
    The Current Project is RoboRace.
    Screens Public Beta Coming Soon!

  3. #3
    Mental Ward Patient Blips's Avatar
    Join Date
    May 2005
    Posts
    482
    OOOOooo thnx so much !

  4. #4
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    I thought it depended on how you're calling the function.

    function myFunction() {
    }


    Should work fine no matter where it's declared, but

    myFunction = function() {
    };


    Will only exist once the script is run... right?
    http://www.birchlabs.co.uk/
    You know you want to.

  5. #5
    Untitled-2.fla
    Join Date
    Jul 2002
    Posts
    391
    ^^ they'll work the same, but are used for two different things

    myFunction = function() is used for methods or prototyping

    var myObj = new Object
    myObj.functionName = function ( )

  6. #6
    smile for the camera! jesserules's Avatar
    Join Date
    Dec 2004
    Location
    a dark place where people have trouble in flash
    Posts
    720
    i just have experience with functions doing this.
    My site
    The Current Project is RoboRace.
    Screens Public Beta Coming Soon!

  7. #7
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    Functions are 'created' when the script is executed.

    When the playhead gets to a frame, it executes the code from the top frame down, so you can call functions on a lower frame, but not the opposite.

    However, in my opinion, the correct way to do it is to have the playhead move over your function declerations, and then move on to your game logic.

    For my games, I go to an 'init' frame which executes and creates all my functions, then I go to the menu. After that I never have a need to touch the init frame again, as from then on, I can call the functions from anywhere.

  8. #8
    leight.com.au leight's Avatar
    Join Date
    Sep 2002
    Location
    australia
    Posts
    1,437
    iopred, wouldn't that be faster also? Because during the actual game, the flash player doesn't have to run through the function code again, unless it is being called up?

    - leighton

  9. #9
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    I would assume so yes.

  10. #10
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    The difference between the two function declarations of :-

    code:

    function myFunction() {
    }



    and

    code:

    myFunction = function() {
    };



    is that the latter can not be executed by a statement before it is created i.e. if some line contains a call to it before its declaration in the same frame for example, then it won't be executed, but the previous one does.

    First type this in your first frame and run it:-

    code:

    scan();
    function scan()
    {
    trace("called");
    }



    then try this:-

    code:

    scan();
    scan = function()
    {
    trace("called");
    }



    you can see the difference.

    edit:
    iopred, wouldn't that be faster also? Because during the actual game, the flash player doesn't have to run through the function code again, unless it is being called up?

    - leighton
    I don't think the flash player runs through the function code untill it is evoked, so it should'nt make much or any difference
    Last edited by adit_ya_sharma; 05-23-2005 at 12:32 AM.
    -Aditya

  11. #11
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    I think leight was talking about flash actually creating the objects to hold the functions, if every frame your redefining the functions, it should be slower than defining it only once.

  12. #12
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    yeah ofcourse

    edit: I was wondering though, will this be feasable, I have many mc's in my side scrolling game which have the same code running on them individually with one 'onEnterFrame' for each one. Now, since the code in all of them is the same, will doing something like this will make it any faster
    1) I make the whole code in a function body on the main timeline like:-

    code:

    checky = function()
    {
    the code here....
    }



    and in all the mc's i can write this:-

    code:

    onEnterFrame = function()
    {
    _root.checky();
    }


    ???
    Last edited by adit_ya_sharma; 05-23-2005 at 01:31 AM.
    -Aditya

  13. #13
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    It will call the function, but you will find it will not work as you think it will.

    For it to work as you hope, you should do something as such:

    code:

    onClipEvent(load){
    this.frame = _root.frame;
    }

    onClipEvent(enterFrame){
    this.frame();
    }



    or

    code:

    this.frame = _root.frame;
    this.onEnterFrame = this.frame;



    Whenever you reference variables inside the frame function, remember to use this, eg: this._x.

  14. #14
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    Yes, those variaqbles always gave me trouble whenever I tried that, thanks for the help. Iam not pretty sure though if it will be much faster but it would be more organized for sure
    -Aditya

  15. #15
    Heli Attack! iopred's Avatar
    Join Date
    Jun 2003
    Location
    Sydney, Australia
    Posts
    923
    The abundance of enterFrames will kill you.

    Best thing to do is throw all the MC's into an array, and call a function in them explicitly.

  16. #16
    doItLikeThis
    Join Date
    Jan 2004
    Location
    :noitacoL
    Posts
    1,080
    Are you talking about checking through a for loop? I already done that and did'nt see much performance gain. If you're talking about something else than can you please show me a rough example?
    -Aditya

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