flash actionscript:
...::RDG::..,
[quoted text, click to view] > Hi, I want that when the user presses a key, "i" for
> example, a symbol appears in the screen and when
> user presses it again it diasppears. Can anyone tell
> me how to do this?
Sure thing. You've got two goals, here: 1) to make a symbol appear and
disappear, and 2) to accomplish this via key press. Let's tackle them each,
in turn.
If your symbol is a movie clip, you're in luck. All movie clip symbols
are objects, and all objects in ActionScript are defined by classes.
There's a "MovieClip class" entry of the ActionScript 2.0 Language Reference
that tells you everything you need to know about movie clips. Things they
can do are called methods; things they can react to are called events; and
characteristics they have are called properties. (This is true, by the way,
for input/dynamic textfields [the TextField class], sounds [the Sound
class], button symbols [the Button class], and, heck, just about anything
you can think of in Flash. Very handy, that the documentation is arranged
that way.)
So you'll notice the MovieClip._visible property. This determines
whether a given MovieClip instance is visible or not. Make sure to give
your movie clip an instance name (select it and see the Property inspector).
Once your movie clip has an instance name, simply use that name in place of
the class name, and you're good.
// script goes in frame 1
myMovieClip._visible = false;
Right at the beginning, then, your movie clip will be invisible.
Next, you'll want to listen for a key press. For this, you need to
research -- you guessed it -- the Key class. You'll find a Key.onKeyUp
event (remember, events are things an object can react to, such as a key
being pressed, then release). Look up that event in the Key class and
you'll see sample code right there. In your case, you'll have the event
listener set the _visible property of your movie clip to true.
Actually, since the _visible property is Boolean (true/false), you can
set it to whatever it is *not*, and that will flip visibility back and
forth.
myMovieClip._visible = !myMovieClip._visible;
.... where the logical NOT operaator (!) returns the negation of the Boolean
value.
David
stiller (at) quip (dot) net
Dev essays:
http://www.quip.net/blog/ "Luck is the residue of good design."