king of everything:我有一个Flash制作的问题,急需Flash高手解答.

来源:百度文库 编辑:神马品牌网 时间:2024/05/11 02:30:30
我在FlashMX2004里面做了一个甲虫,想用键盘方向键控制它的上下左右移动,怎样编脚本

onClipEvent (load) {
// 初始化小猪的移动速度
speed = 5;
}
onClipEvent (enterFrame) {
//左方向
if (Key.isDown(Key.LEFT) && !Key.isDown(Key.RIGHT)) {
this._x -= speed;
this.gotoAndStop("left_pos");
_root.left_mc.gotoAndStop(2);
} else {
_root.left_mc.gotoAndStop(1);
}
//右方向
if (Key.isDown(Key.RIGHT) && !Key.isDown(Key.LEFT)) {
this._x += speed;
this.gotoAndStop("right_pos");
_root.right_mc.gotoAndStop(2);
} else {
_root.right_mc.gotoAndStop(1);
}
//上方向
if (Key.isDown(Key.UP) && !Key.isDown(Key.DOWN)) {
this._y -= speed;
this.gotoAndStop("up_pos");
_root.up_mc.gotoAndStop(2);
} else {
_root.up_mc.gotoAndStop(1);
}
//下方向
if (Key.isDown(Key.DOWN) && !Key.isDown(Key.UP)) {
this._y += speed;
this.gotoAndStop("down_pos");
_root.down_mc.gotoAndStop(2);
} else {
_root.down_mc.gotoAndStop(1);
}
//重新调整角色的位置
if (this._y<30) {
this._y = 360;
}
if (this._y>360) {
this._y = 30;
}
if (this._x<120) {
this._x = 470;
}
if (this._x>470) {
this._x = 120;
}
}

以上是一只小猪用键盘控制的脚本,你改成虫子就好了!~

把甲虫拉到场景上,然后选中那个甲虫,按F8,在弹出的面板选“影片剪辑”并确定,然后选中场景上甲虫的影片剪辑,按F9打开动作面板,写下下面的代码就行了:
on (keyPress "<Left>") {
this._x -= 5;
}
on (keyPress "<Right>") {
this._x += 5;
}
on (keyPress "<Up>") {
this._y -= 5;
}
on (keyPress "<Down>") {
this._y += 5;
}