Flasher Archive
[Previous] [Next] - [Index] [Thread Index] - [Previous in Thread] [Next in Thread]
Subject: | FLASH: making the call action behave more like a function call |
From: | Damian Morton |
Date: | Sat, 29 Apr 2000 11:43:57 +0100 |
If you begin to use the call action a lot, things can get kind of hairy as
you try to manage passing paramaters to and from called frames. Often, you
end up clobbering variables that you wanted to maintain.
Heres my stab at how to make the call action behave more like a regular
function call.
This set of actionscripts sort of simulates a stack based function call.
There are two clip instances that hold variables at any point in a call
action. The two instances are /var and /call. /var holds local variables.
/call holds the variables you want to pass to the function you are about to
call. The first thing the called function does is Call "/stack:save", which
saves away /var, renames /call to /var, and creates a new, empty, /call
instance. The last thing a function does is to call "/stack:restore" which
basically does the reverse of the "/stack:save" operation.
The upshot of all this juggling is that each "function" now has temporary
local variables in "/var", and a set of variables to communicate to and from
called functions in "/call". If a function calls itself, or calls a function
that calls the caller, each function call has its own set of variables, and
there are no collisions.
Even if you arent using functions calling themselves (recursion), it can be
very usefull to differentiate between temporary local variables, parameters
passed to functions, and variables belonging to clip instances, especially
if there is a chain of functions calling each other.
For the object oriented types out there, clip instances correspond to object
instances, labeled actions correspond to methods, "/var:x" corresponds to a
local variable of a method, "/call:y" corresponds to an in/out parameter,
and "z" corresponds to an object property.
Heres an example of a recursive fibbonacci function implemented in
actionscript.
Library
Clip: "Stack Manager"
Label: "init"
Set Variable: "top" = 1
Duplicate Movie Clip ("/_frame_", "call", 1)
Duplicate Movie Clip ("/_frame_", "var", 0)
Set Property ("/call", Visibility) = 0
Set Property ("/var", Visibility) = 0
Stop
Label: "save"
Set Variable: "top" = top + 1
Set Property ("/var", Name) = "_stack_"&top
Set Property ("/call", Name) = "var"
Duplicate Movie Clip ("/_frame_", "call", top)
Set Property ("/call", Visibility) = 0
Label: "restore"
Remove Movie Clip ("/call")
Set Property ("/var", Name) = "call"
Set Property ("/_stack_"&top, Name) = "var"
Set Variable: "top" = top - 1
Clip: "Fibbonacci"
Label: "fib"
Comment: fib(n)
Call ("/stack:save")
If (/var:n > 1)
Set Variable: "/call:n" = /var:n - 1
Call ("fib")
Set Variable: "/var:fib" = /call:fib * /var:n
Else
Set Variable: "/var:fib" = 1
End If
Call ("/stack:restore")
Clip: "Stack Frame"
empty
you will need to lay out your movie something like this:
Main Movie
Movie Clip: Target="/stack" ID="Stack Manager"
Movie Clip: Target="/fib" ID="Fibbonacci"
Movie Clip: Target="/_frame_" ID="Stack Frame"
flasher is generously supported by...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Get the last 100 messages from the flasher list NOW
http://www.chinwag.com/flasher/last100.shtml
Flash books http://www.chinwag.com/flasher/books.shtml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To unsubscribe or change your list settings go to
http://www.chinwag.com/flasher or email helpchinwag [dot] com
Replies
FLASH: site checkish, Matt Perkins
Re: FLASH: making the call action behave, Branden Hall
Replies
Re: FLASH: replaces characters in a subs, Bill Numerick
[Previous] [Next] - [Index] [Thread Index] - [Next in Thread] [Previous in Thread]