我正在构建一个自定义绘画应用程序,我遇到了一个问题,即我添加到画布的图层在添加了24个图层后消失了。
以下是绘图工具的代码:
//=====================================================================================================================================
// DRAWING FUNCTIONS
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseDown_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: onMouseDown_canvas() - mouse:" + p );
_realView.addEventListener( MouseEvent.MOUSE_UP, onMouseUp_canvas );
_realView.addEventListener( MouseEvent.MOUSE_OUT, onMouseOut_canvas );
_realView.addEventListener( MouseEvent.MOUSE_MOVE, onMouseMove_canvas );
_currentLayer = new Sprite();
_currentLayer.mouseEnabled = false;
_realView.mcContainer.addChild( _currentLayer );
_$clearFirstBrushStroke = false;
switch( _$tool )
{
case ToolType.ERASER:
case ToolType.BRUSH:
case ToolType.PENCIL:
_currentLayer.graphics.beginFill(
( _$tool == ToolType.ERASER ? 0xffffff : _$color ),
( _$tool == ToolType.BRUSH ? .4 : 1 )
);
_currentLayer.graphics.drawCircle( p.x, p.y, _$toolSize/2 )
_currentLayer.graphics.endFill();
_firstPoint = p;
_$clearFirstBrushStroke = true;
if( _$tool == ToolType.BRUSH )
_currentLayer.filters = [ new BlurFilter( 3, 3, 2 ) ];
break;
case ToolType.BRUSH2:
drawGlitter( _currentLayer.graphics, p );
break;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseUp_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: onMouseUp_canvas() - mouse:" + p );
endDrawing();
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseMove_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
switch( _$tool )
{
case ToolType.ERASER:
case ToolType.BRUSH:
case ToolType.PENCIL:
if( _$clearFirstBrushStroke )
{
_$clearFirstBrushStroke = false;
_currentLayer.graphics.clear();
_currentLayer.graphics.lineStyle(
_$toolSize,
( _$tool == ToolType.ERASER ? 0xffffff : _$color ),
( _$tool == ToolType.BRUSH ? .4 : 1 ),
true,
LineScaleMode.NORMAL,
CapsStyle.ROUND,
JointStyle.ROUND
);
_currentLayer.graphics.moveTo( _firstPoint.x, _firstPoint.y );
}
_currentLayer.graphics.lineTo( p.x, p.y );
break;
case ToolType.BRUSH2:
drawGlitter( _currentLayer.graphics, p );
break;
}
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function onMouseOut_canvas( evt:MouseEvent ):void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: onMouseOut_canvas() - mouse:" + p );
endDrawing();
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function endDrawing():void
{
var p:Point = new Point( _realView.mouseX, _realView.mouseY );
_log.log( "CanvasViewController :: endDrawing() - mouse:" + p );
_realView.removeEventListener( MouseEvent.MOUSE_UP, onMouseUp_canvas );
_realView.removeEventListener( MouseEvent.MOUSE_OUT, onMouseOut_canvas );
_realView.removeEventListener( MouseEvent.MOUSE_MOVE, onMouseMove_canvas );
var bmd:BitmapData = new BitmapData( _realView.width, _realView.height, true, 0x00000000 );
bmd.draw( _currentLayer );
var bmp:Bitmap = new Bitmap( bmd, "auto", true );
_realView.mcContainer.removeChild( _currentLayer );
_realView.mcContainer.addChild( bmp );
_currentLayer = null;
_log.log( " numChildren:" + _realView.mcContainer.numChildren );
}
//-------------------------------------------------------------------------------------------------------------------------------------
protected function drawGlitter( g:Graphics, c:Point, $glitterSize:Number = 2, $density:Number = .5 ):void
{
var $startAngle:Number = NewMath.Random( 0, Math.PI*2 );
var $stepAngle:Number = $startAngle / ( $density * _$toolSize );
for( var $i:int = 0; $i < ( $density * _$toolSize ); $i++ )
{
var $a:Number = $startAngle + ( $i * $stepAngle );
var $r:Number = NewMath.Random( 0, _$toolSize / 2 );
var p:Point = new Point( c.x + Math.cos( $a ) * $r, c.y + Math.sin( $a ) * $r );
g.beginFill( _$color, 1 );
g.drawCircle( p.x, p.y, $glitterSize / 2 );
g.endFill();
}
}
numChildren继续增长,但是一旦它在该容器中遇到24层,第一层就会被删除或变得不可见或者其他东西。在25层,第二层消失,等等。
在此处查看此操作:http://dev.option5.net/CAUS/CAUS14002/index.html
这可能是一个掩盖问题吗?只能掩盖这么多层:_realView.mcContainer(添加新图层的地方)被_realView.mcMask屏蔽?