【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
图片处理类:作用是载入一个图片,并将图片分割成几个小块,存入一个数组,供文档类调用
[Actionscript3] 纯文本查看 复制代码 package
{//此类是加载一个图片,并将图片切割后将其存入一个数组
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.geom.Rectangle;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.display.Sprite;
public class Cut extends EventDispatcher
{
private var x_stage:uint;
private var y_stage:uint;
private var wid_bit:uint;
private var hei_bit:uint;
private var bit_arr:Array = [];
private var url:String;
private var rongqi:Sprite;
public static var CHILD_OVER:String = "child_over";
public function Cut(url_b:String,x_stages:uint=4,y_stages:uint=4):void
{
// 需传递参数,图片地址,横向分几份,给向分几份
url = url_b;
x_stage = x_stages;
y_stage = y_stages;
init(url);
}
private function init(url_bit:String):void
{//加载图片
var bit_load:Loader=new Loader();
bit_load.contentLoaderInfo.addEventListener(Event.COMPLETE,startCut);
bit_load.load(new URLRequest(url_bit));
}
private function startCut(evt:Event):void
{//图片加载完成后,将图片进行分割,并存入数组
var bit_load:Bitmap = evt.target.content as Bitmap;
var bit_data:BitmapData = bit_load.bitmapData;
wid_bit = Math.round(bit_load.width / x_stage);
hei_bit = Math.round(bit_load.height / y_stage);
for (var i:uint=0; i<y_stage; i++)
{
for (var j:uint=0; j<x_stage; j++)
{
var rect:Rectangle = new Rectangle(wid_bit * j,hei_bit * i,wid_bit,hei_bit);
var point:Point = new Point(0,0);
var b_data:BitmapData = new BitmapData(wid_bit,hei_bit);
b_data.copyPixels(bit_data,rect,point);
var b_bitmap:Bitmap = new Bitmap(b_data);
rongqi = new Sprite();
//b_bitmap.x = wid_bit * j + 5 * j;
//b_bitmap.y = hei_bit * i + 5 * i;
rongqi.addChild(b_bitmap);
rongqi.mouseChildren = false;
bit_arr.push(rongqi);
}
}
dispatchEvent(new Event(Cut.CHILD_OVER));
}
public function get arr():Array
{
return bit_arr;
}
}
}
文档类:考虑到第一个小块都是正方形,因此事先将图片改成456*342大小,后面的代码都是按12个小块写的
[Actionscript3] 纯文本查看 复制代码 package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
public class Main extends Sprite
{
private var url:String = "funian.jpg";//要裁入的位图名,与flash文件在同一文件夹中
private var img_arr:Array;//用来存放Cut类分割好的小块
private var ditu:Array = new Array(new Array(4),new Array(5),new Array(4));//用来记录每个小块在第几列第几行
private var pailie:Array = new Array();//用来存放每个小块的坐标
private var img:Cut;
private var kuang:Sprite;
var img_sprite:Sprite;
private var se:uint;
public function Main()
{
// constructor code
init(url);
}
private function init(u:String):void
{
kuang = new Sprite();
kuang.graphics.lineStyle(3,0xff0000);
//kuang.graphics.beginFill(0x663399);
kuang.graphics.moveTo(0,0);
kuang.graphics.lineTo(456,0);
kuang.graphics.lineTo(456,114);
kuang.graphics.lineTo(570,114);
kuang.graphics.lineTo(570,228);
kuang.graphics.lineTo(456,228);
kuang.graphics.lineTo(456,342);
kuang.graphics.lineTo(0,342);
kuang.graphics.lineTo(0,0);
kuang.graphics.endFill();
addChild(kuang);
for (var i:uint = 0; i<3; i++)
{
if (i==1)
{
se = 5;
}
else
{
se = 4;
}
for (var j:uint = 0; j<se; j++)
{
ditu[i][j] = 1;
var zuobiao:Array = [j * 114,i * 114];
pailie.push(zuobiao);
}
}
img = new Cut(u,4,3);
img.addEventListener(Cut.CHILD_OVER,xianshi);
}
private function xianshi(evt:Event):void
{
img_arr = img.arr;
for (var n:uint = 0; n<img_arr.length; n++)
{
var num:uint = Math.floor(Math.random() * pailie.length);
var weizhi:Array = pailie[num];
img_sprite = img_arr[n];
pailie.splice(num,1);
addChild(img_sprite);
img_sprite.x = weizhi[0];
img_sprite.y = weizhi[1];
ditu[weizhi[1] / 114][weizhi[0] / 114] = 0;
//trace(ditu[0][0]);
img_sprite.addEventListener(MouseEvent.CLICK,dong);
}
}
private function dong(evt:MouseEvent):void
{
var x_myself:uint = evt.target.x/114;
var y_myself:uint = evt.target.y/114;
var zuo:uint = x_myself - 1;
var you:uint = x_myself + 1;
var shang:uint = y_myself - 1;
var xia:uint = y_myself + 1;
if (zuo!=4294967295 && ditu[y_myself][zuo] == 1)
{
ditu[y_myself][x_myself] = 1;
ditu[y_myself][zuo] = 0;
evt.target.x -= 114;
}
else if (you!=4 && y_myself !=1 && ditu[y_myself][you] == 1)
{
ditu[y_myself][x_myself] = 1;
ditu[y_myself][you] = 0;
evt.target.x += 114;
}
else if (you!=5 && y_myself ==1 && ditu[y_myself][you] == 1)
{
ditu[y_myself][x_myself] = 1;
ditu[y_myself][you] = 0;
evt.target.x += 114;
}
else if (shang!=4294967295 && ditu[shang][x_myself]==1)
{
ditu[y_myself][x_myself] = 1;
ditu[shang][x_myself] = 0;
evt.target.y -= 114;
}
else if (xia!=3 && ditu[xia][x_myself]==1)
{
ditu[y_myself][x_myself] = 1;
ditu[xia][x_myself] = 0;
evt.target.y += 114;
}
}
}
}
|