비헤이비어 격리

가능하면 단일 핸들러에서 Event.ENTER_FRAME 이벤트와 같은 이벤트를 분리합니다.

Apple 클래스의 Event.ENTER_FRAME 이벤트를 단일 핸들러에 격리하여 코드를 더욱 최적화할 수 있습니다. 이 기법을 사용하면 CPU 리소스가 절약됩니다. 다음 예제에서는 BitmapApple 클래스에서 더 이상 이동 비헤이비어를 처리하지 않는 이러한 다른 방식을 보여 줍니다.

package org.bytearray.bitmap 
{ 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
     
    public class BitmapApple extends Bitmap 
    { 
        private var destinationX:Number; 
        private var destinationY:Number; 
         
        public function BitmapApple(buffer:BitmapData) 
        { 
            super (buffer); 
             
            smoothing = true; 
        } 
}

다음 코드는 단일 핸들러에서 사과를 인스턴스화하고 사과의 이동을 처리합니다.

import org.bytearray.bitmap.BitmapApple; 
 
const MAX_NUM:int = 100; 
var holder:Sprite = new Sprite(); 
 
addChild(holder); 
 
var holderVector:Vector.<BitmapApple> = new Vector.<BitmapApple>(MAX_NUM, true); 
var source:AppleSource = new AppleSource(); 
var bounds:Object = source.getBounds(source); 
     
var mat:Matrix = new Matrix(); 
mat.translate(-bounds.x,-bounds.y); 
 
stage.quality = StageQuality.BEST; 
 
var buffer:BitmapData = new BitmapData(source.width+1,source.height+1, true,0); 
buffer.draw(source,mat); 
 
stage.quality = StageQuality.LOW; 
     
var bitmapApple:BitmapApple; 
     
for (var i:int = 0; i< MAX_NUM; i++) 
{ 
    bitmapApple = new BitmapApple(buffer); 
     
    bitmapApple.destinationX = Math.random()*stage.stageWidth; 
    bitmapApple.destinationY = Math.random()*stage.stageHeight; 
     
    holderVector[i] = bitmapApple; 
         
    holder.addChild(bitmapApple); 
} 
 
stage.addEventListener(Event.ENTER_FRAME,onFrame); 
 
var lng:int = holderVector.length 
 
function onFrame(e:Event):void 
{     
        for (var i:int = 0; i < lng; i++) 
        { 
            bitmapApple = holderVector[i]; 
            bitmapApple.alpha = Math.random(); 
             
            bitmapApple.x -= (bitmapApple.x - bitmapApple.destinationX) *.5; 
            bitmapApple.y -= (bitmapApple.y - bitmapApple.destinationY) *.5; 
             
            if (Math.abs(bitmapApple.x - bitmapApple.destinationX ) < 1 &&      
                Math.abs(bitmapApple.y - bitmapApple.destinationY ) < 1) 
            { 
                    bitmapApple.destinationX = Math.random()*stage.stageWidth; 
                    bitmapApple.destinationY = Math.random()*stage.stageHeight; 
            } 
        } 
}

각 사과를 이동하는 200개의 핸들러 대신 이동을 처리하는 단일 Event.ENTER_FRAME 이벤트가 생성됩니다. 전체 애니메이션을 쉽게 일시 정지할 수 있으므로 게임에서 유용할 수 있습니다.

예를 들어 간단한 게임에서 다음 핸들러를 사용할 수 있습니다.

stage.addEventListener(Event.ENTER_FRAME, updateGame); 
function updateGame (e:Event):void 
{ 
    gameEngine.update(); 
} 

다음 단계는 사과가 마우스 또는 키보드와 상호 작용하도록 하는 것입니다. 여기에는 BitmapApple 클래스의 수정이 필요합니다.

package org.bytearray.bitmap 
{ 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
    import flash.display.Sprite; 
     
    public class BitmapApple extends Sprite 
    { 
        public var destinationX:Number; 
        public var destinationY:Number; 
        private var container:Sprite; 
        private var containerBitmap:Bitmap; 
         
        public function BitmapApple(buffer:BitmapData) 
        { 
            container = new Sprite(); 
            containerBitmap = new Bitmap(buffer); 
            containerBitmap.smoothing = true; 
            container.addChild(containerBitmap); 
            addChild(container); 
        } 
}

일반적인 Sprite 객체와 같이 대화형의 BitmapApple 인스턴스가 생성됩니다. 그러나 인스턴스는 표시 객체가 변형되는 경우 다시 샘플링되지 않는 단일 비트맵에 연결됩니다.