这里我创建了一个网格。但是当大量列闪存时,意外退出。
package
{
import flash.events.MouseEvent;
import fl.transitions.Tween;
import fl.transitions.easing.Strong;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import fl.motion.Color;
import flash.text.TextField;
public class Line extends MovieClip
{
public function Line()
{
addEventListener(Event.ENTER_FRAME,createGraphContainer);
// constructor code
}
public function createGraphContainer(ev:Event):void
{
var grid:MovieClip = new MovieClip();
var numColumns:Number = 50;
var numRows:Number = 25;
var cellHeight:Number = 5;
var cellWidth:Number = 10;
grid.graphics.clear();
grid.graphics.lineStyle(.5, 0xFFECDB);
// we drop in the " + 1 " so that it will cap the right and bottom sides.;
for (var col:Number = 0; col < numColumns + 1; col++)
{
for (var row:Number = 0; row < numRows + 1; row++)
{
addChild(grid);
grid.x = 50;
grid.y = 50;
//trace(col, row);
grid.graphics.moveTo(0, row * cellHeight);
grid.graphics.lineTo(cellWidth * numColumns, row * cellHeight);
grid.graphics.moveTo(col * cellWidth, 0);
grid.graphics.lineTo(col * cellWidth, cellHeight * numRows);
}
}
}
}
}
这里var numColumns:Number = 150; var numRows:Number = 225;闪光灯意外退出。给我一个解决方案。
答案 0 :(得分:3)
你要添加row * col children(在嵌套循环中:addchild(grid)),这样你就会得到一堆包含越来越复杂的矢量绘图的row * col动画片段。 不好,特别是因为这是在enterframe上完成的!
实际上它是一个网格,应该绘制一次,图形本身将绘制在另一层上。如果必须重绘网格,可以将displayobject传递给函数。
此外,您不需要嵌套循环:使用drawRect可以分割嵌套循环,并且当rect一次绘制顶部和底部时,您可以将循环递增2。
这是一个应该运行得稍微快一点的例子:)
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 < numColumns; col += 2 )
{
grid.graphics.drawRect( col * cellWidth, 0, cellWidth, gridHeight );
}
for (var row:Number = 0; row < 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.
}
}
}