all groups > flash actionscript > september 2004 >
You're in the

flash actionscript

group:

key listener help



Re: key listener help Patrick Bay
9/8/2004 6:04:01 PM
flash actionscript: Hi Scott,

Well, pausing your movie is a little tricky. The timeline keeps running
even if you issue a stop action, so you have to issue a stop to all of
your movie clips. In the example below, the only timeline that's stopped
is the root:

var myKeyListener:Object=new Object();
myKeyListener.onKeyDown=function() {
if (Key.getCode()==32) {
_root.stop();
}
}
Key.addListener(myKeyListener);


If you have any other movie clips running, you have to issue stop
actions to them as well.

Regards,
Patrick

[quoted text, click to view]
key listener help Scott Sosebee
9/8/2004 9:58:03 PM
I need to allow a user to pause an SWF by pressing the space bar, and the start
it again by doing the same. I was told to use a "keylistener", but I am a
BEGINNER to action scripting (much more of an animator). I have downloaded a
script that lets me show dialogue when keys are pressed, but still cannot
figure out the pause thing. If anyone could offer any help; I would really
appreciate it.

Best,
Scott
Re: key listener help kglad
9/8/2004 10:03:23 PM
myListener = new Object();
myListener.onKeyDown = function() {
if (Key.getCode() == Key.SPACE) {
if (!toggle) {
stop();
toggle = 1;
} else {
play();
toggle = 0;
}
}
};
Key.addListener(myListener);
Re: key listener help blenz
9/8/2004 10:08:58 PM
hi again, hope this can help :
assuming the name of the movie that is playing that you want to pause is
called showCaseMovie

var spaceListener:Object = new Object();
spaceListener.onKeyUp = function() {

if (Key.getCode() == Key.SPACE) {
if(movieState==undefined)
{
showCaseMovie.stop();
movieState="paused";
}
else
{
showCaseMovie.play();
delete movieState
}


}
};
Key.addListener(spaceListener);

so essentially your space bar acts as both the trigger to pause and play
again the movie...
hope that helps
Re: key listener help Scott Sosebee
9/9/2004 2:34:02 PM
Thanks sooo much. I put an action into every movie and movie clip (the entire
swf runs in a "shell: load and unload movies). Regardless, it works great!
IHowever, I dont know why it works, but it does. Thanks again!

Scott
Re: key listener help kglad
9/9/2004 2:43:36 PM
if you want one piece of code that will stop and start all moviecips you can
use:

myListener = new Object();
myListener.onKeyDown = function() {
if (Key.getCode() == Key.SPACE) {
if (!toggle) {
toggle = 1;
} else {
toggle = 0;
}
pauseF(_root,toggle);
}
};
pauseF(mc,param){
for(obj in mc){
if(typeof(mc[obj])=="moviecip"){
if(toggle){
mc[obj].stop();
} else {
mc[obj].play();
}
pauseF(mc[obj],param);
}
}
}
Re: key listener help Scott Sosebee
9/9/2004 3:09:15 PM
well, I spoke too soon.
The piece of code that was suggested to stop all movies returned this error:

**Error** Scene=Scene 1, layer=actions, frame=1:Line 8: Syntax error.
pauseF(mc,param){

Total ActionScript Errors: 1 Reported Errors: 1


the code below worked if I put it in every movie and movie clip, as well as in
the main "shell movie" in every frame where a new movie loads. However, it only
works if I view it in Flash. If I wief the movie in Flash Player (which is how
it will be viewd). The key actions are totally ignored. It also would sometimes
start the movie clip from tghe beginning instead of pausing. Any ideas?

Thanks again!


myListener = new Object();
myListener.onKeyDown = function() {
if (Key.getCode() == Key.SPACE) {
if (!toggle) {
stop();
toggle = 1;
} else {
play();
toggle = 0;
}
}
};
Key.addListener(myListener);

Re: key listener help kglad
9/9/2004 3:19:17 PM
that should be:

Re: key listener help Scott Sosebee
9/9/2004 3:36:55 PM
kglad;
I want to thank you in advance. I hope I'm not bugging you.

With that said, I'm not receiving a syntax error anymore, however, that piece
of code is yielding no results. it seems to be completely ignored. I put it in
my main "shell" movie as an action at the beginning of the timeline. I also
tried placing it in every action frame where a new movie is loaded. It still
did nothing.

Any ideas would be mucho appreciated.

best.
Re: key listener help kglad
9/9/2004 7:14:03 PM
i had another typo using toggle instead of param in the body of pauseF().
also, that code won't control the _root timeline so i added code. and finally,
you don't want to put that code in a frame that's looped because you don't want
to continually define new listeners:

myListener = new Object();
myListener.onKeyDown = function() {
if (Key.getCode() == Key.SPACE) {
toggle = !toggle;
pauseF(_root, toggle);
}
};
function pauseF(mc, param) {
for (obj in mc) {
if (typeof (mc[obj]) == "movieclip") {
trace(mc[obj]);
if (param) {
_root.stop();
mc[obj].stop();
} else {
_root.play();
mc[obj].play();
}
pauseF(mc[obj], param);
}
}
}
Key.addListener(myListener);
Re: key listener help sneakyimp
12/11/2004 8:28:25 PM
Re: key listener help kglad
12/11/2004 8:44:23 PM
Re: key listener help sneakyimp
12/11/2004 9:33:07 PM
sorry...it was in blenz's post: if (Key.getCode() == Key.SPACE) {
if(movieState==undefined) { showCaseMovie.stop(); movieState='paused'; } else {
showCaseMovie.play(); delete movieState } } }; Key.addListener(spaceListener);
Re: key listener help NSurveyor
12/11/2004 9:38:40 PM
Re: key listener help sneakyimp
12/11/2004 9:57:22 PM
i would like to refer to movieState from some other location--NOT within the
listener function. like in a totally different movie. would movieState be
defined within some other movie clip? would you need a path to it?
Re: key listener help NSurveyor
12/11/2004 10:07:30 PM
You would. SO, make it global:
if (Key.getCode() == Key.SPACE) {
if(movieState==undefined)
{
showCaseMovie.stop();
_global.movieState="paused";
}
else
{
showCaseMovie.play();
delete movieState
}


}
};
Key.addListener(spaceListener);
AddThis Social Bookmark Button