Control
Today to tell you about the use of the keyboard in Flash to control the movement of objects in two ways, is the use of Flash ActionScript updateafterevent and onenterframe to achieve.
When viewing the effect, click the Flash movie with your mouse to see the effect with the keyboard key.
First look at the effect 1:
Look again effect 2:
Click here to download the source file
Effect 1 I am using onenterframe to achieve, the effect of 2 I am using updateafterevent to achieve. first, declare that the frame frequency of the top two flash movies is the same 30fps. Everyone through the test above demo may find that the effect of 1 moving faster than the effect of 2 faster! Of course you can speed up by changing the frame frequency.
Mainly for you to introduce the above two kinds of effect code.
Effect 1
spritedirection = 0;
This.onenterframe = function () {
if (Key.isdown (key.right)) {
Setdirection (0);
_root.sprite._x + 3;
}
if (Key.isdown (Key.left)) {
Setdirection (1);
_root.sprite._x-= 3;
}
if (Key.isdown (Key.down)) {
_root.sprite._y + 3;
}
if (Key.isdown (key.up)) {
_root.sprite._y-= 3;
}
};
function setdirection (param) {
if (param==0) {
Sprite._xscale = 100
} else {
Sprite._xscale =-100
}
}
Effect 2:
spritedirection = 0;
SetInterval (function () {
if (Key.isdown (key.right)) {
Setdirection (0);
_root.sprite._x + 3;
}
if (Key.isdown (Key.left)) {
Setdirection (1);
_root.sprite._x-= 3;
}
if (Key.isdown (Key.down)) {
_root.sprite._y + 3;
}
if (Key.isdown (key.up)) {
_root.sprite._y-= 3;
}
Updateafterevent ();
}, 10);
function setdirection (param) {
if (param==0) {
Sprite._xscale = 100
} else {
Sprite._xscale =-100
}
}