クルクル回るアレ
/**
* Copyright wwweeeep ( http://wonderfl.net/user/wwweeeep )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/iGPp
*/
/*
マウスクリックでスタート
押しっぱなしで逆回転
*/
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
[SWF(width='500', height='500', backgroundColor='#306090', frameRate='30')]
public class Main extends Sprite
{
private var base:/*String*/Array = [
"wwwwwwwwwwwwwww",
"wSSoooowoooooow",
"wSSowoowowwwwow",
"wwwwwowwoowooow",
"wwoowowwwowowww",
"wooooowooowooow",
"wowwwwoowwwowow",
"woowwoowoooowow",
"wwoowoowoowowow",
"wwwoowowwoowwow",
"woowowooowoooow",
"woooowwwoowowww",
"wowwwwoowowoGGw",
"wooooooooowoGGw",
"wwwwwwwwwwwwwww",
];
public var map:Map = new Map(base);
public var player:Machine = new Machine(40, 4, 45);
public var isStart:Boolean = false;//開始フラグ
public var isGoal:Boolean = false;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
this.addChild(map);
this.addChild(player);
this.player.x = this.map.sp.x;
this.player.y = this.map.sp.y;
addEventListener(Event.ENTER_FRAME, Update);
stage.addEventListener(MouseEvent.CLICK, MouseListener);
stage.addEventListener(MouseEvent.MOUSE_DOWN, MouseListener);
stage.addEventListener(MouseEvent.MOUSE_UP, MouseListener);
}
//更新処理
private function Update(e:Event):void
{
//自機を回転
if (player.turnLeft) this.player.angle -= this.player.turnSp;
else this.player.angle += this.player.turnSp;
if (this.isStart) {
//自機を移動
this.player.Move(mouseX, mouseY);
//当たり判定
if (this.player.myHitTest(this.map.wall.bitmapData, new Point(0, 0))) {
//対壁
this.player.x = this.map.sp.x;
this.player.y = this.map.sp.y;
this.isStart = false;//死亡
}else if (this.player.myHitTest(this.map.goal.bitmapData, new Point(0, 0))) {
this.player.visible = false;
this.isGoal = true;//ゴール
}
}
}
private function MouseListener(e:MouseEvent):void
{
if (e.type == MouseEvent.CLICK) {
//ゲーム開始
if (this.isStart == false) this.isStart = true;
}
//回転方向
else if (e.type == MouseEvent.MOUSE_DOWN) this.player.turnLeft = true;
else if (e.type == MouseEvent.MOUSE_UP) this.player.turnLeft = false;
}
}
}
//◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
//◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
//◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Point;
//回る自機オブジェクト
class Machine extends Sprite
{
//自機
private var me:Shape;
public var speed:Number = 5;//移動速度
public var turnLeft:Boolean = false;//回転方向(左回りかどうか)
public var turnSp:Number = 2;//回転速度
private var size:Number;
private var part:uint = 1;
private var target:uint = 0;//判定対象
private var bmp:/*BitmapData*/Array = new Array();
//角度を更新
public function set angle(value:Number):void
{
//範囲調整
if (value < 0) value += 360;
if (value >= 360) value %= 360;
//オブジェクトの角度を更新
this.me.rotation = value;
//判定対象を更新
this.target = int(this.part * value / 360);
}
//角度を参照
public function get angle():Number
{
return this.me.rotation;
}
public function Machine(size:Number = 20, bold:Number = 5, part:uint = 45):void
{
if (part != 0) this.part = part;
else this.part = 45;
this.size = size;
var base:Shape = new Shape();
base.graphics.beginFill(0xFF0000);
base.graphics.drawRoundRect( -size/2, -bold/2, size, bold, bold/2, bold/2);
/*
base.graphics.moveTo( -size/2,-bold/2);
base.graphics.lineTo( -size/2+5,-bold/2 - 5);
base.graphics.lineTo( -size/2+10,-bold/2);
base.graphics.lineTo( -size/2,-bold/2);
*/
var i:int;
var q:Number;
var matrix:Matrix;
var bd:BitmapData;
for (i = 0; i < this.part; i++) {
bd = new BitmapData(size * 2, size * 2, true, 0x0);
q = i * 2 * Math.PI / part;
matrix = new Matrix(1, 0, 0, 1, 0, 0);
matrix.rotate(q);
matrix.tx = size / 2;
matrix.ty = size / 2;
bd.draw(base, matrix);
this.bmp.push(bd);
}
this.me = base;
this.addChild(me);
}
//当たり判定
public function myHitTest(secondObject:Object, secondBitmapDataPoint:Point = null, secondAlphaThreshold:uint = 1):Boolean
{
if (target >= part) target %= part;
if (this.bmp[target].hitTest(new Point(x - size/2,y-size/2), 0xFE, secondObject, secondBitmapDataPoint, secondAlphaThreshold)) {
return true;
}
return false;
}
//移動
public function Move(px:Number, py:Number):void
{
var dx:Number = px - x;
var dy:Number = py - y;
var r:Number = Math.sqrt(dx * dx + dy * dy);
if (r <= speed) {
this.x = px;
this.y = py;
}else {
this.x += speed * dx / r;
this.y += speed * dy / r;
}
}
}
//◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
//◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
//◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆◆
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Point;
import flash.geom.Rectangle;
//マップオブジェクト
class Map extends Sprite
{
//マップデータ
public var wall:Bitmap;
public var start:Bitmap;
public var goal:Bitmap;
//サイズ情報(横単位数,縦単位数,横単位幅,縦単位幅)
public var SIZE:Rectangle = new Rectangle(10, 10, 50, 50);
//開始位置
public var sp:Point;
public var sRect:Rectangle = new Rectangle();
public function Map(base:/*String*/Array, width:int = 500, height:int = 500):void
{
var i:int;
var j:int;
//サイズ設定
SIZE.y = base.length;
SIZE.x = base[0].length;
for (i = 1; i < SIZE.y; i++) {
if (SIZE.x > base[i].length) SIZE.x = base[i].length;
}
SIZE.width = Math.ceil(width / SIZE.x);
SIZE.height = Math.ceil(height / SIZE.y);
//空のBitmapDataオブジェクトを生成
var empty:BitmapData = new BitmapData(SIZE.x * SIZE.width, SIZE.y * SIZE.height, true, 0x0);
this.wall = new Bitmap(empty.clone());
this.start = new Bitmap(empty.clone());
this.goal = new Bitmap(empty.clone());
var char:String;
var rect:Rectangle = new Rectangle(0, 0, SIZE.width, SIZE.height);
//1マス分の出力内容
var wall:BitmapData = new BitmapData(SIZE.width, SIZE.height, true, 0xFF000000);
var start:BitmapData = new BitmapData(SIZE.width, SIZE.height, true, 0xEEAA5555);
var goal:BitmapData = new BitmapData(SIZE.width, SIZE.height, true, 0xEE55AA55);
for (i = 0; i < SIZE.y; i++) {
for (j = 0; j < SIZE.x; j++) {
char = base[i].charAt(j);
if (char == "w") {
this.wall.bitmapData.copyPixels(wall, rect, new Point(j * SIZE.width, i * SIZE.height));
}else if (char == "S") {
this.start.bitmapData.copyPixels(start, rect, new Point(j * SIZE.width, i * SIZE.height));
//開始位置を設定
this.SetStartPoint(j, i);
}else if (char == "G") {
this.goal.bitmapData.copyPixels(goal, rect, new Point(j * SIZE.width, i * SIZE.height));
}
}
}
this.addChild(this.wall);
this.addChild(this.start);
this.addChild(this.goal);
}
//開始位置の設定
private function SetStartPoint(x:int, y:int):void
{
var tmp:Rectangle = new Rectangle(x * SIZE.width, y * SIZE.height, (x + 1) * SIZE.width, (y + 1) * SIZE.height);
//始点設定
if (this.sp == null) {
this.sp = new Point();
this.sRect.x = tmp.x;
this.sRect.y = tmp.y;
}
if (this.sRect.x > tmp.x) this.sRect.x = tmp.x;
if (this.sRect.y > tmp.y) this.sRect.y = tmp.y;
if (this.sRect.width < tmp.width) this.sRect.width = tmp.width;
if (this.sRect.height < tmp.height) this.sRect.height = tmp.height;
this.sp.x = (this.sRect.width - this.sRect.x) / 2 + this.sRect.x;
this.sp.y = (this.sRect.height - this.sRect.y) / 2 + this.sRect.y;
}
}