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

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

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


forked from : kkeisuke's beginBitmapFill で背景を無限スクロール [diff(29)]

FORKED

forked from: beginBitmapFill で背景を無限スクロール(dispose) forked from: forked from: beginBitmapFill で背景を無限スクロール(dispose) [diff(1)]

  1. // forked from kkeisuke's forked from: beginBitmapFill で背景を無限スクロール(dispose)
  2. // forked from kkeisuke's beginBitmapFill で背景を無限スクロール
  3. package  
  4. {
  5.     import flash.display.BitmapData;
  6.     import flash.display.Shape;
  7.     import flash.display.Sprite;
  8.     import flash.display.StageAlign;
  9.     import flash.display.StageScaleMode;
  10.     import flash.events.Event;
  11.     import flash.geom.Matrix;
  12.     import net.hires.debug.Stats;
  13.     
  14.     /** 
  15.     * beginBitmapFill のテストです。
  16.     * 背景を無限スクロールさせると、メモリはどのような状態になるのでしょう?
  17.     * 
  18.     * アドバイスを頂きました!ありがとうございます!!
  19.     * dispose() を入れたら、自宅の環境だと 5M も減りました!!
  20.     * 開放される頻度も上がったように感じます!!
  21.     */
  22.     [SWF(backgroundColor = 0xffffff, frameRate = 40, width = 465, height = 465)]
  23.     public class TileLoopScroll extends Sprite
  24.     {
  25.         private var bd:BitmapData;
  26.         private var bdw:int;
  27.         private var bdh:int;
  28.         private var sw:Number;
  29.         private var sh:Number;
  30.         private var mtx2:Matrix;
  31.         
  32.         
  33.         public function TileLoopScroll() 
  34.         {
  35.             addEventListener(Event.ADDED_TO_STAGE , added);
  36.         }
  37.         
  38.         
  39.         private function added(e:Event):void 
  40.         {
  41.             removeEventListener(Event.ADDED_TO_STAGE, added);
  42.             
  43.             init();
  44.         }
  45.         
  46.         
  47.         private function getStar():BitmapData {
  48.             // 素材の準備
  49.             var star:Shape = new Shape();
  50.             star.graphics.beginFill(0xFCE80D);
  51.             star.graphics.drawRect(-50, -50100100);
  52.             star.graphics.beginFill(0xFCC00D);
  53.             GraphicsUtil.star(star.graphics, 2040);
  54.             star.graphics.endFill();
  55.             star.x = star.width * 0.5;
  56.             star.y = star.height * 0.5;
  57.             
  58.             var mtx:Matrix = new Matrix();
  59.             mtx.translate(star.width * 0.5, star.height * 0.5);
  60.             
  61.             var result:BitmapData;
  62.             result = new BitmapData(star.width, star.height);
  63.             result.draw(star, mtx);
  64.             
  65.             return result;
  66.         }
  67.         
  68.         private function init():void
  69.         {
  70.             stage.align = StageAlign.TOP_LEFT;
  71.             stage.scaleMode = StageScaleMode.NO_SCALE;
  72.             
  73.             bd = getStar();
  74.             
  75.             bdw = bd.width;
  76.             bdh = bd.height;
  77.             
  78.             // リサイズ用
  79.             setStageSize(null);
  80.             
  81.             // beginBitmapFill 用の Matrix
  82.             mtx2 = new Matrix();
  83.             
  84.             this.addEventListener(Event.ENTER_FRAME , loop);
  85.             stage.addEventListener(Event.RESIZE , setStageSize);
  86.             
  87.             addChild(new Stats());
  88.         }
  89.         
  90.         
  91.         private function draw():void
  92.         {
  93.             // 素材の bitmapdata
  94.             bd.dispose();
  95.             bd = null;
  96.             bd = getStar();
  97.             
  98.             // 一応 clear() してるのですが・・・
  99.             this.graphics.clear();
  100.             this.graphics.beginBitmapFill(bd, mtx2);
  101.             this.graphics.drawRect(00, sw, sh);
  102.             this.graphics.endFill();
  103.         }
  104.         
  105.         
  106.         private function loop(e:Event):void 
  107.         {
  108.             if (mtx2.tx > bdw || mtx2.ty > bdh) 
  109.             {
  110.                 mtx2.tx = mtx2.ty = 0;
  111.             }
  112.             
  113.             // tx,ty でスクロールさせる。
  114.             mtx2.tx += 2
  115.             mtx2.ty += 2
  116.             
  117.             draw();
  118.         }
  119.         
  120.         
  121.         private function setStageSize(e:Event):void 
  122.         {
  123.             sw = stage.stageWidth;
  124.             sh = stage.stageHeight;
  125.         }
  126.         
  127.     }
  128. }
  129. import flash.display.Graphics;
  130. import flash.display.Shape;
  131. import flash.geom.Point;
  132. /** 
  133. * 既存にない Graphics を描画するクラスです。Graphics を返します。
  134. */
  135. class GraphicsUtil
  136. {
  137.     static private var PI:Number = Math.PI;
  138.     static private var DEGTORAD:Number = Math.PI / 180;
  139.     
  140.     
  141.     /** 
  142.     * 星形
  143.     * @param g Graphics ターゲット 
  144.     * @param inR 内側の半径
  145.     * @param outR 外側の半径
  146.     * @param vertex 頂点の数。5点以上
  147.     * @return Graphics 生成した星形
  148.     */
  149.     public static function star(g:Graphics, inR:Number, outR:Number, vertex:int = 5):Graphics
  150.     {
  151.         var points:/*Number*/Array = [];
  152.         var rad:Number = PI * 2 / vertex;
  153.         
  154.         for (var i: int = 0; i < vertex; i++) 
  155.         {
  156.             var outTheta:Number = (i * rad) - PI * 0.5;
  157.             var inTheta:Number = outTheta + (rad * 0.5);
  158.             
  159.             points[i << 2] = outR * Math.cos(outTheta);
  160.             points[(i << 2) + 1] = outR * Math.sin(outTheta);
  161.             points[(i << 2) + 2] = inR * Math.cos(inTheta);
  162.             points[(i << 2) + 3] = inR * Math.sin(inTheta);
  163.         }
  164.         
  165.         g.moveTo(points[0], points[1]);
  166.         
  167.         var n:int = points.length >> 1;
  168.         for (var j: int = 1; j < n; j++) 
  169.         {
  170.             g.lineTo(points[j << 1], points[(j << 1) + 1]);
  171.         }
  172.         
  173.         g.lineTo(points[0], points[1]);
  174.         
  175.         return g;
  176.     }
  177. }
noswf

forked from: beginBitmapFill で背景を無限スクロール(dispose) forked from: beginBitmapFill で背景を無限スクロール(Statsの罠) [diff(35)]

  1. // forked from kkeisuke's forked from: beginBitmapFill で背景を無限スクロール(dispose)
  2. // forked from kkeisuke's beginBitmapFill で背景を無限スクロール
  3. package  
  4. {
  5.     import flash.display.BitmapData;
  6.     import flash.display.Shape;
  7.     import flash.display.Sprite;
  8.     import flash.display.StageAlign;
  9.     import flash.display.StageScaleMode;
  10.     import flash.events.Event;
  11.     import flash.geom.Matrix;
  12.     import net.hires.debug.Stats;
  13.     import flash.text.*;
  14.     import flash.system.System
  15.     
  16.     /** 
  17.     * beginBitmapFill のテストです。
  18.     * 背景を無限スクロールさせると、メモリはどのような状態になるのでしょう?
  19.     * 
  20.     * アドバイスを頂きました!ありがとうございます!!
  21.     * dispose() を入れたら、自宅の環境だと 5M も減りました!!
  22.     * 開放される頻度も上がったように感じます!!
  23.     */
  24.     [SWF(backgroundColor = 0xffffff, frameRate = 40, width = 465, height = 465)]
  25.     public class TileLoopScroll extends Sprite
  26.     {
  27.         private var bd:BitmapData;
  28.         private var bdw:int;
  29.         private var bdh:int;
  30.         private var sw:Number;
  31.         private var sh:Number;
  32.         private var mtx2:Matrix;
  33.         private var dbg:TextField;
  34.         private var Memory:Number=0;
  35.         private var maxMemory:Number=0;
  36.         
  37.         
  38.         public function TileLoopScroll() 
  39.         {
  40.             addEventListener(Event.ADDED_TO_STAGE , added);
  41.         }
  42.         
  43.         
  44.         private function added(e:Event):void 
  45.         {
  46.             removeEventListener(Event.ADDED_TO_STAGE, added);
  47.             
  48.             init();
  49.         }
  50.         
  51.         
  52.         private function getStar():BitmapData {
  53.             // 素材の準備
  54.             var star:Shape = new Shape();
  55.             star.graphics.beginFill(0xFCE80D);
  56.             star.graphics.drawRect(-50, -50100100);
  57.             star.graphics.beginFill(0xFCC00D);
  58.             GraphicsUtil.star(star.graphics, 2040);
  59.             star.graphics.endFill();
  60.             star.x = star.width * 0.5;
  61.             star.y = star.height * 0.5;
  62.             
  63.             var mtx:Matrix = new Matrix();
  64.             mtx.translate(star.width * 0.5, star.height * 0.5);
  65.             
  66.             var result:BitmapData;
  67.             result = new BitmapData(star.width, star.height);
  68.             result.draw(star, mtx);
  69.             
  70.             return result;
  71.         }
  72.         
  73.         private function init():void
  74.         {
  75.             stage.align = StageAlign.TOP_LEFT;
  76.             stage.scaleMode = StageScaleMode.NO_SCALE;
  77.             
  78.             bd = getStar();
  79.             
  80.             bdw = bd.width;
  81.             bdh = bd.height;
  82.             
  83.             // リサイズ用
  84.             setStageSize(null);
  85.             
  86.             // beginBitmapFill 用の Matrix
  87.             mtx2 = new Matrix();
  88.             
  89.             this.addEventListener(Event.ENTER_FRAME , loop);
  90.             stage.addEventListener(Event.RESIZE , setStageSize);
  91.             
  92.             //addChild(new Stats());
  93.             dbg=new TextField()
  94.             dbg.autoSize=TextFieldAutoSize.LEFT
  95.             dbg.selectable=false;
  96.             dbg.mouseEnabled=false;
  97.             var format:TextFormat=new TextFormat();
  98.             format.color=0x666666
  99.             format.size=12;
  100.             format.font='_ゴシック';
  101.             dbg.defaultTextFormat=format
  102.             addChild(dbg);
  103.             dbg.x=200
  104.             
  105.         }
  106.         
  107.         
  108.         private function draw():void
  109.         {
  110.             // 素材の bitmapdata
  111.             //bd.dispose();
  112.             //bd = null;
  113.             //bd = getStar();
  114.             
  115.             // 一応 clear() してるのですが・・・
  116.             this.graphics.clear();
  117.             this.graphics.beginBitmapFill(bd, mtx2);
  118.             this.graphics.drawRect(00, sw, sh);
  119.             this.graphics.endFill();
  120.             //メモリが増えていくのは、実はStatsのPixel描画によるものでは?
  121.             //TextAreaにMemoryを表示するとほとんど変わらないので、上記の操作でメモリリークは無いと思われます。
  122.             //なお、dispose()は、BitmapDataのメモリを開放するので、その際ガベージコレクションを更新しているに過ぎない気がします。
  123.             //実際には、わずかですが、その処理分の負荷が増える事になりますよね
  124.             //ちなみにこれでも、微妙にメモリは加算されますが、下記の表記によるゴミみたいなものなので、
  125.             //デバックできる環境で、System.gc()(ガベージコレクションの強制開放)すると、メモリがまったく変化しないのが確認できるかと思います。
  126.             Memory=Number((System.totalMemory * 0.000000954).toFixed(3));
  127.             maxMemory=(maxMemory<Memory)?Memory:maxMemory;
  128.             dbg.text='Mem '+String(Memory)+'\nMax '+String(maxMemory);
  129.             //以下はデバッグでのみ有効()
  130.             System.gc();
  131.         }
  132.         
  133.         
  134.         private function loop(e:Event):void 
  135.         {
  136.             if (mtx2.tx > bdw || mtx2.ty > bdh) 
  137.             {
  138.                 mtx2.tx = mtx2.ty = 0;
  139.             }
  140.             
  141.             // tx,ty でスクロールさせる。
  142.             mtx2.tx += 2
  143.             mtx2.ty += 2
  144.             
  145.             draw();
  146.         }
  147.         
  148.         
  149.         private function setStageSize(e:Event):void 
  150.         {
  151.             sw = stage.stageWidth;
  152.             sh = stage.stageHeight;
  153.         }
  154.         
  155.     }
  156. }
  157. import flash.display.Graphics;
  158. import flash.display.Shape;
  159. import flash.geom.Point;
  160. /** 
  161. * 既存にない Graphics を描画するクラスです。Graphics を返します。
  162. */
  163. class GraphicsUtil
  164. {
  165.     static private var PI:Number = Math.PI;
  166.     static private var DEGTORAD:Number = Math.PI / 180;
  167.     
  168.     
  169.     /** 
  170.     * 星形
  171.     * @param g Graphics ターゲット 
  172.     * @param inR 内側の半径
  173.     * @param outR 外側の半径
  174.     * @param vertex 頂点の数。5点以上
  175.     * @return Graphics 生成した星形
  176.     */
  177.     public static function star(g:Graphics, inR:Number, outR:Number, vertex:int = 5):Graphics
  178.     {
  179.         var points:/*Number*/Array = [];
  180.         var rad:Number = PI * 2 / vertex;
  181.         
  182.         for (var i: int = 0; i < vertex; i++) 
  183.         {
  184.             var outTheta:Number = (i * rad) - PI * 0.5;
  185.             var inTheta:Number = outTheta + (rad * 0.5);
  186.             
  187.             points[i << 2] = outR * Math.cos(outTheta);
  188.             points[(i << 2) + 1] = outR * Math.sin(outTheta);
  189.             points[(i << 2) + 2] = inR * Math.cos(inTheta);
  190.             points[(i << 2) + 3] = inR * Math.sin(inTheta);
  191.         }
  192.         
  193.         g.moveTo(points[0], points[1]);
  194.         
  195.         var n:int = points.length >> 1;
  196.         for (var j: int = 1; j < n; j++) 
  197.         {
  198.             g.lineTo(points[j << 1], points[(j << 1) + 1]);
  199.         }
  200.         
  201.         g.lineTo(points[0], points[1]);
  202.         
  203.         return g;
  204.     }
  205. }
noswf
Get Adobe Flash Player