breaktime
クリックで壁を壊す
/**
* Copyright iong ( http://wonderfl.net/user/iong )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/h0Af
*/
/*
* クリックで壁を壊す
*/
package{
import flash.display.Sprite;
public class FlashTest extends Sprite
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.geom.Point;
import flash.geom.Matrix;
import flash.geom.Transform;
import flash.geom.ColorTransform;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.StageQuality;
private var _SPB:Sprite = new Sprite();
private var _SPR:Sprite = new Sprite();
private var _SPF:Sprite = new Sprite();
private var cv:BitmapData = new BitmapData(465, 465, true, 0x0);
private var cvMat:Matrix = new Matrix();
private var target:Sprite;
private var bc:uint = 0;
private var iBegin:uint = 0;
// 分割数
private var iSP:uint = 15;
// 画面幅・高さ
private var _W:uint = 155;
private var _H:uint = 155;
private var aryShake:Array;
// 動作判定
private var isAct:Boolean = false;
public function FlashTest():void
{
stage.quality = StageQuality.LOW;
cvMat.scale(3,3);
addChild(new Bitmap(cv));
with(_SPB.graphics){
beginFill(0x000000, 1);
moveTo(0,0);
lineTo(_W,0);
lineTo(_W,_H);
lineTo(0,_H);
endFill();
}
init();
addEventListener(Event.ENTER_FRAME, act);
stage.addEventListener(MouseEvent.CLICK, onC);
}
private function init():void
{
isAct = false;
iBegin= 0;
var bp:Sprite = new MakeBlock(_W, _H);
_SPR.addChild(bp);
bp.addEventListener(Event.ENTER_FRAME, begin);
target = bp;
}
private function begin(e:Event):void
{
e.target.y = Math.min(0, e.target.y+iBegin);
iBegin++;
if(e.target.y==0){
shake();
e.target.removeEventListener(Event.ENTER_FRAME, begin);
isAct = true;
}
}
private function act(e:Event):void
{
cv.lock();
cv.draw(_SPB, cvMat);
cv.draw(_SPR, cvMat);
cv.draw(_SPF, cvMat);
cv.unlock();
}
private function onC(e:MouseEvent):void
{
if(isAct){
shake();
breakBlock(target);
}
}
/* 壁を壊す */
private function breakBlock(bm:Sprite):void
{
var blockR:Sprite = new Sprite();
blockR.name = "bb"+bc;
bc++;
var spW:uint= Math.ceil(bm.width/iSP);
var spH:uint= Math.ceil(bm.height/iSP);
var iT:Transform = new Transform(bm);
for(var i:int=0; i<iSP; i++){
for(var k:int=0; k<iSP; k++){
var tB:BitmapData = new BitmapData(spW, spH, true, 0x0);
var m:Matrix = new Matrix();
m.translate(-spW*i, -spH*k);
tB.draw(bm,m);
var bp:Sprite = new BreakPiece(new Bitmap(tB), spW*i, spH*k);
bp.transform.colorTransform = new ColorTransform(1,1,1,1,
iT.colorTransform.redOffset
,iT.colorTransform.greenOffset
,iT.colorTransform.blueOffset, 0);
blockR.addChild(bp);
}
}
blockR.addEventListener(Event.REMOVED, rmBB);
_SPR.removeChild(target);
init();
_SPF.addChild(blockR);
}
private function rmBB(e:Event):void
{
if(e.currentTarget.numChildren<=1){
e.currentTarget.removeEventListener(Event.REMOVED, rmBB);
_SPF.removeChild(DisplayObject(e.currentTarget));
}
}
/* 画面を揺らす */
private function shake():void
{
_SPR.removeEventListener(Event.ENTER_FRAME, shakeAct);
cvMat.tx=0;
cvMat.ty=0;
aryShake= new Array(15,-15, 8,-8, 5,-5,3,-3,2,-2,1,-1,1,-1,1,-1);
_SPR.addEventListener(Event.ENTER_FRAME, shakeAct);
}
private function shakeAct(e:Event):void
{
if(aryShake.length>0){
var sp:Number = aryShake.shift();
cvMat.translate(sp,sp);
}else{
cvMat.translate(0,0);
e.target.removeEventListener(Event.ENTER_FRAME, shakeAct);
}
}
}
}
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.display.Graphics;
import flash.events.Event;
import flash.geom.Matrix;
import flash.geom.ColorTransform;
class MakeBlock extends Sprite
{
private var aryRand:Array= new Array();
public function MakeBlock(w:uint, h:uint):void
{
aryRand = init();
var iR:uint = Math.round(Math.random()*(aryRand.length-1));
addChild(make(aryRand[iR] as Array, w, h));
y -= h;
}
private function init():Array
{
var retVal:Array = new Array();
retVal[0] = new Array();
retVal[0][0]= String("12334100000");
retVal[0][0]= retVal[0][0].split("\n");
retVal[0][1]= 0xff111111;
retVal[0][2]= 0xff333333;
retVal[0][3]= 0xff666666;
retVal[0][4]= 0xfff0f0f0;
retVal[1] = new Array();
retVal[1][0]= String("1"
+"\n"+"1"
+"\n"+"1"
+"\n"+"1"
+"\n"+"2"
+"\n"+"3");
retVal[1][0]= retVal[1][0].split("\n");
retVal[1][1]= 0xff999999;
retVal[1][2]= 0xffaaaaaa;
retVal[1][3]= 0xffdddddd;
retVal[2] = new Array();
retVal[2][0]= String("222212222222212222"
+"\n"+"222212222222212222"
+"\n"+"222212222222212222"
+"\n"+"111111111111111111"
+"\n"+"122222222122222222"
+"\n"+"122222222122222222"
+"\n"+"122222222122222222"
+"\n"+"111111111111111111");
retVal[2][0]= retVal[2][0].split("\n");
retVal[2][1]= 0xff333333;
retVal[2][2]= 0xff999999;
return retVal;
}
private function setColor():void
{
var iR:uint = Math.round(Math.random()*60);
var iG:uint = Math.round(Math.random()*60);
var iB:uint = Math.round(Math.random()*60);
this.transform.colorTransform = new ColorTransform(1,1,1,1, iR, iG, iB, 0);
}
private function make(ary:Array, w:uint, h:uint):Sprite
{
var tBD:BitmapData = new BitmapData(ary[0][0].length, ary[0].length, true, 0x0);
var iC:uint;
for(var bpi:int=0; bpi<ary[0].length; bpi++){
for(var bpm:int=0; bpm<ary[0][bpi].length; bpm++){
if(ary[0][bpi].charAt(bpm)!="0"){
iC = int(ary[0][bpi].charAt(bpm));
tBD.setPixel32(bpm, bpi, ary[iC]);
}
}
}
var retVal:Sprite = new Sprite();
retVal.graphics.beginBitmapFill(tBD, new Matrix(), true, true);
retVal.graphics.moveTo(0,0);
retVal.graphics.lineTo(w,0);
retVal.graphics.lineTo(w,h);
retVal.graphics.lineTo(0,h);
retVal.graphics.endFill();
setColor();
return retVal;
}
}
class BreakPiece extends Sprite
{
private var spX:int;
private var spY:int;
private var addY:uint = 1;
function BreakPiece(bm:Bitmap, spW:uint, spH:uint):void
{
spX = -(Math.round(Math.random()*10)) + (Math.round(Math.random()*20));
spY = -(Math.round(Math.random()*20)) + (Math.round(Math.random()*14));
bm.x = spW;
bm.y = spH;
addChild(bm);
addEventListener(Event.ENTER_FRAME, act);
}
private function act(e:Event):void
{
x += spX;
y += spY;
y += addY;
addY++;
if(y >=133+this.height){
removeEventListener(Event.ENTER_FRAME, act);
e.target.parent.removeChild(this);
}
}
}