※現在、「wonderfl build flash online」求人コンテンツ制作に関してのアンケートを実施中です!みなさまのお力添えを頂いて、続々とアンケート結果が集まっていますが、まだまだ募集しております。ご協力のほど、どうぞよろしくお願いいたします!

wonderfl運営事務局
→アンケートページ(※ログインしてからお答えいただけるようになっています。)

 notice: Flash editor updated! Join the development! Thanks to MiniBuilder


FORKED
  1. // forked from nemu90kWw's ダラ外宇宙水槽 for Wonderfl
  2. package
  3. {
  4.     import flash.display.*;
  5.     import flash.events.*;
  6.     import flash.net.*;
  7.     import flash.system.*;
  8.     
  9.     // カーソルキーで操作できます。
  10.     [SWF(width="465", height="465", frameRate="60")]
  11.     public class Darius extends Sprite
  12.     {
  13.         public static const WIDTH:int = 233;
  14.         public static const HEIGHT:int = 233;
  15.         
  16.         private var buffer:BitmapData;
  17.         private var screen:Bitmap;
  18.         
  19.         private var loader:Loader;
  20.         private var fish:Fish;
  21.         private var bg:BG;
  22.         
  23.         function Darius()
  24.         {
  25.             Wonderfl.capture_delay(3);
  26.             loader = new Loader();
  27.             
  28.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  29.             loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/2/2c/2c65/2c65f2686591075d582ac89a915d8819975e8155"), new LoaderContext(true));
  30.         }
  31.         
  32.         private function onLoadComplete(e:Event):void
  33.         {
  34.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
  35.             
  36.             buffer = new BitmapData(WIDTH, HEIGHT, false0);
  37.             screen = new Bitmap(buffer);
  38.             screen.scaleX = screen.scaleY = 2;
  39.             addChild(screen);
  40.             
  41.             fish = new Fish(Bitmap(loader.content).bitmapData);
  42.             bg = new BG();
  43.             
  44.             Key.setListener(stage);
  45.             addEventListener(Event.ENTER_FRAME, onEnterFrame);
  46.         }
  47.         
  48.         private function onEnterFrame(e:Event):void
  49.         {
  50.             buffer.fillRect(buffer.rect, 0x181818);
  51.             bg.draw(buffer);
  52.             
  53.             if(Key.isDown(40)) {fish.speed -= 0.05; fish.wait = 60*3;}
  54.             if(Key.isDown(38)) {fish.speed += 0.05; fish.wait = 60*3;}
  55.             if(Key.isDown(37)) {fish.head.dir += 4; fish.wait = 60*3;}
  56.             if(Key.isDown(39)) {fish.head.dir -= 4; fish.wait = 60*3;}
  57.             
  58.             fish.main();
  59.             fish.draw(buffer);
  60.         }
  61.     }
  62. }
  63.     import flash.display.*;
  64.     import flash.geom.*;
  65.     import flash.events.*;
  66.     
  67.     class Fish
  68.     {
  69.         private var parts:Vector.<Segment> = new Vector.<Segment>(7true);
  70.         private var dirlog:Vector.<int> = new Vector.<int>();
  71.         
  72.         public var head:Segment;
  73.         public var speed:Number = 4;
  74.         public var dest:Number = 128;
  75.         public var wait:int = 90;
  76.         
  77.         function Fish(bmp:BitmapData)
  78.         {
  79.             //反転画像の生成
  80.             var temp:BitmapData = new BitmapData(Segment.WIDTH*16, Segment.HEIGHT*7true0);
  81.             var matrix:Matrix = new Matrix();
  82.             temp.copyPixels(bmp, bmp.rect, new Point(00));
  83.             matrix.scale(-11);
  84.             matrix.translate(Segment.WIDTH*170);
  85.             temp.draw(bmp, matrix, nullnullnew Rectangle(Segment.WIDTH*90, Segment.WIDTH*7, Segment.HEIGHT*7));
  86.             
  87.             for(var i:int = 0; i < 7; i++)
  88.             {
  89.                 var segment:Segment = new Segment(temp, i);
  90.                 parts[i] = segment;
  91.             }
  92.             
  93.             //パーツ毎の距離
  94.             parts[0].size = 44;
  95.             parts[1].size = 32;
  96.             parts[2].size = 28;
  97.             parts[3].size = 22;
  98.             parts[4].size = 24;
  99.             parts[5].size = 24;
  100.             
  101.             //初期位置
  102.             head = parts[0];
  103.             head.x = Darius.WIDTH/2 + 80;
  104.             head.y = 8;
  105.             head.z = Segment.D;
  106.             head.dir = 128;
  107.             
  108.             for(i = 0; i < 7*3+5; i++) {
  109.                 dirlog.unshift(head.dir);
  110.             }
  111.         }
  112.         
  113.         public function main():void
  114.         {
  115.             head.x += MathEx.getVectorX(head.dir, speed);
  116.             head.z += MathEx.getVectorY(head.dir, speed);
  117.             
  118.             head.x = MathEx.limit(-Darius.WIDTH+80, head.x, Darius.WIDTH-80);
  119.             head.z = MathEx.limit(Segment.D, head.z, Segment.D+600);
  120.             
  121.             if(wait > 0) {
  122.                 wait--;
  123.             }
  124.             else {
  125.                 if(Math.random() < 0.03) {dest = Math.floor(Math.random() * 16) * 16;}
  126.                 
  127.                 if(MathEx.getBearing(head.dir, dest) < -2) {
  128.                     head.dir += 4;
  129.                 }
  130.                 else if(MathEx.getBearing(head.dir, dest) > 2) {
  131.                     head.dir -= 4;
  132.                 }
  133.             }
  134.             
  135.             if(head.dir >= 256) {head.dir -= 256;}
  136.             if(head.dir < 0) {head.dir += 256;}
  137.             
  138.             dirlog.pop();
  139.             dirlog.unshift(head.dir);
  140.             
  141.             //体節の処理
  142.             for(var i:int = 1; i < parts.length; i++)
  143.             {
  144.                 var segment:Segment = parts[i];
  145.                 var front:Segment = parts[i-1];
  146.                 
  147.                 segment.x = front.x + MathEx.getVectorX(front.dir, -front.size);
  148.                 segment.z = front.z + MathEx.getVectorY(front.dir, -front.size)+0.1;
  149.                 segment.y = front.y;
  150.                 segment.dir = dirlog[i*3+5];
  151.             }
  152.         }
  153.         
  154.         public function draw(screen:BitmapData):void
  155.         {
  156.             var temp:Vector.<Segment> = parts.slice();    //配列のコピー
  157.             temp.sort(function(x:Segment, y:Segment):Number {return x.z <= y.z ? 1 : -1;});    //Zソート
  158.             
  159.             for(var i:int = 0; i < temp.length; i++) {
  160.                 temp[i].draw(screen);
  161.             }
  162.         }
  163.     }
  164.     
  165.     class Segment
  166.     {
  167.         private var frames:Vector.<BitmapData> = new Vector.<BitmapData>(16true);
  168.         private var matrix:Matrix = new Matrix();
  169.         
  170.         public static const WIDTH:int = 128;
  171.         public static const HEIGHT:int = 160;
  172.         public static const D:int = 400;
  173.         
  174.         private var cx:int = WIDTH/2;
  175.         private var cy:int = HEIGHT/2 + 4;
  176.         
  177.         public var x:Number = 0;
  178.         public var y:Number = 0;
  179.         public var z:Number = 0;
  180.         public var dir:int = 0;
  181.         public var size:int = 30;
  182.         
  183.         function Segment(bmp:BitmapData, num:int)
  184.         {
  185.             for(var i:int = 0; i < 16; i++)
  186.             {
  187.                 var frame:BitmapData = new BitmapData(WIDTH, HEIGHT, true0);
  188.                 frame.copyPixels(bmp, new Rectangle(i * WIDTH, num * HEIGHT, WIDTH, HEIGHT), new Point(00));
  189.                 frames[i] = frame;
  190.             }
  191.         }
  192.         
  193.         public function draw(screen:BitmapData):void
  194.         {
  195.             matrix.identity();
  196.             matrix.translate(-cx, -cy);
  197.             matrix.scale(D / z, D / z);
  198.             matrix.translate((D * x / z) + Darius.WIDTH/2, (D * y / z) + Darius.HEIGHT/2);
  199.             screen.draw(frames[Math.floor((dir+8) / 16+12) % 16], matrix);
  200.         }
  201.     }
  202.     
  203.     class BG
  204.     {
  205.         private var list:Vector.<Star> = new Vector.<Star>(400true)
  206.         
  207.         function BG()
  208.         {
  209.             for(var i:int = 0; i < list.length; i++) {
  210.                 list[i] = new Star();
  211.             }
  212.         }
  213.         
  214.         public function draw(screen:BitmapData):void
  215.         {
  216.             for each(var star:Star in list) {
  217.                 star.draw(screen);
  218.             }
  219.         }
  220.     }
  221.     
  222.     class Star
  223.     {
  224.         public var x:Number;
  225.         public var y:Number;
  226.         public var speed:Number;
  227.         public var color:uint;
  228.         
  229.         function Star()
  230.         {
  231.             x = Math.random()*Darius.WIDTH;
  232.             shuffle();
  233.         }
  234.         
  235.         public function shuffle():void
  236.         {
  237.             y = Math.random()*Darius.HEIGHT;
  238.             speed = Math.random()+0.1;
  239.             color = MathEx.rand_int(0, 0xC0)*0x10000 + MathEx.rand_int(0, 0xC0)*0x100 + MathEx.rand_int(0, 0xC0);
  240.         }
  241.         
  242.         public function draw(screen:BitmapData):void
  243.         {
  244.             x -= speed;
  245.             if(x < 0) {
  246.                 x = Darius.WIDTH;
  247.                 shuffle();
  248.             }
  249.             screen.setPixel(x, y, color);
  250.         }
  251.     }
  252.     
  253.     class MathEx
  254.     {
  255.         public static function limit(min:Number, target:Number, max:Number):Number {return Math.max(min, Math.min(target, max));}
  256.         public static function rand_int(min:int, max:int):int {return Math.floor(Math.random()*(max-min+1)+min);}
  257.         public static function getVectorX(dir:Number, speed:Number):Number {return Math.cos(Math.PI/128*dir)*speed;}
  258.         public static function getVectorY(dir:Number, speed:Number):Number {return Math.sin(Math.PI/128*dir)*speed;}
  259.         
  260.         public static function getBearing(dir1:Number, dir2:Number):Number
  261.         {
  262.             var d1:Number = normalizeAngle(dir1);
  263.             var d2:Number = normalizeAngle(dir2);
  264.             
  265.             if(d2-128 > d1) {d1 += 256;}
  266.             if(d2+128 < d1) {d1 -= 256;}
  267.             
  268.             return d1-d2;
  269.         }
  270.         
  271.         public static function normalizeAngle(dir:Number):Number
  272.         {
  273.             if (dir < -128) {
  274.                 dir = 256 - (-dir % 256);
  275.             }
  276.             dir = (dir + 128) % 256 - 128;
  277.             return dir;
  278.         }
  279.     }
  280.     
  281.     class Key
  282.     {
  283.         private static var down:Vector.<Boolean> = new Vector.<Boolean>(256true);
  284.         
  285.         public static function setListener(target:InteractiveObject):void
  286.         {
  287.             target.stage.focus = target;
  288.             
  289.             target.addEventListener(KeyboardEvent.KEY_DOWN, function (event:KeyboardEvent):void {down[event.keyCode] = true;});
  290.             target.addEventListener(KeyboardEvent.KEY_UP, function (event:KeyboardEvent):void {down[event.keyCode] = false;});
  291.             target.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
  292.         }
  293.         
  294.         private static function onFocusOut(event:FocusEvent):void
  295.         {
  296.             event.currentTarget.stage.focus = event.currentTarget;
  297.             
  298.             //キーを押しながらフォーカスが外れると押しっぱなしになる現象の対策
  299.             for(var i:int = 0; i < down.length; i++) {down[i] = false;}
  300.         }
  301.         
  302.         public static function isDown(keycode:int):Boolean {
  303.             return down[keycode];
  304.         }
  305.     }
noswf
  1. // forked from nemu90kWw's ダラ外宇宙水槽 for Wonderfl
  2. package
  3. {
  4.     import flash.display.*;
  5.     import flash.events.*;
  6.     import flash.net.*;
  7.     import flash.system.*;
  8.     import net.hires.debug.Stats;
  9.     
  10.     // カーソルキーで操作できます。
  11.     [SWF(width="465", height="465", frameRate="60")]
  12.     public class Darius extends Sprite
  13.     {
  14.         private const NUM:int = 24;
  15.         
  16.         public static const WIDTH:int = 233;
  17.         public static const HEIGHT:int = 233;
  18.         
  19.         private var buffer:BitmapData;
  20.         private var screen:Bitmap;
  21.         
  22.         private var loader:Loader;
  23.         private var fishes:Vector.<Fish> = new Vector.<Fish>();
  24.         private var bg:BG;
  25.         
  26.         public static var pool:Array = new Array();
  27.         
  28.         function Darius()
  29.         {
  30.             Wonderfl.capture_delay(10);
  31.             loader = new Loader();
  32.             
  33.             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
  34.             loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/2/2c/2c65/2c65f2686591075d582ac89a915d8819975e8155"), new LoaderContext(true));
  35.         }
  36.         
  37.         private function onLoadComplete(e:Event):void
  38.         {
  39.             loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onLoadComplete);
  40.             Fish.setup(Bitmap(loader.content).bitmapData);
  41.             stage.quality = StageQuality.LOW;
  42.             
  43.             buffer = new BitmapData(WIDTH, HEIGHT, false0);
  44.             screen = new Bitmap(buffer);
  45.             screen.scaleX = screen.scaleY = 2;
  46.             addChild(screen);
  47.             
  48.             for(var i:int = 0; i < NUM; i++) {
  49.                 fishes.push(new Fish());
  50.             }
  51.             bg = new BG();
  52.             
  53.             addChild(new Stats());
  54.             
  55.             Key.setListener(stage);
  56.             addEventListener(Event.ENTER_FRAME, onEnterFrame);
  57.         }
  58.         
  59.         private function onEnterFrame(e:Event):void
  60.         {
  61.             buffer.fillRect(buffer.rect, 0x181818);
  62.             bg.draw(buffer);
  63.             for each(var fish:Fish in fishes)
  64.             {
  65.                 if(Key.isDown(40)) {fish.speed -= 0.05; fish.wait = 60*3;}
  66.                 if(Key.isDown(38)) {fish.speed += 0.05; fish.wait = 60*3;}
  67.                 if(Key.isDown(37)) {fish.head.dir += 4; fish.wait = 60*3;}
  68.                 if(Key.isDown(39)) {fish.head.dir -= 4; fish.wait = 60*3;}
  69.                 
  70.                 fish.main();
  71.                 //fish.draw(buffer);
  72.             }
  73.             pool.sortOn("z"Array.NUMERIC | Array.DESCENDING);
  74.             for(var i:int = 0; i < pool.length; i++) {
  75.                 pool[i].draw(buffer);
  76.             }
  77.         }
  78.     }
  79. }
  80.     import flash.display.*;
  81.     import flash.geom.*;
  82.     import flash.events.*;
  83.     
  84.     class Fish
  85.     {
  86.         private static var grp:BitmapData;
  87.         private var parts:Vector.<Segment> = new Vector.<Segment>(7true);
  88.         private var dirlog:Vector.<int> = new Vector.<int>();
  89.         
  90.         public var head:Segment;
  91.         public var speed:Number = 4;
  92.         public var dest:Number = 128;
  93.         public var wait:int = 0;
  94.         
  95.         public static function setup(bmp:BitmapData):void
  96.         {
  97.             //反転画像の生成
  98.             grp = new BitmapData(Segment.WIDTH*16, Segment.HEIGHT*7true0);
  99.             var matrix:Matrix = new Matrix();
  100.             grp.copyPixels(bmp, bmp.rect, new Point(00));
  101.             matrix.scale(-11);
  102.             matrix.translate(Segment.WIDTH*170);
  103.             grp.draw(bmp, matrix, nullnullnew Rectangle(Segment.WIDTH*90, Segment.WIDTH*7, Segment.HEIGHT*7));
  104.         }
  105.         
  106.         function Fish()
  107.         {
  108.             for(var i:int = 0; i < 7; i++)
  109.             {
  110.                 var segment:Segment = new Segment(grp, i);
  111.                 parts[i] = segment;
  112.                 Darius.pool.push(parts[i]);
  113.             }
  114.             
  115.             //パーツ毎の距離
  116.             parts[0].size = 44;
  117.             parts[1].size = 32;
  118.             parts[2].size = 28;
  119.             parts[3].size = 22;
  120.             parts[4].size = 24;
  121.             parts[5].size = 24;
  122.             
  123.             //初期位置
  124.             head = parts[0];
  125.             head.x = 0;
  126.             head.y = -150 + Math.random()*300;
  127.             head.z = Segment.D + Math.random()*600;
  128.             dest = Math.floor(Math.random() * 16) * 16;
  129.             head.dir = dest;
  130.             
  131.             for(i = 0; i < 7*3+5; i++) {
  132.                 dirlog.unshift(head.dir);
  133.             }
  134.         }
  135.         
  136.         public function main():void
  137.         {
  138.             head.x += MathEx.getVectorX(head.dir, speed);
  139.             head.z += MathEx.getVectorY(head.dir, speed);
  140.             
  141.             head.x = MathEx.limit(-Darius.WIDTH+80, head.x, Darius.WIDTH-80);
  142.             head.z = MathEx.limit(Segment.D, head.z, Segment.D+600);
  143.             
  144.             if(wait > 0) {
  145.                 wait--;
  146.             }
  147.             else {
  148.                 if(Math.random() < 0.03) {dest = Math.floor(Math.random() * 16) * 16;}
  149.                 
  150.                 if(MathEx.getBearing(head.dir, dest) < -2) {
  151.                     head.dir += 4;
  152.                 }
  153.                 else if(MathEx.getBearing(head.dir, dest) > 2) {
  154.                     head.dir -= 4;
  155.                 }
  156.             }
  157.             
  158.             if(head.dir >= 256) {head.dir -= 256;}
  159.             if(head.dir < 0) {head.dir += 256;}
  160.             
  161.             dirlog.pop();
  162.             dirlog.unshift(head.dir);
  163.             
  164.             //体節の処理
  165.             for(var i:int = 1; i < parts.length; i++)
  166.             {
  167.                 var segment:Segment = parts[i];
  168.                 var front:Segment = parts[i-1];
  169.                 
  170.                 segment.x = front.x + MathEx.getVectorX(front.dir, -front.size);
  171.                 segment.z = front.z + MathEx.getVectorY(front.dir, -front.size)+0.1;
  172.                 segment.y = front.y;
  173.                 segment.dir = dirlog[i*3+5];
  174.             }
  175.         }
  176.         /*
  177.         public function draw(screen:BitmapData):void
  178.         {
  179.             var temp:Vector.<Segment> = parts.slice();    //配列のコピー
  180.             temp.sort(function(x:Segment, y:Segment):Number {return x.z <= y.z ? 1 : -1;});    //Zソート
  181.             
  182.             for(var i:int = 0; i < temp.length; i++) {
  183.                 temp[i].draw(screen);
  184.             }
  185.         }
  186.         */
  187.     }
  188.     
  189.     class Segment
  190.     {
  191.         private static var frames:Vector.<Vector.<DisplayImage>>;
  192.         private var matrix:Matrix = new Matrix();
  193.         
  194.         public static const WIDTH:int = 128;
  195.         public static const HEIGHT:int = 160;
  196.         public static const D:int = 400;
  197.         
  198.         private var num:int;
  199.         public var x:Number = 0;
  200.         public var y:Number = 0;
  201.         public var z:Number = 0;
  202.         public var dir:int = 0;
  203.         public var size:int = 30;
  204.         
  205.         function Segment(bmp:BitmapData, num:int)
  206.         {
  207.             if(frames == null)
  208.             {
  209.                 frames = new Vector.<Vector.<DisplayImage>>(7true);
  210.                 for(var j:int = 0; j < frames.length; j++)
  211.                 {
  212.                     frames[j] = new Vector.<DisplayImage>(16true);
  213.                     for(var i:int = 0; i < frames[j].length; i++)
  214.                     {
  215.                         var frame:BitmapData = new BitmapData(WIDTH, HEIGHT, true0);
  216.                         frame.copyPixels(bmp, new Rectangle(i * WIDTH, j * HEIGHT, WIDTH, HEIGHT), new Point(00));
  217.                         frames[j][i] = new DisplayImage(frame, WIDTH/2, HEIGHT/2 + 4);
  218.                     }
  219.                 }
  220.             }
  221.             
  222.             this.num = num;
  223.         }
  224.         
  225.         public function draw(screen:BitmapData):void
  226.         {
  227.             var frame:DisplayImage = frames[num][Math.floor((dir+8) / 16+12) % 16];
  228.             matrix.identity();
  229.             matrix.translate(-frame.cx, -frame.cy);
  230.             matrix.scale(D / z, D / z);
  231.             matrix.translate((D * x / z) + Darius.WIDTH/2, (D * y / z) + Darius.HEIGHT/2);
  232.             screen.draw(frame.bmp, matrix);
  233.         }
  234.     }
  235.     
  236.     class BG
  237.     {
  238.         private var list:Vector.<Star> = new Vector.<Star>(400true)
  239.         
  240.         function BG()
  241.         {
  242.             for(var i:int = 0; i < list.length; i++) {
  243.                 list[i] = new Star();
  244.             }
  245.         }
  246.         
  247.         public function draw(screen:BitmapData):void
  248.         {
  249.             for each(var star:Star in list) {
  250.                 star.draw(screen);
  251.             }
  252.         }
  253.     }
  254.     
  255.     class Star
  256.     {
  257.         public var x:Number;
  258.         public var y:Number;
  259.         public var speed:Number;
  260.         public var color:uint;
  261.         
  262.         function Star()
  263.         {
  264.             x = Math.random()*Darius.WIDTH;
  265.             shuffle();
  266.         }
  267.         
  268.         public function shuffle():void
  269.         {
  270.             y = Math.random()*Darius.HEIGHT;
  271.             speed = Math.random()+0.1;
  272.             color = MathEx.rand_int(0, 0xC0)*0x10000 + MathEx.rand_int(0, 0xC0)*0x100 + MathEx.rand_int(0, 0xC0);
  273.         }
  274.         
  275.         public function draw(screen:BitmapData):void
  276.         {
  277.             x -= speed;
  278.             if(x < 0) {
  279.                 x = Darius.WIDTH;
  280.                 shuffle();
  281.             }
  282.             screen.setPixel(x, y, color);
  283.         }
  284.     }
  285.     
  286.     class DisplayImage
  287.     {
  288.         public var bmp:BitmapData;
  289.         public var rect:Rectangle;
  290.         public var cx:int, cy:int;
  291.         
  292.         function DisplayImage(bmp:BitmapData, cx:int, cy:int)
  293.         {
  294.             this.bmp = bmp;
  295.             this.rect = bmp.rect;
  296.             this.cx = cx;
  297.             this.cy = cy;
  298.             trimming();
  299.         }
  300.         
  301.         private function trimming():void
  302.         {
  303.             var rect:Rectangle = bmp.getColorBoundsRect(0xFF000000, 0x00000000);
  304.             var temp:BitmapData = new BitmapData(rect.width, rect.height, true, 0x00000000);
  305.             cx -= rect.x;
  306.             cy -= rect.y;
  307.             temp.copyPixels(bmp, rect, new Point(00));
  308.             bmp = temp;
  309.         }
  310.     }
  311.     
  312.     class MathEx
  313.     {
  314.         public static function limit(min:Number, target:Number, max:Number):Number {return Math.max(min, Math.min(target, max));}
  315.         public static function rand_int(min:int, max:int):int {return Math.floor(Math.random()*(max-min+1)+min);}
  316.         public static function getVectorX(dir:Number, speed:Number):Number {return Math.cos(Math.PI/128*dir)*speed;}
  317.         public static function getVectorY(dir:Number, speed:Number):Number {return Math.sin(Math.PI/128*dir)*speed;}
  318.         
  319.         public static function getBearing(dir1:Number, dir2:Number):Number
  320.         {
  321.             var d1:Number = normalizeAngle(dir1);
  322.             var d2:Number = normalizeAngle(dir2);
  323.             
  324.             if(d2-128 > d1) {d1 += 256;}
  325.             if(d2+128 < d1) {d1 -= 256;}
  326.             
  327.             return d1-d2;
  328.         }
  329.         
  330.         public static function normalizeAngle(dir:Number):Number
  331.         {
  332.             if (dir < -128) {
  333.                 dir = 256 - (-dir % 256);
  334.             }
  335.             dir = (dir + 128) % 256 - 128;
  336.             return dir;
  337.         }
  338.     }
  339.     
  340.     class Key
  341.     {
  342.         private static var down:Vector.<Boolean> = new Vector.<Boolean>(256true);
  343.         
  344.         public static function setListener(target:InteractiveObject):void
  345.         {
  346.             target.stage.focus = target;
  347.             
  348.             target.addEventListener(KeyboardEvent.KEY_DOWN, function (event:KeyboardEvent):void {down[event.keyCode] = true;});
  349.             target.addEventListener(KeyboardEvent.KEY_UP, function (event:KeyboardEvent):void {down[event.keyCode] = false;});
  350.             target.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut);
  351.         }
  352.         
  353.         private static function onFocusOut(event:FocusEvent):void
  354.         {
  355.             event.currentTarget.stage.focus = event.currentTarget;
  356.             
  357.             //キーを押しながらフォーカスが外れると押しっぱなしになる現象の対策
  358.             for(var i:int = 0; i < down.length; i++) {down[i] = false;}
  359.         }
  360.         
  361.         public static function isDown(keycode:int):Boolean {
  362.             return down[keycode];
  363.         }
  364.     }
noswf
Get Adobe Flash Player