Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you're adding row * col children ( in the nested loop: addchild( grid ) ) so you end up with a pile of row * col movieclips containing increasingly complex vector drawings. not good especially as this is done on enterframe !</p> <p>actually it is a grid and should be drawn once, the graph itself would be drawn on antoher layer. you could pass the displayobject to a function if you have to redraw the grid.</p> <p>plus you don't need nested loops : using a drawRect instead will give you the possibility of splitting the nested loops and as a rect draws the top and the bottom at once, you can event increment the loops by 2.</p> <p>here's an example that should run slightly faster :)</p> <pre><code>package { import flash.display.MovieClip; import flash.display.Shape; import flash.events.Event; import flash.utils.getTimer; public class Line extends MovieClip { private var grid:Shape; public function Line() { //adds the canvas once grid = new Shape(); grid.x = 50; grid.y = 50; addChild( grid ); //draws once createGraphContainer( grid, 100, 25, 5, 2 ); //draws in a loop //addEventListener(Event.ENTER_FRAME, redrawGrid ); } private function redrawGrid(e:Event):void { var t:uint = getTimer(); var cols:int = 1 + int( Math.random() * 200 ); var rows:int = 1 + int( Math.random() * 200 ); var width:int = 1 + Math.random() * 10; var height:int = 1 + Math.random() * 10; createGraphContainer( grid, cols, rows, width, height ); trace( 'redrawing took :', ( getTimer() - t ), 'for col:', cols, 'rows:', rows, 'cell width:', width, 'cell height:', height ); } public function createGraphContainer( grid:Shape, numColumns:int = 100, numRows:int = 100, cellWidth:Number = 10, cellHeight:Number = 5 ):void { grid.graphics.clear(); grid.graphics.lineStyle( 0.5, 0xFFECDB); //grid.graphics.beginFill( 0xFF0000, .25 ); // uncomment for debug var gridWidth:Number = numColumns * cellWidth; var gridHeight:Number = numRows * cellHeight; grid.graphics.drawRect( 0, 0, gridWidth, gridHeight ); for (var col:Number = 0; col &lt; numColumns; col += 2 ) { grid.graphics.drawRect( col * cellWidth, 0, cellWidth, gridHeight ); } for (var row:Number = 0; row &lt; numRows; row += 2 ) { grid.graphics.drawRect( 0, row * cellHeight, gridWidth, cellHeight ); } grid.cacheAsBitmap = true;//not to be used in a loop very efficient if drawn once. } } } </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