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

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

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


forked from : k0rin's Super Express [diff(1)]

FORKED
  1. // forked from hacker_m__h35dj's forked from: Super Express
  2. // forked from k0rin's Super Express
  3. // It was a tricky task to scroll seamless mountains.
  4. // Click to see how it works.
  5. // 繋ぎ目のない山を無限スクロールさせるのにちょっと悩みました。
  6. // クリックでどうなってるのかネタバレします。
  7. // 架線柱のティアリングがひどいなあ……。
  8. package {
  9.     import flash.display.*;
  10.     import flash.events.*;
  11.     import flash.geom.*;
  12.     
  13.     [SWF(width = "465", height = "465", frameRate = "40")]
  14.     public class Main extends Sprite
  15.     {
  16.         public static const WIDTH:Number = 465;
  17.         public static const HEIGHT:Number = 465;
  18.         
  19.         private var debug:Boolean = false;
  20.         private var entities:Vector.<Entity> = new Vector.<Entity>();
  21.         public function Main():void
  22.         {
  23.             // 空を描画
  24.             var matrix:Matrix = new Matrix();
  25.             matrix.createGradientBox(WIDTH, HEIGHT, Math.PI / 2);
  26.             graphics.beginGradientFill(GradientType.LINEAR, [0xD5E1FB, 0xFFFFFF], null, [0128], matrix);
  27.             graphics.drawRect(00, WIDTH, HEIGHT);
  28.             graphics.endFill();
  29.             
  30.             var fogR:Number = 116;
  31.             var fogG:Number = 126;
  32.             var fogB:Number = 143;
  33.             
  34.             var mountainR:Number = 23;
  35.             var mountainG:Number = 21;
  36.             var mountainB:Number = 32;
  37.             
  38.             const NUMBER_OF_MOUNTAINS:int = 4;
  39.             
  40.             for (var i:int = 0; i < NUMBER_OF_MOUNTAINS; i++) {
  41.                 var blend:Number = i / (NUMBER_OF_MOUNTAINS - 1);
  42.                 
  43.                 var _r:Number = lerp(fogR, mountainR, blend);
  44.                 var _g:Number = lerp(fogG, mountainG, blend);
  45.                 var _b:Number = lerp(fogB, mountainB, blend);
  46.                 
  47.                 var baseHeight:Number = HEIGHT / 2 + i * 25;
  48.                 var color:uint = (_r << 16) | (_g << 8) | _b;
  49.                 
  50.                 var mountain:Mountain = new Mountain(-Math.pow(i + 12), baseHeight, color);
  51.                 entities.push(addChild(mountain));
  52.             }
  53.             
  54.             entities.push(addChild(new PoleAndWire()));
  55.             entities.push(addChild(new Tunnel()));
  56.             
  57.             var outline:Shape = new Shape();
  58.             var g:Graphics = outline.graphics;
  59.             g.lineStyle(1, 0x808080);
  60.             g.drawRect( -1, -1, WIDTH + 2, HEIGHT + 2);
  61.             addChild(outline);
  62.             
  63.             restoreFilters(debug);
  64.             
  65.             stage.addEventListener(MouseEvent.CLICK, clickHandler);
  66.             addEventListener(Event.ENTER_FRAME, enterFrameHandler);
  67.         }
  68.         
  69.         private function clickHandler(e:MouseEvent):void 
  70.         {
  71.             debug = !debug;
  72.             
  73.             var matrix:Matrix = new Matrix();
  74.             if (debug) {
  75.                 // transformで表示領域外を確認。お手軽でいいと思う。
  76.                 matrix.scale(0.20.2);
  77.                 matrix.translate(WIDTH * 0.4, HEIGHT * 0.4);
  78.             }
  79.             transform.matrix = matrix;
  80.             
  81.             restoreFilters(debug);
  82.         }
  83.         
  84.         private function restoreFilters(debug:Boolean):void
  85.         {
  86.             for each (var entity:Entity in entities)
  87.             {
  88.                 entity.restoreFilter(debug);
  89.             }
  90.         }
  91.         
  92.         private function enterFrameHandler(e:Event):void 
  93.         {
  94.             for each (var entity:Entity in entities)
  95.             {
  96.                 entity.update();
  97.             }
  98.         }
  99.     }
  100. }
  101. import flash.display.*;
  102. import flash.filters.BlurFilter;
  103. import flash.filters.GlowFilter;
  104. import flash.geom.Matrix;
  105. class Entity extends Sprite
  106. {
  107.     public function update():void { };
  108.     public function restoreFilter(debug:Boolean):void { };
  109. }
  110. class Mountain extends Entity
  111. {
  112.     private var heightMap:Vector.<Number> = new Vector.<Number>();
  113.     private const SEGMENT_LENGTH:Number = 10;
  114.     
  115.     private var baseHeight:Number;
  116.     private var color:uint;
  117.     private var speed:Number;
  118.     
  119.     function Mountain(speed:Number, baseHeight:Number, color:uint)
  120.     {
  121.         this.baseHeight = baseHeight;
  122.         this.color = color;
  123.         this.speed = speed;
  124.         
  125.         generateHeightMap();
  126.         createShape();
  127.     }
  128.     
  129.     public override function update():void
  130.     {
  131.         x += speed;
  132.         if (x < -(width - Main.WIDTH)) {
  133.             var removeSegmentNumber:int = (width - Main.WIDTH) / SEGMENT_LENGTH;
  134.             heightMap.splice(0, removeSegmentNumber);
  135.             x += removeSegmentNumber * SEGMENT_LENGTH;
  136.             
  137.             generateHeightMap();
  138.             createShape();
  139.         }
  140.     }
  141.     
  142.     private function generateHeightMap():void
  143.     {
  144.         // 再帰で分割していく
  145.         divide(baseHeight, baseHeight, 0200);
  146.         
  147.         function divide(left:Number, right:Number, depth:int, offset:Number):void
  148.         {
  149.             if (depth < 6) {
  150.                 var half:Number = (left + right) / 2 + rnd( -offset / 2, offset / 2);
  151.                 
  152.                 divide(left, half, depth + 1, offset / 2);
  153.                 divide(half, right, depth + 1, offset / 2);
  154.             } else {
  155.                 // 十分に分割したら順番に書き出し
  156.                 heightMap.push(left);
  157.             }
  158.         }
  159.     }
  160.         
  161.     private function createShape():void
  162.     {
  163.         var g:Graphics = graphics;
  164.         
  165.         g.clear();
  166.         g.beginFill(color);
  167.         g.moveTo(0, Main.HEIGHT);
  168.         for (var i:int = 0; i < heightMap.length; i++) {
  169.             g.lineTo(i * SEGMENT_LENGTH, heightMap[i]);
  170.         }
  171.         g.lineTo((i - 1) * SEGMENT_LENGTH, Main.HEIGHT);
  172.         g.endFill();
  173.         
  174.         // デバッグ表示
  175.         g.lineStyle(1, color);
  176.         g.moveTo(0, heightMap[0]);
  177.         g.lineTo(0, Main.HEIGHT * 2);
  178.     }
  179. }
  180. const SPEED:Number = 80;
  181. class PoleAndWire extends Entity
  182. {
  183.     private const SPACING:Number = Main.WIDTH * 5;
  184.     
  185.     private const POLE_THICK:Number = 40;
  186.     private const WIRE_TOP:Number = 20;
  187.     private const WIRE_BOTTOM:Number = 100;
  188.     
  189.     function PoleAndWire()
  190.     {
  191.         var g:Graphics = graphics;
  192.         
  193.         g.beginFill(0x333344);
  194.         g.drawRect(-POLE_THICK / 20, POLE_THICK, Main.HEIGHT);
  195.         g.endFill();
  196.         
  197.         g.lineStyle(1, 0x222233);
  198.         g.moveTo(POLE_THICK / 2, WIRE_TOP);
  199.         g.curveTo(SPACING / 2, WIRE_BOTTOM, SPACING - POLE_THICK, WIRE_TOP);
  200.         g.moveTo(-POLE_THICK / 2, WIRE_TOP);
  201.         g.curveTo(-SPACING / 2, WIRE_BOTTOM, -SPACING + POLE_THICK, WIRE_TOP);
  202.         
  203.         x = (SPACING + Main.WIDTH) / 2;
  204.     }
  205.     
  206.     public override function update():void
  207.     {
  208.         x -= SPEED;
  209.         if (x < (-SPACING + Main.WIDTH) / 2) {
  210.             x += SPACING;
  211.         }
  212.     }
  213.     
  214.     public override function restoreFilter(debug:Boolean):void
  215.     {
  216.         filters = debug ? null : [ new BlurFilter(8001) ];
  217.     }
  218. }
  219. class Tunnel extends Entity
  220. {
  221.     // |ENTRANCE|SPACE|LIGHT|SPACE|ENTRANCE|
  222.     // ^ origin
  223.     
  224.     private const LIGHT:Number = 100;
  225.     private const SPACE:Number = Main.WIDTH * 1.4;
  226.     private const ENTRANCE:Number = Main.WIDTH * 1.5;
  227.     private const WIDTH:Number = LIGHT + SPACE * 2 + ENTRANCE * 2;
  228.     
  229.     private const ENTRANCE_COLOR:uint = 0x888899;
  230.     private const DARKNESS_COLOR:uint = 0x0A0908;
  231.     private const LIGHT_COLOR:uint = 0xFFF0E0;
  232.     
  233.     private var lightCount:int;
  234.     private var light:Shape;
  235.     
  236.     function Tunnel()
  237.     {
  238.         var g:Graphics = graphics;
  239.         
  240.         var matrix:Matrix = new Matrix();
  241.         matrix.createGradientBox(ENTRANCE, Main.HEIGHT);
  242.         g.beginGradientFill(GradientType.LINEAR, [ENTRANCE_COLOR, DARKNESS_COLOR], null, [0255], matrix);
  243.         g.drawRect(00, ENTRANCE, Main.HEIGHT);
  244.         matrix.createGradientBox(ENTRANCE, Main.HEIGHT, 0, WIDTH - ENTRANCE, 0);
  245.         g.beginGradientFill(GradientType.LINEAR, [DARKNESS_COLOR, ENTRANCE_COLOR], null, [0255], matrix);
  246.         g.drawRect(WIDTH - ENTRANCE, 0, ENTRANCE, Main.HEIGHT);
  247.         g.endFill();
  248.         
  249.         g.beginFill(DARKNESS_COLOR);
  250.         g.drawRect(ENTRANCE, 0, LIGHT + SPACE * 2, Main.HEIGHT);
  251.         g.endFill();
  252.         
  253.         light = new Shape();
  254.         light.graphics.beginFill(LIGHT_COLOR);
  255.         light.graphics.drawRect(WIDTH / 2, Main.HEIGHT * 0.55, LIGHT, 20);
  256.         light.graphics.endFill();
  257.         addChild(light);
  258.         
  259.         prepareNextTunnel();
  260.         
  261.         // 最初のトンネルまでは定距離にする。
  262.         x = SPEED * 600;
  263.     }
  264.     
  265.     public override function update():void
  266.     {
  267.         x -= SPEED;
  268.         if (x < -(WIDTH - ENTRANCE - Main.WIDTH)) {
  269.             if (--lightCount >= 0) {
  270.                 // ライトをループ
  271.                 x += SPACE * 2 + LIGHT - Main.WIDTH;
  272.                 trace(length);
  273.             }
  274.         }
  275.         if (x < -WIDTH * 2) {
  276.             prepareNextTunnel();
  277.         }
  278.     }
  279.     
  280.     public override function restoreFilter(debug:Boolean):void
  281.     {
  282.         filters = debug ? null : [ new BlurFilter(8001) ];
  283.         light.filters = debug ? null : [ new GlowFilter(0xFF8000, 1505034) ];
  284.     }
  285.     
  286.     private function prepareNextTunnel():void
  287.     {
  288.         x = SPEED * rnd(3001500);
  289.         lightCount = rnd(660);
  290.     }
  291. }
  292. // 線形補間
  293. function lerp(n0:Number, n1:Number, p:Number):Number
  294. {
  295.     return n0 * (1 - p) + n1 * p;
  296. }
  297. // [min, max)の乱数を取得
  298. function rnd(min:Number, max:Number):Number
  299. {
  300.     return min + Math.random() * (max - min);
  301. //    return lerp(min, max, Math.random());
  302. }
noswf
Get Adobe Flash Player