//初始化变量
var xx = 6;
var yy = 5;
var startX = 100;
var startY = 100;
//获取被分割图片大小和
var xy = xx*yy;
attachMovie("Piece", "P", 222);//捆绑库中输出名为PIECE的MC
var wi = P.Image._width;
var hi = P.Image._height;
P.removeMovieClip();
bg_mc.swapDepths(xy+2);
myname.swapDepths(xy+3);
_root.setMask(bg_mc);//这是增加的一个MC,用来遮照整个场景。
//获取分量
var xs = wi/(xx-1);
var ys = hi/(yy-1);
//定义拖动事件
piece_event = function(){//定义一个类
this.onPress = function(){
this.swapDepths(xy+1);
startDrag(this);
}
this.onRelease = function(){
if(this._parent._xmouse>startX && this._parent._xmouse<startX+wi && this._parent._ymouse>startY && this._parent._ymouse<startY+hi){
this._x = Math.round((this._x-startX)/xs)*xs+startX;
this._y = Math.round((this._y-startY)/ys)*ys+startY;
}
stopDrag();
}
//注册该类给piece对象
piece_event.prototype = new MovieClip();//将类继承MovieClip的属性和方法
Object.registerClass("Piece", piece_event);//注册类给输出标识为PIECE的MC
这样使每一个PIECE的MC都具有piece_event类的属性和方法
//复制图片,设置每个被分割图片位置
for(i=0; i<xy; i++){
var x = (i%xx)*xs;
var y = Math.floor(i/xx)*ys;
var p = attachMovie("Piece", "Piece"+i, i+1, {_x:startX+x, _y:startY+y});
p.Image._x-=x;
p.Image._y-=y;
p.Pzzle._xscale = xs;
p.Pzzle._yscale = ys;
}