【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
上次对图片的大小有要求,这次对图片的大小没有要求,但对长宽比不是4:3的,会自动进行裁剪,使长宽比为4:3。同时,增加一个重置按钮,这样重玩的时候不用关闭flash player。但是改来改去,改得自己也糊涂了,代码非常乱,有没有大神可以帮我看看代码可以怎样写得思路更清晰些。
文档类
[Actionscript3] 纯文本查看 复制代码 package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFormat;
public class Main extends Sprite
{
private var url:String = "funian2.jpg";//要裁入的位图名,与flash文件在同一文件夹中
private var img_arr:Array;//用来存放Cut类分割好的小块
private var ditu:Array;//用来记录每个小块在第几列第几行
private var pailie:Array;//用来存放每个小块的坐标
private var pailie1:Array;
private var img:Cut;
private var kuang:Sprite;
private var btn1:Sprite;
private var btn2:Sprite;
private var img_sprite:Sprite;
private var se:uint;
private var sd:Sound;
private var txt:TextField;
private var txt_format:TextFormat;
private var sleep:uint = 0;
public function Main()
{
// constructor code
sd = new Sound();
sd.addEventListener(Event.COMPLETE,init);
sd.load(new URLRequest("11152.mp3"));//加入了一个点击时的音效,同在flash文件夹中
}
private function init(evt:Event):void
{
se = 192;
txt = new TextField();
txt_format = new TextFormat();
txt_format.size = 60;
txt_format.color = 0xff9900;
addChild(txt);
txt.x = 960 - txt.width;
txt.y = 96;
txt.text = "0";
txt.setTextFormat(txt_format);
kuang = new Sprite();
kuang.graphics.lineStyle(3,0xff0000);
//kuang.graphics.beginFill(0x663399);
kuang.graphics.drawRect(0,0,se * 4,se * 3);
kuang.graphics.endFill();
addChild(kuang);
btn1 = new Sprite();
btn1.graphics.lineStyle(1);
btn1.graphics.beginFill(0xff9900);
btn1.graphics.drawRoundRect(0,0,50,25,10,10);
btn1.graphics.endFill();
addChild(btn1);
btn1.x = 850;
btn1.y = 400;
btn1.addEventListener(MouseEvent.CLICK,startGame);
img = new Cut(url,4,3);
img.addEventListener(Cut.CHILD_OVER,over);
}
private function over(evt:Event):void
{
img_arr = img.arr;
ditu = new Array(new Array(4),new Array(4),new Array(4));//用来记录每个小块在第几列第几行
pailie = new Array();//用来存放每个小块的坐标
pailie1 = new Array();
sleep = 0;
for (var i:uint = 0; i<3; i++)
{
for (var j:uint = 0; j<4; j++)
{
var zuobiao:Array = [j * se,i * se];
pailie.push(zuobiao);
pailie1.push(zuobiao);
}
}
for (var o:uint = 0; o<img_arr.length; o++)
{
img_arr[o].x = pailie1[o][0];
img_arr[o].y = pailie1[o][1];
kuang.addChild(img_arr[o]);
img_arr[o].addEventListener(MouseEvent.CLICK,dong);
}
}
private function startGame(evt:Event):void
{
if (kuang.numChildren != 0)
{
for (var num_k:uint = kuang.numChildren; num_k > 0; num_k--)
{
kuang.removeChildAt(0);
}
}
sleep = 0;
txt.text = String(sleep);
txt.setTextFormat(txt_format);
for (var i:uint = 0; i<3; i++)
{
for (var j:uint = 0; j<4; j++)
{
ditu[i][j] = 1;
}
}
for (var n:uint = 0; n<img_arr.length-1; n++)
{
var num:uint = Math.floor(Math.random() * pailie.length);
var weizhi:Array = pailie[num];
pailie.splice(num,1);
img_sprite = img_arr[n];
kuang.addChild(img_sprite);
img_sprite.x = weizhi[0];
img_sprite.y = weizhi[1];
ditu[weizhi[1] / se][weizhi[0] / se] = 0;
}
pailie = pailie1.slice(0);
}
private function dong(evt:MouseEvent):void
{
var x_myself:uint = evt.target.x / se;
var y_myself:uint = evt.target.y / se;
var zuo:uint = x_myself - 1;
var you:uint = x_myself + 1;
var shang:uint = y_myself - 1;
var xia:uint = y_myself + 1;
var sd_n:uint = 0;
if (zuo!=4294967295 && ditu[y_myself][zuo] == 1)
{
ditu[y_myself][x_myself] = 1;
ditu[y_myself][zuo] = 0;
evt.target.x -= se;
sd_n = 1;
sleep += 1;
}
else if (you!=4 && ditu[y_myself][you] == 1)
{
ditu[y_myself][x_myself] = 1;
ditu[y_myself][you] = 0;
evt.target.x += se;
sd_n = 1;
sleep += 1;
}
else if (shang!=4294967295 && ditu[shang][x_myself]==1)
{
ditu[y_myself][x_myself] = 1;
ditu[shang][x_myself] = 0;
evt.target.y -= se;
sd_n = 1;
sleep += 1;
}
else if (xia!=3 && ditu[xia][x_myself]==1)
{
ditu[y_myself][x_myself] = 1;
ditu[xia][x_myself] = 0;
evt.target.y += se;
sd_n = 1;
sleep += 1;
}
txt.text = String(sleep);
txt.setTextFormat(txt_format);
if (sd_n == 1)
{
sd.play();
}
}
}
}
图片裁剪类
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;
import flash.geom.Matrix;
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=3):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);
//trace(wid_bit);
if (wid_bit>=hei_bit)//根据宽和高的比为4:3来确定怎么裁剪
{
wid_bit = hei_bit;
}
else
{
hei_bit = wid_bit;
}
var matrix:Matrix = new Matrix();
matrix.scale(192/wid_bit,192/hei_bit);
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);
var c_data:BitmapData = new BitmapData(192,192);
b_data.copyPixels(bit_data,rect,point);
c_data.draw(b_data,matrix);
var b_bitmap:Bitmap = new Bitmap(c_data);
rongqi = new Sprite();
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;
}
public function get wAndh():uint
{
return wid_bit;
}
}
}
|