as2.0跟鼠标移动的flash :请问如何让这段AS2.0代码到了边缘就不动了呀?

2024年11月23日 07:26
有2个网友回答
网友(1):

var mc1:MovieClip = this.myMC1;
var mc2:MovieClip = this.myMC2;
//记录MC的尺寸
var mcW:Number = mc1._width;
var mcH:Number = mc1._height;
//记录屏幕尺寸
var sw:Number = Stage.width;
var sh:Number = Stage.height;
//设置MC可移动的边界
var topM:Number = 0 + mcH * .5;
var buttonM:Number = sh - mcH * .5;
var leftM:Number = 0 + mcW * .5;
var rightM:Number = sw - mcW * .5;
//设置MC移动速度
var spX:Number;
var spY:Number;
function MoveMyMC(tar:MovieClip)
{
//随机移动:
spX = (Math.random() * sw - tar._x) / 5;
spY = (Math.random() * sh - tar._y) / 5;

tar.spX = spX;
tar.spY = spY;

//设置MC临时XY
var tx:Number;
var ty:Number;

tar.onEnterFrame = function()
{
//记录MC的XY
tx = tar._x;
ty = tar._y;
//改变MC的XY
tx += this.spX;
ty += this.spY;
//检测边界:
if (tx > rightM) {
//右边
tx = rightM;
}
if (tx < leftM) {
//左边
tx = leftM;
}
if (ty < topM) {
//上边
ty = topM;
}
if (ty > buttonM) {
//下边
ty = buttonM;
}
//更新MC位置:
this._x = tx;
this._y = ty;
};
}
this.onMouseDown = function()
{
//如果只有一个MC移动,只要使用一个就可以了:
MoveMyMC(mc1);
MoveMyMC(mc2);
};

网友(2):

//======CODE======
onClipEvent (load) {
/* 初始化变量*/
StartSize = 20;
MaxStep = 20;
SizeIncrement = 15;
/* 复制尾巴符号*/
for (var i = 1; i<=MaxStep; i++) {
duplicateMovieClip(Tail_mc, "Tail"+i, i);
with (eval("Tail"+i)) {
_xscale = StartSize+(i*SizeIncrement);
_yscale = StartSize+(i*SizeIncrement);
_alpha = 100-(100/MaxStep)*(i-1);
}
}
/* 隐藏原始的符号*/
Tail_mc._visible = false;
}
onClipEvent (enterFrame) {
/* 让第一个符号跟着鼠标移动*/
Tail1._x = _xmouse;
Tail1._y = _ymouse;
/* 让其他符号跟着前一个符号移动*/
for (var i = 2; i<=MaxStep; i++) {
Temp = eval("Tail"+i);
Temp.xdistance = (eval("Tail"+(i-1))._x-Temp._x)/2+Temp.xdistance*0.3;
Temp.ydistance = (eval("Tail"+(i-1))._y-Temp._y)/2+Temp.ydistance*0.3;
Temp._x += Temp.xdistance;
Temp._y += Temp.ydistance;
}
}
//====CODE END====