forked from: メモリ管理の細々した事。Graphics描画と個別Sprite
forked from メモリ管理の細々した事。 (diff: 22)
ActionScript3 source code
/**
* Copyright djakarta_trap ( http://wonderfl.net/user/djakarta_trap )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/pwb1
*/
// forked from djakarta_trap's メモリ管理の細々した事。
package {
import flash.display.Sprite;
import net.hires.debug.Stats;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.system.*;
import flash.net.*;
[SWF(backgroundColor="#FFFFFF", frameRate=120)]
public class MemoryTest extends Sprite {
/*
* スプライトを個別にアドするのと比べると、フレームレートが落ちる。
* そのかわり、メモリ使用量は1000個のCircleで1Mくらい。
*/
private var _container:Sprite;
private var _counter:int;
private var _sp:Sprite;
public function MemoryTest() {
// write as3 code here..
_container = new Sprite();
this.addChild(_container);
this.addChild(new Stats());
_sp = new Sprite();
_container.addChild(_sp);
this.addEventListener(Event.ENTER_FRAME,_enterFrameHandler);
}
private function _enterFrameHandler(e:Event):void
{
if(_counter > 1000){
this.removeEventListener(e.type,_enterFrameHandler);
this.addEventListener(MouseEvent.CLICK,_clickHandler);
return;
}
var color:uint = Math.random()*0xffffff;
_sp.graphics.beginFill(color);
_sp.graphics.drawCircle(Math.floor(Math.random()*100)/100*stage.stageWidth,Math.floor(Math.random()*100)/100*stage.stageHeight,20);
_sp.graphics.endFill();
_counter++;
}
private function _clickHandler(e:MouseEvent):void
{
//this.removeChild(_container);
/*
* graphics.clear()が必要?
*/
_sp.graphics.clear();
_container.removeChild(_sp);
this.removeChild(_container);
_container = null;
_sp = null;
//強制的にGC発動
System.gc();
try {
new LocalConnection().connect('foo');
new LocalConnection().connect('foo');
}catch (e:*) {}
}
}
}
