Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You must call bmd1.dispose(); before reinstantiating it or it will not release the bitmap from memory.</p> <p><strong>Edit:</strong></p> <p>After looking through your code very carefully, optimizing it, and cleaning up some things, I have come to the conclusion that your simulation is no longer running slower and slower.</p> <p>The problem was that the way you would calculate the particle's x and y speed. Essentially it would diminish in speed until stopping all together. On top of the massive high quality blur you have placed and the lack of freeing bitmaps, your movie would appear and actually slow to a crawl.</p> <p>Here is your code I have modified. I benchmarked it and it didn't peak above 10% cpu or 40k memory after 15 minutes.</p> <p><strong>Particle.as</strong></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 deleted:Boolean = false; public function Particle() { this.graphics.beginFill(0x00CCFF); this.graphics.drawCircle(0,0,Math.random() * 30); //The max prevents particle speed from rounding to zero. speedX = Math.ceil(Math.random() * 10 - 5); speedY = Math.ceil(Math.random() * 10 - 5); } public function render():void{ //It originally appeared to be slowing down. In-fact, it was. x += Math.random() * speedX; y += Math.random() * speedY; deleted = (x &gt; FluidLayer.w || y &gt; FluidLayer.h); //Comment this below if you want particles to be removed once they go out of bounds. if(deleted) { x = Math.random() * FluidLayer.w; y = Math.random() * FluidLayer.h; deleted = false; } } public function isDeleted():Boolean { return deleted; } } } </code></pre> <p><strong>FluidLayer.as</strong></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 bitmap:Bitmap; private var bitmapData:BitmapData; private var particleArray:Array = new Array(); public static const w:uint = 550; public static const h:uint = 400; public function FluidLayer() { addEventListener(Event.ADDED_TO_STAGE, initialize); } //By only one event handler to render, we prevent the overhead of 599 bubbling events. private function render(e:Event):void { for each(var p:Particle in particleArray) { p.render(); //uncomment below if you want particles to become removed when they navigate out of bounds. //if(p.isDeleted()) { //canvas.removeChild(p); //particleArray.splice(particleArray.indexOf(p),1); //} } bitmapData.fillRect(bitmapData.rect, 0); //clear the bitmapdata bitmapData.draw(canvas,null,null,null,null,true); bitmapData.threshold(bitmapData, bitmapData.rect, new Point(0,0), "&gt;", 0XFF2b2b2b, 0x55FFFF, 0xFFFFFF, false); } //We call initialize once the fluid layer has been added to stage //or else stage values will be null. private function initialize(e:Event):void { canvas = new Sprite(); //You DEFINITELY want to lower the blur amount here. //This is what is ultimately slowing your SWF down to a crawl. //canvas.filters = new Array(new BlurFilter(20,20,1)); for(var i:uint = 0; i &lt; 600; i++) { var p:Particle = new Particle(); p.x = Math.random() * w; p.y = Math.random() * h; canvas.addChild(p); particleArray.push(p); } //The bitmap and bitmapData only need to be initialized once bitmapData = new BitmapData(w, h, true); bitmap = new Bitmap(bitmapData, "auto", true); bitmap.alpha = 0.7; addChild(bitmap); addEventListener(Event.ENTER_FRAME, render); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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