Aislamiento de comportamientos

Aísle eventos como, por ejemplo, Event.ENTER_FRAME en un solo controlador, si es posible.

El código se puede optimizar aún más aislando el evento Event.ENTER_FRAME de la clase Apple en un solo controlador. Con esta técnica se ahorran recursos de CPU. El siguiente ejemplo muestra este enfoque diferente, donde la clase BitmapApple no vuelve a administrar el comportamiento del movimiento:

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

El siguiente código crea instancias de las manzanas y controla su movimiento en un solo controlador:

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

El resultado es un solo evento Event.ENTER_FRAME que controla el movimiento, en lugar de 200 controladores moviendo cada manzana. Toda la animación se puede detener fácilmente, lo cual puede ser útil en un juego.

Por ejemplo, un sencillo juego puede utilizar el siguiente controlador:

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

El siguiente paso consiste en hacer que las manzanas interactúen con el ratón o el teclado, lo que requiere una modificación en la clase 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); 
        } 
}

El resultado son instancias de BitmapApple interactivas, como objetos Sprite tradicionales. Sin embargo, las instancias se vinculan a un solo mapa de bits, que no se vuelve a muestrear cuando se transforman los objetos de visualización.