2

scrollRect のパフォーマンスを向上させるには、ベクターをビットマップとしてキャッシュする必要があります。そうしないと、scrollRect のパフォーマンスが低下するだけのマスクになります (情報ソース)。

ただし、cacheAsBitmap を適用すると、オブジェクト/scrollRect を移動できないようです。なぜ?

package
{
    //Imports
    import flash.display.Screen;
    import flash.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;
        }
    }
}
4

1 に答える 1

2

さらに面白い:

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
        }
    }
}

なので、何らかのバグだと思います。

于 2011-08-30T10:34:28.970 に答える