flash on 2011-6-19
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.net.URLRequest;
import flash.system.LoaderContext;
public class Main extends Sprite
{
private const URL:Array =
[
"http://assets.wonderfl.net/images/related_images/6/63/635b/635bc1dc1a78a59a0d3ab226d6e8aa06ef9cd38e",
"http://assets.wonderfl.net/images/related_images/5/50/50a6/50a66896d7b51cb03bdc798ed67d4ef00073846b",
"http://assets.wonderfl.net/images/related_images/7/7c/7c37/7c37c5ad110cae8e8dedf3d299676a749d83cec8",
"http://assets.wonderfl.net/images/related_images/9/98/9853/9853b125712ba2a049d21208f59607e4a76dadda",
"http://assets.wonderfl.net/images/related_images/7/78/7842/784278d0b49d28243c957e1f0598721bf06daa94",
"http://assets.wonderfl.net/images/related_images/d/dc/dc3a/dc3a71bb214a99bde816b74d39e26591dab596d8"
];
public static const WIDTH:Number = 232.5;
public static const HEIGHT:Number = 232.5;
private var bg:BitmapData;
private var canvas:BitmapData;
public static var shadowCanvas:BitmapData;
private var loadCount:int = 0;
private var data:Array = [];
public function Main()
{
for (var i:int = 0; i < URL.length; i++)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
loader.load(new URLRequest(URL[i]), new LoaderContext(true));
loader.name = i.toString();
}
}
private function initHandler(event:Event):void
{
var loader:Loader = event.currentTarget.loader as Loader;
var bd:BitmapData = new BitmapData(loader.width, loader.height, true, 0x0);
bd.draw(loader);
data[int(loader.name)] = bd;
loadCount++;
if (loadCount == URL.length) initialize();
}
private function initialize():void
{
loadGraphics();
var bgSprite:Sprite = new Sprite();
bgSprite.graphics.beginBitmapFill(bg);
bgSprite.graphics.drawRect(0, 0, WIDTH, HEIGHT);
bgSprite.graphics.endFill();
bgSprite.scaleX = bgSprite.scaleY = 2;
addChild(bgSprite);
shadowCanvas = new BitmapData(WIDTH, HEIGHT, true, 0x0);
var shadowBitmap:Bitmap = new Bitmap(shadowCanvas);
shadowBitmap.scaleX = shadowBitmap.scaleY = 2;
addChild(shadowBitmap);
canvas = new BitmapData(WIDTH, HEIGHT, true, 0x0);
var bitmap:Bitmap = new Bitmap(canvas);
bitmap.scaleX = bitmap.scaleY = 2;
addChild(bitmap);
Block.initialize();
Ball.group.push(new Ball());
Paddle.group.push(new Paddle());
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function loadGraphics():void
{
Ball.image = data[0];
bg = data[1];
Block.image = data[2];
Paddle.image = data[3];
Block.shadowImage = data[4];
Block.imageText = data[5];
}
private function onEnterFrame(event:Event):void
{
update();
render();
}
private function update():void
{
for each (var ball:Ball in Ball.group)
{
ball.update();
}
for each (var paddle:Paddle in Paddle.group)
{
paddle.update(ball);
}
for each (var flash:Flash in Flash.group)
{
flash.update();
}
}
private function render():void
{
canvas.fillRect(canvas.rect, 0x0);
for each (var block:Block in Block.group)
{
canvas.copyPixels(block, block.rect, new Point(block.x, block.y));
}
for each (var ball:Ball in Ball.group)
{
canvas.copyPixels(ball, ball.rect, new Point(ball.x, ball.y), ball, new Point(), true);
}
for each (var paddle:Paddle in Paddle.group)
{
canvas.copyPixels(paddle, paddle.rect, new Point(paddle.x, paddle.y), paddle, new Point(), true);
}
}
}
}
import flash.display.BitmapData;
import flash.geom.Point;
import flash.geom.Rectangle;
class Block extends BitmapData
{
public static var image:BitmapData;
public static var imageText:BitmapData;
public static var shadowImage:BitmapData;
public static var group:Vector.<Block> = new Vector.<Block>();
public static var data:Vector.<Vector.<Block>> = new Vector.<Vector.<Block>>();
public static const WIDTH:int = 8;
public static const HEIGHT:int = 8;
public var x:int;
public var y:int;
private var _tileX:int;
private var _tileY:int;
private var _frame:int;
public function Block()
{
super(WIDTH, HEIGHT, true, 0x0);
frame = 0;
}
public static function search(x:int, y:int):Block
{
for each (var block:Block in group)
{
if (block.tileX == x && block.tileY == y)
{
return block;
}
}
return null;
}
public static function initialize():void
{
for (var y:int = 0; y < imageText.height; y++)
{
data[y] = new Vector.<Block>(imageText.height);
for (var x:int = 0; x < imageText.width; x++)
{
if (imageText.getPixel(x, y) != 0xFFFFFF)
{
var block:Block = new Block();
block.tileY = y + 2;
block.tileX = x + 2;
group.push(block);
var shadowY:int = block.y + HEIGHT, shadowX:int = block.x + WIDTH;
if (Main.HEIGHT <= shadowY || Main.WIDTH <= shadowX) continue;
Main.shadowCanvas.copyPixels(shadowImage, new Rectangle((shadowX / WIDTH % 4) * WIDTH, (shadowY / HEIGHT % 4) * HEIGHT, WIDTH, HEIGHT), new Point(shadowX, shadowY));
}
}
}
}
public function get frame():int
{
return _frame;
}
public function set frame(value:int):void
{
_frame = value;
copyPixels(image, new Rectangle(WIDTH * value, 0, WIDTH, HEIGHT), new Point());
}
public function get tileY():int
{
return _tileY;
}
public function set tileY(value:int):void
{
_tileY = value;
y = _tileY * HEIGHT;
}
public function get tileX():int
{
return _tileX;
}
public function set tileX(value:int):void
{
_tileX = value;
x = _tileX * WIDTH;
}
}
class Ball extends BitmapData
{
public static var image:BitmapData;
public static var group:Vector.<Ball> = new Vector.<Ball>();
public static const WIDTH:int = 5;
public static const HEIGHT:int = 4;
public var x:int = 130;
public var y:int = 130;
public var vx:int = 1;
public var vy:int = 1;
public function Ball()
{
super(image.width, image.height, true, 0x0);
draw(image);
}
public function update():void
{
var bx:int;
var by:int;
var block:Block;
for (var i:int = 0; i < 3; i++)
{
if (vx == 1)
{
bx = (x + WIDTH) / Block.WIDTH;
by = y / Block.HEIGHT;
block = Block.search(bx, by);
if (block)
{
vx = -1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
by = (y + HEIGHT - 1) / Block.HEIGHT;
block = Block.search(bx, by);
if (block)
{
vx = -1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
if (x >= Main.WIDTH) vx = -1;
}
else
{
bx = (x - 1) / Block.WIDTH;
by = y / Block.HEIGHT;
block = Block.search(bx, by);
if (block)
{
vx = 1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
by = (y + HEIGHT - 1) / Block.HEIGHT;
block = Block.search(bx, by);
if (block)
{
vx = 1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
if (x < 0) vx = 1;
}
x += vx;
if (vy == 1)
{
bx = x / Block.WIDTH;
by = (y + HEIGHT) / Block.HEIGHT;
block = Block.search(bx, by);
if (block)
{
vy = -1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
bx = (x + WIDTH - 1) / Block.WIDTH;
block = Block.search(bx, by);
if (block)
{
vy = -1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
if (y >= 200 - HEIGHT / 2) vy = -1;
}
else
{
bx = x / Block.WIDTH;
by = (y - 1) / Block.HEIGHT;
block = Block.search(bx, by);
if (block)
{
vy = 1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
bx = (x + WIDTH - 1) / Block.WIDTH;
block = Block.search(bx, by);
if (block)
{
vy = 1;
Flash.group.push(new Flash(block, [1, 2, 3, 4, 5, 0], 2));
}
if (y < 0) vy = 1;
}
y += vy;
}
}
}
class Flash
{
public var block:Block;
public var frameIndex:int = 0;
public var frames:Array = [];
public var delay:int = 0;
public var maxDelay:int = 0;
public static var group:Vector.<Flash> = new Vector.<Flash>();
public function Flash(block:Block, frames:Array, maxDelay:int)
{
this.block = block;
this.frames = frames;
this.maxDelay = maxDelay;
}
public function update():void
{
if (++delay == maxDelay)
{
delay = 0;
if (frameIndex + 1 < frames.length)
{
block.frame = frames[++frameIndex];
}
else
{
frameIndex = 0;
group.splice(group.indexOf(this), 1);
}
}
}
}
class Paddle extends BitmapData
{
public static var image:BitmapData;
public static var group:Vector.<Paddle> = new Vector.<Paddle>();
public static const WIDTH:int = 32;
public static const HEIGHT:int = 8;
public var x:Number = 0;
public var y:Number = 200;
public var angle:Number = 0.0;
public function Paddle()
{
super(image.width, image.height, true, 0x0);
draw(image);
}
public function update(ball:Ball):void
{
angle += 0.1;
x = (ball.x - ((y - ball.y) * Math.sin(angle) / 4)) - 13;
if (x < 0) x = 0;
if (Main.WIDTH - WIDTH <= x) x = Main.WIDTH - WIDTH;
}
}