all groups > flash actionscript > july 2007 >
You're in the

flash actionscript

group:

Method calls on timeline (scope?)


Method calls on timeline (scope?) Some1Won
7/20/2007 5:36:30 PM
flash actionscript: Hello,

I am using flash 8/AS 2.

I am trying to call methods on the timeline from a class, and sometimes it
works, and sometimes it doesn?t.

A simplified example:
In my fla, I have 2 layers, one called ?script? and the other called
?commScript?. In layer ?script? at frame ?n? (let?s say 5) and only frame ?5?,
I have:

var applesFunction:Function = function():Void {
trace(?apples called.?);
}

In layer ?commscript?, in the first blank keyframe (and this keyframe extends
through the timeline), I have:

var grapesFunction:Function = function():Void {
trace(?grapes called.?);
}

In some method in some class (let's call ?fruitMixer?), I go:

public function test():Void {
_root.applesFunction();
_root.grapesFunction();
}

This object is also created in frame 1, in the top layer (which would be
?script?, ?commScript? is on the layer below). When I run and call test() in
the fruitMixer object, I get the output:

?apples called.?

On further poking, I find it actually can't even see the other method, a
?trace(_root.grapesFunction);? in the test() shows up as ?undefined.?

However, when I swap my layer order, suddenly, I get:

?apples called.?
?grapes called.?

While, it works, it seems pretty random. My question is: why is it working?
How does flash really handle this kind of thing? Does layer order really
matter, or was it some sort of odd fluke/ ?magic??

Tim
Re: Method calls on timeline (scope?) GWD
7/20/2007 11:02:02 PM
This is because you have declared your functions as variables. And the
variables are defined as the code executes. This same thing will happen even
with code on the same layer in the same frame.
e.g:


I think you'll find different behaviour if you change it to:

function grapesFunction():Void {
trace(?grapes called.?);
}

Here's a simple example all on one frame:

This is because you have declared your functions as variables. And the
variables are defined as the code executes. This same thing will happen even
with code on the same layer in the same frame.
e.g:


I think you'll find different behaviour if you change it to:

function grapesFunction():Void {
trace(?grapes called.?);
}
Re: Method calls on timeline (scope?) GWD
7/21/2007 12:00:00 AM
There are two things here...

You last point is absolutely correct.. all code executes for the first time in
frames when the playhead reaches that frame. This is why sometimes people code
preloader loops in the first few frames of large swfs and only send the code on
to the main part of the swf timeline when the bytesLoaded = bytesTotal.

When the playhead enters a new frame all the code is executed from top to
bottom of layers/frame. Which leads me back to your first point - you noticed
that layer order seems to matter.

I ran some tests and came up with the same results as you in terms of function
visibility by layer. I had thought (incorrectly) that functions were visible
to all code within the same frame. The reason I thought this is that layers
don't exist when actionscript is compiled to byte code (as a swf file). Layers
are just a way of breaking things up inside the flash authoring environment to
help us work on things visually in terms of how we think and organise things.

When its compiled , and the layers are converted into the swf, all the
author-time visual objects on stage retain their depth order but from an
actionscript perspective its as if all the code (on the same frame number) in
different layers was running in the same frame. A frame lower down just has its
code 'appended' to the frames above. Or so I thought....

Here's what I think now (I don't know if its accurate - perhaps someone else
can either verify or correct):
I assume its got something to do with how the compiler does things. I guess it
determines what it does from the code within each frame. My guess is that
a) when functions are compiled from each frame they are essentially treated
the same as variable function assignments but
b) are compiled to run as variable assignments at the beginning of each
original frame's code block in the order that they appear in the frame based
actionscript so
c) they are visible to all code in that frame (and all subsequent code in the
same frame on different 'layers' in flash authoring).

Example code:

//layer 1:
sayhello();
function sayhello() {
trace("hello"); //it works
saygoodbye(); //it works
sayhelloagain(); //does not execute if this function is declared on layer2
}

function saygoodbye(){
trace('goodbye');
};

//layer 2
function sayhelloagain(){
trace("hello again")
}
Re: Method calls on timeline (scope?) Some1Won
7/21/2007 2:34:30 AM
Hi GWD,

What you say makes sense: I am trying to use a variable before it is defined.
However, the reason why I have my functions in variables is originally I tried
just a straight declaration:

Layer 1 (frame 1):
function apples():Void {
trace(?apples?);
}

Layer 2 (frame 1):
function grapes ():Void {
trace(?grapes?);
}


And my object is created in layer 1.

From that object I couldn?t access grapes();. I noticed I was able to access
variables anywhere, so that?s why I had put them in variables and called them
that way.

However, from playing with it just now, I noticed it had nothing to do with
layer order... but it worked when I created my object in layer 2, rather then
layer 1.

From this, it looks like my object is able to access methods only in the
?layers above? ? And that is the scope it can access?: if I put it in the top
layer, it can only access the top layer and if I create my object on the bottom
layer, it should be able to access any method in any layer above?

And likewise on frames, if a method is on frame 5, an object in a layer below
on frame 1 can?t access it, but on or past frame 5, it can (as the play
head/flash has reached the frame and ?processed? it)?
Re: Method calls on timeline (scope?) Some1Won
7/22/2007 1:07:04 AM
Hm, I guess for now, since your conclusion is similar to mine, (and I have yet
to find any thing countering it) I will assume that this is how flash processes
script on frames. If anyone else wants to correct/add on, they are welcome to
it.

Thanks GWD.
AddThis Social Bookmark Button