先从FLASH说起。我要达到的效果是点击地面,人物就走到点击的地点。思路:一个鼠标监听器监听鼠标的点击事件,把X座标和Y座标传到角色,做为角色的目的地。角色每一帧都向这个目的地移动一点点。
role_mc为场景里的一个MovieClip
role_mc.x = role_mc._x;
role_mc.y = role_mc._y;
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
moveRole(role_mc, _xmouse, _ymouse);
};
Mouse.addListener(mouseListener);
function moveRole(role:MovieClip, x:Number, y:Number) {
role.x = x;
role.y = y;
role.onEnterFrame = function() {
if (this.x != this._x) {
this._x += this.x-this._x>0 ? 1 : -1;
}
if (this.y != this._y) {
this._y += this.y-this._y>0 ? 1 : -1;
}
if (this.x == this._x && this.y == this._y) {
delete this.onEnterFrame;
}
};
}
role_mc.y = role_mc._y;
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
moveRole(role_mc, _xmouse, _ymouse);
};
Mouse.addListener(mouseListener);
function moveRole(role:MovieClip, x:Number, y:Number) {
role.x = x;
role.y = y;
role.onEnterFrame = function() {
if (this.x != this._x) {
this._x += this.x-this._x>0 ? 1 : -1;
}
if (this.y != this._y) {
this._y += this.y-this._y>0 ? 1 : -1;
}
if (this.x == this._x && this.y == this._y) {
delete this.onEnterFrame;
}
};
}

收藏到QQ书签