in order to increase performance of a scrollRect i must cache the vector as a bitmap, otherwise the scrollRect will be simply a less performant mask (info source).
however, i can't seem to move an object/scrollRect once i've applied cacheAsBitmap. why?
package
{
//Imports
import flash.display.Screen;
import f开发者_Go百科lash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
//Class
[SWF(width = "800", height = "500", backgroundColor = "0x444444")]
public class ScrollRectTest extends Sprite
{
//Variables
private var background:Sprite;
private var ball:Sprite;
private var newScrollRect:Rectangle;
//Constructor
public function ScrollRectTest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.frameRate = 60;
init();
}
//Initialize
private function init():void
{
background = new Sprite();
background.graphics.beginFill(0x000000, 1.0);
background.graphics.drawRect(0, 0, 200, 400);
background.graphics.endFill();
ball = new Sprite();
ball.graphics.beginFill(0xFF0000, 1.0);
ball.graphics.drawCircle(0, 0, 100);
ball.graphics.endFill();
//ball.cacheAsBitmap = true; //<-- uncomment this
ball.scrollRect = new Rectangle(background.x, background.y, background.width, background.height);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
addChild(background);
addChild(ball);
}
//Mouse Down Event Handler
private function mouseDownEventHandler(evt:MouseEvent):void
{
addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Mouse Up Event Handler
private function mouseUpEventHandler(evt:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Enter Frame Event Handler
private function enterFrameEventHandler(evt:Event):void
{
newScrollRect = ball.scrollRect;
newScrollRect.y -= 10;
newScrollRect.x -= 5;
ball.scrollRect = newScrollRect;
}
}
}
Even more funny:
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
[SWF(width = "800", height = "500", backgroundColor = "0x444444")]
public class ScrollRectTest extends Sprite
{
//Variables
private var background:Sprite;
private var ball:Sprite;
private var newScrollRect:Rectangle;
//Constructor
public function ScrollRectTest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.frameRate = 60;
init();
}
//Initialize
private function init():void
{
background = new Sprite();
background.graphics.beginFill(0x000000, 1.0);
background.graphics.drawRect(0, 0, 200, 400);
background.graphics.endFill();
ball = new Sprite();
ball.graphics.beginFill(0xFFFF00, 1.0);
ball.graphics.drawCircle(0, 0, 100);
ball.graphics.endFill();
var foo:Sprite = new Sprite();
var g:Graphics = foo.graphics;
g.beginFill(0xFF0000, 0.2);
g.drawRect(0, 0, 100, 100);
g.endFill();
ball.addChild(foo);
ball.cacheAsBitmap = true; //<-- uncomment this
ball.scrollRect = new Rectangle(background.x, background.y, background.width, background.height);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
addChild(background);
addChild(ball);
}
//Mouse Down Event Handler
private function mouseDownEventHandler(evt:MouseEvent):void
{
addEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Mouse Up Event Handler
private function mouseUpEventHandler(evt:MouseEvent):void
{
removeEventListener(Event.ENTER_FRAME, enterFrameEventHandler);
}
//Enter Frame Event Handler
private function enterFrameEventHandler(evt:Event):void
{
newScrollRect = ball.scrollRect;
newScrollRect.y -= 1;
newScrollRect.x -= 1;
ball.scrollRect = newScrollRect;
// ball.scaleX = 1 + Math.random() * 0.01;// uncomment this to force redraw
}
}
}
So I assume it's some kind of bug.
精彩评论