Groups | Blog | Home
all groups > flash actionscript > may 2004 >

flash actionscript : is it possible to make a blocking function?


sneakyimp
5/7/2004 11:59:30 PM
hi:

i'm currently working on building a modal dialog component. is it possible to
make a blocking function in flash?

in other words, I want to be able to write something like this:

if (myPromptUserWithDialog("Are you sure?")) {
}

and code execution will halt until the dialog is dealt with.
kglad
5/8/2004 12:53:50 AM
there's no blocking function, but there should be a workable solution for your
situation. if you're seeking user confirmation to procede, you probably have
already elicited user input to initiate some action. that call for action can
be delayed until your user has confirmed they wish to procede.
sneakyimp
5/8/2004 1:12:27 AM
yes...i've been thinking about how to do it....a loop sounds like a bad idea
because it would chew up CPU, no?

i've also considered a timer...checking every 10 ms or so to see if a flag has
been set. i also like the callback notion.


kglad
5/8/2004 1:25:24 AM
sneakyimp
5/8/2004 3:14:03 AM
this isn't for any specific situation...i'm trying to make a general purpose
component.

it just seems like it would be super nice in a bit of code if you could take a
pause in the excecution of your code to prompt the user for a yes or no answer
(or perhaps some more detailed data). I can easily see how to define a
changeHandler function for my dialog but sometimes it's super convenient just
to have a prompt dialog. i can't remember where i've seen this...maybe in
Javascript? Visual Basic?

as it stands, you have to divide your code up into frames or scenes if you
want your code execution to wait on an answer. it's kind of nice sometimes to
have all your code in one spot...saves you a few mouse moves when moving back
and forth between functions.


kglad
5/8/2004 3:22:43 AM
you don't have to break code into more than one frame to hold execution at any
given point. just break your code into more than one function. call the first
function when you want code to begin execution and make the function call to
the next bit of coding dependent upon user input, possibly displaying an
intermediate result based on the calculations done up to that point.
sneakyimp
5/9/2004 11:55:56 PM
I guess I see what you mean...still trying to get my head around it.

I've attached my dialog object (my first flash object class!--you'll see the
background from macromedia's object demo in the file). I've created an object
that will prompt the user with a single message, let them click OK or CANCEL,
and then call the callback function you've specified with a true or false value
depending on what they clicked. the dialog darkens the entire movie in the
background (and you can't click anything underneath) and you can drag the
dialog around by its title bar.

i just wish that the PromptUser function that i've written could return the
result directly instead of having to call a callback function....it PromptUser
was a blocking function, you could do something like this:

if (PromptUser("Are you sure you want to do that?", "My Dialog Title",
"callbackFn")) {
// plan A
} else {
// plan B
}


kglad
5/10/2004 2:48:44 AM
sneakyimp
5/10/2004 4:31:53 PM
Carl Fink
5/10/2004 5:26:08 PM
Simple way to make an object modal:

1. Create it (or swapDepths it) on the highest layer
2. On the next layer down, create an empty MovieClip
(by drawing a rectangle and converting it) that's the
same size as the stage.
3. GIve it onPress and onRelease (and optionally
onReleaseOutside) handlers, which don't actually
have to do anything.
4. Set its alpha to zero.
5. In frame 1 set it to _visible = false.

When you summon your modal dialog box, also set
blocking_mc._visible = true. It's still not actually
VISIBLE (being alpha=0), but now it will intercept
all mouse clicks. If anything on your stage responds
to keystrokes, block them, too.

The OK button on your dialog should set
blocking_mc._visible = false.
kglad
5/11/2004 1:52:17 AM
i have your file. it looks like it works well to me. is there something that's not working?
sneakyimp
5/11/2004 2:20:56 AM
no it works...it's just not a true blocking function....so i can't do this:

if (PromptUser("Is Plan A OK?", "My Dialog Title", "callbackFn")) {
// plan A
} else {
// plan B
}

I don't see an easy way to get my PromptUser function to return a value and so
the programming seems a bit convoluted:

PromptUser("Is Plan A OK?", "My Dialog Title", "callbackFn")
function callbackFn(result) {
if (result) {
//plan a
} else {
//plan b
}
}

i need to define a separate function and i can't easily prevent the code in
that frame after my PromptUser() call from running. Imagine that i had to
gather 5 subsequent data calls from a user (in order!). this would seem to
require that i create at least 5 functions...and the readability is really
going to suffer:

PromptUser("Do you want option 1?", "My Dialog Title", "callbackFn")
function callbackFn1(result) {
if (result) {
//plan a
} else {
//plan b
}
}

// this code is going to execute almost concurrently with the above code
because the function
// doesn't block...if this code needs to have option 1 resolved first, i'm
going to have to define it within
// the callbackFn1 or move it to a different frame
PromptUser("Do you want option 2?", "My Dialog Title", "callbackFn")
function callbackFn2(result) {
if (result) {
//plan a
} else {
//plan b
}
}

on sort of another front, i'm curious about how to define components...looks
to me like flash lets you define your own components and add them to the
component window. is there any documentation on this functionality? i haven't
seen much in the help file.
kglad
5/11/2004 2:10:20 PM
i don't see a problem except you should stop thinking of this as a blocking
function. you might be better served to think of each step, that requires user
input, as a separate function. for each user input that you need you'll have a
separate function. at the end of each function another call would be made to
the next function and that function would call your dialog component to obtain
the data it needs to complete its task. your functions execute in the order
you disignate and there's no concern about timing the execution of the code.
each time your dialog component is called you may want to pass the name of the
next function that the dialog component will call. that will simplify your
coding: when your component is called it knows which function to call next
with the user's input.
sneakyimp
5/13/2004 7:30:05 PM
i guess you're right. it would be possible to make it blocking (perhaps with a
loop that checked a variable) but this would probably chew up CPU cycles and be
pointless. flash does multi tasking well...there's no SLEEP function or WAIT
function so i might as well just get used to what flash does.
AddThis Social Bookmark Button