怎样用键盘中的上下左右控制动画!
onClipEvent (load) {
speed = 5;// 当 MC 载入时,设置速度为 5 ,也就是说每按一下方向键,就移动 5 象素
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
this._x -= speed;// 当 ← 被按下时,自身的 X 位置不断减 speed , X 轴坐标不断减小,也就是不断向左移动
}
if (Key.isDown(Key.RIGHT)) {
this._x += speed;// 当 → 被按下时,自身 X 位置不断加 speed , X 轴坐标不断增加,也就是不断向右移动
}
if (Key.isDown(Key.UP)) {
this._y -= speed;// 当 ↑ 被按下时,自身的 Y 位置不断减 speed , Y 轴坐标不断减小,也就是不断向上移动
}
if (Key.isDown(Key.DOWN)) {
this._y += speed;// 当 ↓ 被按下时,自身的 Y 位置不断加 speed , Y 轴坐标不断增加,也就是不断向下移动
}
}
onEnterFrame = function () {
if (Key.isDown(Key.LEFT)) {
mc._x -= 20;
}
if (Key.isDown(Key.RIGHT)) {
mc._x += 20;
}
if (Key.isDown(Key.UP)) {
mc._y -= 20;
}
if (Key.isDown(Key.DOWN)) {
mc._y += 20;
}
if (mc._x<0) {
mc._x = 0;
}
if (mc._x>550-mc._width) {
mc._x = 550-mc._width;
}
if (mc._y<0) {
mc._y = 0;
}
if (mc._y>400-mc._height) {
mc._y = 400-mc._height;
}
}