代码写入MC
// 侦听键盘
onClipEvent (load) {
myListener = new Object();
myListener.onKeyDown = function() {
kpress = 1;
};
myListener.onKeyUp = function() {
kpress = 0;
};
Key.addListener(myListener);
}
// 设置初始值
onClipEvent (load) {
// 移动x初速度
var speed = 0;
// 移动y初速度
var yspeed = 0;
// 是否按键
var kpress = 0;
// 最大速度
var maxs =15;
}
// 设置最大速度
onClipEvent (enterFrame) {
if (speed>maxs) var speed = maxs;
if (speed<-maxs)var speed = -maxs;
if (yspeed>maxs) var yspeed = maxs;
if (yspeed<-maxs) var yspeed = -maxs;
}
// 设置移动
onClipEvent (enterFrame) {
_x += speed;
_y += yspeed;
if ( kpress == 0) {
if (speed>0) speed--;
if (speed<0) speed++;
if (yspeed>0) yspeed--;
if (yspeed<0) yspeed++;
if (_x>530||_x<20) var speed=0;
if (_y>380||_y<20) var yspeed=0;
}
// 设置按键
if (Key.isDown(Key.LEFT)) {
speed--;
if (_x<=20) var speed=0;
}
if (Key.isDown(Key.RIGHT)) {
speed++;
if (_x>=530) var speed=0;
}
if (Key.isDown(Key.UP)) {
yspeed--;
if (_y<=20) var yspeed=0;
}
if (Key.isDown(Key.DOWN)) {
yspeed++;
if (_y>=380) var yspeed=0;
}
}