[Flash Basic theory Lesson 06] Make Move role [key class]

Source: Internet
Author: User

Back to "flash Basic Theory Class-Catalog"

Train of thought: 1. Use keyboard to control MC movement, such as: Key.isdown (key.right);

2. And agreed to the scope of the MC move: Top, bottom, left, right.

Step 1:

Make a role, save as a movie clip, and the instance is named "MC".

Step 2:

As code layer:

var speed = 12;

//移动速度:每次移动的距离

var top = mc._height/2;
var bottom = Stage.height-mc._height/2;
var left = mc._width/2;
var right = Stage.width-mc._width/2;


_root.onEnterFrame = function () {
if (Key.isDown(Key.DOWN) && mc._y<bottom) {
mc._y += speed;
}
if (Key.isDown(Key.UP) && mc._y>top) {
mc._y -= speed;
}
if (Key.isDown(Key.RIGHT) && mc._x<right) {
mc._x += speed;
}
if (Key.isDown(Key.LEFT) && mc._x>left) {
mc._x -= speed;
}
};

Flash charge: Get keyboard character method

var ml = new Object();

Key.addListener(ml);
ml.onKeyDown = function() {

var kd = Key.getAscii();

trace(kd)

}

//给ml这个Object添加一个键盘侦听,ml开始侦听按下Key的AscII码。

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.