Note that there are some explanatory texts on larger screens.

plurals
  1. POAS3: Why is this fluid simulation running slower and slower?
    text
    copied!<p>I'v got 2 classes. Simulating fluid. Both classes are pretty straight forward and short, but after 2 seconds of running, the simulation gets really slow, looks like a memory leak. But i can't see any leaks in this code. </p> <p>Please let me know if you can figure out, why this is happening?</p> <p>FluidLayer.as</p> <pre><code>package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.display.BitmapData; import flash.filters.BlurFilter; import flash.display.Bitmap; import flash.geom.Point; import flash.geom.ColorTransform; import flash.display.IBitmapDrawable; import flash.events.Event; import flash.display.MovieClip; public class FluidLayer extends MovieClip { private var canvas:Sprite; private var b:Bitmap; private var blur:BlurFilter = new BlurFilter(20,20,3); private var bmd1:BitmapData; public function FluidLayer() { canvas = new Sprite(); for(var i:int = 0;i &lt; 600;i++){ var p:Particle = new Particle(); canvas.addChild(p); p.x = stage.stageWidth * Math.random(); p.y = stage.stageHeight* Math.random(); p.initi(stage); } canvas.filters = new Array(blur); addEventListener(Event.ENTER_FRAME, render); } private function render(e:Event):void{ remove(); b = new Bitmap(makeFluid(canvas),"auto", true); b.alpha = 0.7; addChild(b); } private function makeFluid(o:Sprite):BitmapData{ bmd1 = new BitmapData(stage.stageWidth, stage.stageHeight, true); bmd1.draw(o,null,null,null,null,true); bmd1.threshold(bmd1, bmd1.rect, new Point(0,0), "&gt;", 0XFF2b2b2b, 0x55FFFF, 0xFFFFFF, false); return bmd1; } private function remove():void{ if(numChildren &gt; 1) removeChildAt(1); if(bmd1){ bmd1.dispose(); bmd1 = null; } } </code></pre> <p>}}</p> <p>Particle.as</p> <pre><code>package { import flash.display.MovieClip; import flash.events.Event; import flash.display.Stage; public class Particle extends MovieClip { private var speedX:int; private var speedY:int; private var _s:Stage; public function Particle() { this.graphics.beginFill(0x00CCFF); this.graphics.drawCircle(0,0,Math.random() * 30); speedX = Math.random() * 10 - 5; speedY = Math.random() * 10 - 5; this.addEventListener(Event.ADDED_TO_STAGE, initi); } public function initi(s:Stage):void{ this._s = s; addEventListener(Event.ENTER_FRAME, render); } private function render(e:Event):void{ this.x += Math.random()*speedX; this.y += Math.random()*speedY; if(this.x &gt; _s.stageWidth || this.y &gt; _s.stageHeight){ //this.x = Math.random()*_s.stageWidth; //this.y = Math.random()*_s.stageHeight; removeEventListener(Event.ENTER_FRAME, render); this.parent.removeChild(this); } } }} </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload