flash 中的简单图片裁剪(拼图代码)

package {
import flash.display.MovieClip;
import flash.display.Loader;
import flash.utils.Timer;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.events.*;
import flash.geom.Rectangle;
import flash.geom.Point;
public class PT extends MovieClip {
public var map:DiTu;
public var nummc:Array = new Array();
public var pt:Array = new Array();
public var nt:Array = new Array();
public var tpbh:uint;
public var timerpt:Timer=new Timer(1000);
public var ptnum:uint;
public var ptmax:uint;
public var tn:uint;
public var ptn:uint;
public var ptoldx,ptoldy;
public var loader:Loader;
public var URLstring:String;
public function PT() {
var xml:XML =new XML();
var urlloader:URLLoader = new URLLoader();
urlloader.load(new URLRequest("config.xml"));
urlloader.addEventListener(Event.COMPLETE,Loadxml);
function Loadxml(e:Event) {
var xml:XML=XML(urlloader.data);
ptmax=xml.child(0).child(0).child(0);
go();
}
}
public function go()
{
tn=0;
ptn=6;
ptnum=9;
this.txt.text=ptn+"";
tpbh=Math.round(Math.random()*(ptmax-1)+1);
URLstring="pic"+tpbh+".jpg";
//trace(URLstring);
loader=new Loader();
loader.load(new URLRequest(URLstring));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
}
public function completeHandler(e:Event) {
var $width:uint,$height:uint;
var i:uint;
$width=loader.width/3;
$height=loader.height/3;
var allbmd:BitmapData=new BitmapData(loader.width,loader.height);
allbmd.draw(loader);
for (i=1; i<=9; i++) {
pt[i]=this.getChildByName("pic"+i);
nt[i]=this.getChildByName("n"+i);
var bmd:BitmapData=new BitmapData($width,$height);
var tx:uint,ty:uint;
tx=int((i-1)%3)*$width;
ty=int((i-1)/3)*$height;
var rect:Rectangle=new Rectangle(tx,ty,tx+$width,ty+$height);
bmd.copyPixels(allbmd,rect,new Point(0,0),bmd);
var bmp:Bitmap=new Bitmap(bmd);
var mc:MovieClip = new MovieClip();
mc.addChild(bmp);
this.addChild(mc);
mc.name=i+"";
nummc[i]=mc;
nummc[i].x=pt[i].x;
nummc[i].y=pt[i].y;
nummc[i].scaleX=pt[i].width/$width;
nummc[i].scaleY=pt[i].height/$height;
nummc[i].visible=false;
var bmp2:Bitmap=new Bitmap(bmd);
var mc2:MovieClip = new MovieClip();
mc2.addChild(bmp2);
this.addChild(mc2);
mc2.name=(i+9)+"";
nummc[i+9]=mc2;
nummc[i+9].x=nt[i].x;
nummc[i+9].y=nt[i].y;
nummc[i+9].scaleX=nt[i].width/$width;
nummc[i+9].scaleY=nt[i].height/$height;
}
timerpt.start();
timerpt.addEventListener(TimerEvent.TIMER,pttimerhd);
}
public function pttimerhd(e:Event) {
tn++;
if (tn<=6) {
ptn--;
this.txt.text=ptn+"";
} else if (tn==7) {
ptn=15;
this.txt.text=ptn+"";
ptdl();
} else {
ptn--;
this.txt.text=ptn+"";
if (ptn==0) {
ptfw();
map.back();
this.parent.removeChild(this);
}
}


}
public function ptfw() {
for (var i=1; i<=9; i++) {
nummc[i].visible=true;
nummc[i+9].visible=true;
nummc[i].parent.removeChild(nummc[i]);
nummc[i+9].parent.removeChild(nummc[i+9]);
}
timerpt.stop();
}
public function ptdl() {
var temp=Math.round(Math.random()*50+15);
var tempx,tempy;
var n,m;
for (var i=1; i<=temp; i++) {
n=Math.round(Math.random()*8+1);
m=Math.round(Math.random()*8+1);
tempx=nummc[n+9].x;
tempy=nummc[n+9].y;
nummc[n+9].x=nummc[m+9].x;
nummc[n+9].y=nummc[m+9].y;
nummc[m+9].x=tempx;
nummc[m+9].y=tempy;
}
for (i=1; i<=9; i++) {
nummc[i+9].addEventListener(MouseEvent.MOUSE_DOWN,ptmd);
}
}
public function ptmd(e:Event) {
var t=e.target.name;
ptoldx=nummc[t].x;
ptoldy=nummc[t].y;
nummc[t].startDrag();
nummc[t].addEventListener(MouseEvent.MOUSE_UP,ptmup);
nummc[t].removeEventListener(MouseEvent.MOUSE_DOWN,ptmd);
}
public function ptmup(e:Event) {
var t=e.target.name;
nummc[t].stopDrag();
if (nummc[t].hitTestObject(nummc[t-9])) {
ptnum--;
nummc[t].visible=false;
nummc[t-9].visible=true;
if (ptnum==0) {
ptfw();
this.parent.removeChild(this);
return;
}
} else {
nummc[t].addEventListener(MouseEvent.MOUSE_DOWN,ptmd);
}
nummc[t].x=ptoldx;
nummc[t].y=ptoldy;
nummc[t].removeEventListener(MouseEvent.MOUSE_UP,ptmup);
}
public function setmap(m:DiTu) {
map=m;
}


}


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值