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码。