Two ways to control movie clips with the keyboard in Flash

Source: Internet
Author: User
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
}
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.