柔性帆布上的绘制方块
问题描述:
我正在尝试在Flex帆布上的广场上的动画图像上编写一些代码。下面的代码有问题,因为它变得越来越慢。我假设我应该清理旧广场或其他东西。柔性帆布上的绘制方块
我在做什么错?:下面
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();">
<mx:Script>
<![CDATA[
public var ticker:Timer;
public function init():void {
ticker = new Timer(10);
// This implements the timerEvent
ticker.addEventListener(TimerEvent.TIMER, update);
ticker.start();
draw();
}
public function update(evt:TimerEvent):void {
draw();
}
public function draw():void {
var squareSize:uint = 10;
var square:Shape = new Shape();
square.graphics.beginFill(0xFFFFFF, 1.0);
square.graphics.drawRect(0, 0, myCanvas.width, myCanvas.height);
var i:int;
for (i = 0; i < myCanvas.height/squareSize; i++) {
var j:int;
for (j = 0; j < myCanvas.width/squareSize; j++) {
if (Math.random() < 0.5) {
square.graphics.beginFill(0x000000, 1.0);
square.graphics.drawRect(j * squareSize, i * squareSize, squareSize, squareSize);
}
}
}
square.graphics.endFill();
myCanvas.rawChildren.addChild(square);
}
]]>
</mx:Script>
<mx:Panel title="Random Squares" height="95%" width="95%"
paddingTop="5" paddingLeft="5" paddingRight="5" paddingBottom="5">
<mx:Canvas id="myCanvas" borderStyle="solid" height="100%" width="100%">
</mx:Canvas>
</mx:Panel>
</mx:Application>
答
您要添加的儿童数不确定您的显示列表,让你在性能麻烦了肯定。
您可以添加新的
myCanvas.rawChildren.removeChildAt(0);
myCanvas.rawChildren.addChild(square);
你也可以做掉同方之前将其完全删除旧的形状 - 注意绘制之前Graphics.clear()
通话,虽然。否则,图形对象将填满数据,就像显示列表现在一样。
public function draw():void {
var squareSize:uint = 10;
myCanvas.graphics.clear();
myCanvas.graphics.beginFill(0xFFFFFF, 1.0);
myCanvas.graphics.drawRect(0, 0, myCanvas.width, myCanvas.height);
...
myCanvas.graphics.beginFill(0x000000, 1.0);
myCanvas.graphics.drawRect(...)
...
myCanvas.graphics.endFill();
}
谢谢!这工作。 第二个解决方案中只有一个小错字。此行: square.graphics.drawRect(0,0,myCanvas.width,myCanvas.height); 应改为: myCanvas.graphics.drawRect(0,0,myCanvas.width,myCanvas.height); – 2009-12-29 19:26:25