点击试玩.
SOURCE:
Index.as:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.TextFormat;
import flash.utils.ByteArray;
import flash.utils.getTimer;
public class PinTu extends Sprite
{
private var pt:Sprite;
private var pC:Sprite;
private var loa:Loader;
public var NUM_LOX:int = 2;
public var NUM_LOY:int = 2;
public var IsLv:int = 1;
public function PinTu()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
initView();
}
private var spriteAry:Array;
private function initCity():void{
spriteAry = new Array();
pC = new Sprite();
pC.x = 600,pC.y = 200;
for(var x1:int = 0;x1<NUM_LOX;x1++){
var spAry:Array = new Array();
for(var y1:int = 0;y1<NUM_LOY;y1++){
var re:RectSprite = new RectSprite(bitAry[x1][y1],400/NUM_LOX,400/NUM_LOY);
re.x = x1*400/NUM_LOX;
re.y = y1*400/NUM_LOY;
re.name = ""+x1+y1;
spAry.push(re);
pC.addChild(re);
}
spriteAry.push(spAry);
}
addChild(pC);
pC.addEventListener(MouseEvent.CLICK,onCitySprite);
//
}
private function initCityRand():void{
var px:int = 0;
var py:int = 0;
var NUM:int = Math.pow(NUM_LOX,2);
for(var i1:int = 0;i1<NUM;i1++){
var a:int = Math.floor((Math.random()*10)%NUM_LOX);
var b:int = Math.floor((Math.random()*10)%NUM_LOY);
var c:int = Math.floor((Math.random()*10)%NUM_LOX);
var d:int = Math.floor((Math.random()*10)%NUM_LOY);
var sps:Sprite = new Sprite();
px = (spriteAry[a][b]).x;
py = (spriteAry[a][b]).y;
spriteAry[a][b].x = spriteAry[c][d].x;
spriteAry[a][b].y = spriteAry[c][d].y;
spriteAry[c][d].x = px;
spriteAry[c][d].y = py;
sps = spriteAry[a][b] as Sprite;
spriteAry[a][b] = spriteAry[c][d];
spriteAry[c][d] = sps;
}
}
private var key:int = 0;
private var fX:int = 0;
private var fY:int = 0;
private var sX:int = 0;
private var sY:int = 0;
private var pA:Point;
private var pB:Point;
private function onCitySprite(ev:MouseEvent):void{
var pAry:Array = new Array();
var px:int = 0;
var py:int = 0;
if(key == 0 && pC.hitTestPoint(mouseX,mouseY)){
fX = Math.floor((mouseX-600)/(400/NUM_LOX));
fY = Math.floor((mouseY-200)/(400/NUM_LOY));
pA = new Point(mouseX,mouseY);
pAry = pC.getObjectsUnderPoint(pA);
//trace(" "+fX+fY);
}
if(key == 1 && pC.hitTestPoint(mouseX,mouseY)){
sX = Math.floor((mouseX-600)/(400/NUM_LOX));
sY = Math.floor((mouseY-200)/(400/NUM_LOY));
//trace(" "+sX+sY);
//key++;
}
key++;
if(key == 2){
var sps:Sprite = new Sprite();
px = (spriteAry[fX][fY]).x;
py = (spriteAry[fX][fY]).y;
spriteAry[fX][fY].x = spriteAry[sX][sY].x;
spriteAry[fX][fY].y = spriteAry[sX][sY].y;
spriteAry[sX][sY].x = px;
spriteAry[sX][sY].y = py;
sps = spriteAry[fX][fY] as Sprite;
spriteAry[fX][fY] = spriteAry[sX][sY];
spriteAry[sX][sY] = sps;
key = 0;
if(CheckRight()){//拼图正确
correctFunc();
}
}
}
private var befTime:int = 0;
private function correctFunc():void{
szTextLv.text = "当前第 "+IsLv+" 关";
szTextLv.setTextFormat(szFormat);
szTextTime.text = "上一关用时:";
szTextTime.setTextFormat(szFormat);
befTime = Math.floor((getTimer()-beginTime)/1000);
beginTime = getTimer();
szTextNum.text = ""+befTime+"秒";
szTextNum.setTextFormat(szFormat);
NUM_LOX++;
NUM_LOY++;
IsLv++;
bitAryFunc(bit.bitmapData);
initCityRand();
}
private function CheckRight():Boolean{
var bool:Boolean = true;
for(var i:int = 0;i<NUM_LOX;i++){
for(var j:int = 0;j<NUM_LOY;j++){
var str:String = ""+i+j;
if(spriteAry[i][j].name != str){
bool = false;
break;
}
}
if(!bool)break;
}
return bool;
}
private var btn:SimpleButton;
private var intLv:int = 0;
private function initView():void{
pt = new Sprite();
loa = new Loader();
initText();
btn = new SimpleButton();
btn = makeButton("点击开始","PLAY",500,100);
//loa.load(new URLRequest("wallA.jpg"));
loa.contentLoaderInfo.addEventListener(Event.COMPLETE,loadPic);
loa.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onError);
//loa.addEventListener(Event.COMPLETE,loadPic);
addChild(pt);
pt.addChild(btn);
btn.addEventListener(MouseEvent.CLICK,onBtn);
szHow.text =
"玩法介绍\n" +
"(1) chick [开始] button\n" +
"(2) select a local pictures\n" +
"(3) chick [开始] button to play.. \n" +
"PS:the pictures must be 400x400";
szHow.setTextFormat(szFormatA);
}
private function onError(ev:IOErrorEvent):void{
urlLoad.load(new URLRequest(fileObj.name));
}
private var szTextLv:TextField;
private var szTextTime:TextField;
private var szTextNum:TextField;
private var szFormat:TextFormat;
private var szFormatA:TextFormat;
private var szHow:TextField;
private function initText():void{
szTextLv = new TextField();
szTextTime = new TextField();
szTextNum = new TextField();
szFormat = new TextFormat();
szFormat.font = "微软雅黑";
szFormat.color = 0xff1493;
szFormat.size = 25;
szFormatA = new TextFormat();
szFormatA.font = "微软雅黑";
szFormatA.color = 0x00D5FF;
szFormatA.size = 15;
szTextLv = RetText(650,100,150,40,0x228B22,0xFFFF00);
szTextTime = RetText(800,100,150,40,0x20B2AA,0xFFFF00);
szTextNum = RetText(950,100,80,40,0xbdb76b,0xFFFF00);
szHow = RetText(1050,200,300,400,0x8b8878,0xee82ee);
pt.addChild(szHow);
pt.addChild(szTextNum);
pt.addChild(szTextTime);
pt.addChild(szTextLv);
}
private function RetText(
x:int = 0,y:int = 0,
wd:int = 0,he:int = 0,
colorBack:int = 0,colorBord:int = 0):TextField{
var text:TextField = new TextField();
text.x = x;
text.y = y;
text.width = wd;
text.height = he;
text.background = true;
text.backgroundColor = colorBack;
text.border = true;
text.borderColor = colorBord;
return text;
}
private var beginTime:int = 0;
private var OpeFile:Boolean = false;
private var OpFileOne:Boolean = true;
private function onBtn(ev:MouseEvent):void{
if(OpFileOne){
onSongAdd();
OpFileOne = false;
}
if(OpeFile){
beginTime = getTimer();
szTextLv.text = "当前第 "+IsLv+" 关";
szTextLv.setTextFormat(szFormat);
IsLv++;
bitAryFunc(bit.bitmapData);
initCityRand();
btn.removeEventListener(MouseEvent.CLICK,onBtn);
}
}
private function makeButton(btnName:String,btnOvr:String,btnX:int = 0,btnY:int = 0):SimpleButton{//按钮生成
var BtnDown:Sprite = new Sprite();
var BtnOvr:Sprite = new Sprite();
var my_label:TextField = BtnDown.addChild (new TextField) as TextField;
my_label.defaultTextFormat = new TextFormat("微软雅黑",24,0x838b83);
my_label.text = btnName;
my_label.x = btnX;
my_label.y = btnY-1;
var my_labelOvr:TextField = BtnOvr.addChild (new TextField) as TextField;
my_labelOvr.defaultTextFormat = new TextFormat("微软雅黑",24,0x838b83);
my_labelOvr.text = btnOvr;
my_labelOvr.x = btnX;
my_labelOvr.y = btnY-1;
var BtnUp:Sprite = new Sprite();
BtnDown.graphics.beginFill(0x2f4f4f);
BtnDown.graphics.drawRoundRect(btnX,btnY,100,40,5,10);
BtnDown.graphics.endFill();
BtnOvr.graphics.beginFill(0x2f4f4f);
BtnOvr.graphics.drawRoundRect(btnX,btnY,100,40,5,10);
BtnOvr.graphics.endFill();
BtnUp.graphics.beginFill(0xD4D4D4);
BtnUp.graphics.drawRoundRect(btnX,btnY,100,40,5,10);
var btn:SimpleButton = new SimpleButton(BtnDown,BtnDown,BtnOvr,BtnUp);
return btn;
}
private var bit:Bitmap;
private function loadPic(ev:Event):void{
bit = Bitmap(loa.content);
bit.x = 100;
bit.y = 200;
bit.width = 400;
bit.height = 400;
pt.addChild(bit);
bitAryFunc(bit.bitmapData);
}
private function onSongAdd():void{
var fileFilt:FileFilter = new FileFilter("Pic(*.jpg)", "*.jpg");
var fileArr:Array = new Array();
fileArr.push(fileFilt);
fileObj.browse(fileArr);
fileObj.addEventListener(Event.CANCEL,onFileCancel);
fileObj.addEventListener(Event.SELECT,onSelect);
//songNameObj.push(_sound);
//SongList(songNameArr,songNameObj);
}
private var fileObj:FileReference = new FileReference();
private var urlLoad:URLLoader = new URLLoader();
private function onSelect(ev:Event):void{//开始上载文件
urlLoad.dataFormat = URLLoaderDataFormat.BINARY;
urlLoad.addEventListener(Event.COMPLETE,onURLLoad);
//loa.load(new URLRequest(fileObj.name));
urlLoad.load(new URLRequest(fileObj.name));
OpeFile = true;
}
private function onFileCancel(ev:Event):void{//取消文件
trace("CANCEL");
OpeFile = false;
}
private function onURLLoad(ev:Event):void{
var content:ByteArray = ev.target.data as ByteArray;
loa.loadBytes(content);
}
private var bitAry:Array;
private function bitAryFunc(bitmapData:BitmapData):void{
bitAry = new Array();
for(var i:int = 0;i<NUM_LOX;i++){
var iary:Array = new Array();
for(var j:int = 0;j<NUM_LOY;j++){
var rect:Rectangle = new Rectangle(i*400/NUM_LOX,j*400/NUM_LOY,400/NUM_LOX,400/NUM_LOY);
var points:Point = new Point(0,0);
var bmd:BitmapData = new BitmapData(400/NUM_LOX,400/NUM_LOY);
bmd.copyPixels(bitmapData,rect,points);
var bm:Bitmap = new Bitmap(bmd);
bm.width = 400/NUM_LOX,bm.height = 400/NUM_LOY;
iary.push(bm);
}
bitAry.push(iary);
}
initCity();
}
}
}
RectSprite.as:
package
{
import flash.display.Bitmap;
import flash.display.Sprite;
public class RectSprite extends Sprite
{
//private var _label:TextField;
public function RectSprite(bit:Bitmap,wd:int = 0,he:int = 0) {
//以下四行使用Sprite内的graphics对象画一个圆角矩形背景
this.graphics.lineStyle(2, 0x85DB18);
this.graphics.beginFill(0xCCCCCC);
this.graphics.drawRoundRect(0,0,wd,he,10,10);
this.graphics.endFill();
addChild(bit);
}
}
}
Down PinTu