0

我目前正在寻找一种既有效又有效的方法来以有效的方式创建、控制和删除影片剪辑。我想做的是创建一个控制器类,它将使用数组和这个脚本处理影片剪辑的创建和删除......

private static var enemyShips:Array = new Array();
public static function addEnemy(ship:MovieClip):void
    {
        enemyShips.push(ship);
        trace(enemyShips.length);
        ship.id = enemyShips.length - 1;
        Globals._stage.addChild(ship);          
    }
    public static function getEnemy():Array
    {
        return enemyShips;
    }

    public static function removeEnemy(i:int):void
    {
        var ep:ExplosionParticle;
        for(var j:int = 0; j < 150; j++)
        {
            ep  = new ExplosionParticle(enemyShips[i].x, enemyShips[i].y);
            Globals._stage.addChild(ep);
        }
        Globals._stage.removeChild(enemyShips[i]);
        updatePositions(enemyShips, i+1);
        enemyShips.splice(i, 1);
    }
private static function updatePositions(array:Array, position:int):void
    {
        for(var i:int = position; i < array.length;i++)
        {
            array[i].id -=1;

        }
    }

对于一些变量的快速简单的解释,全局变量允许在类中创建实例,而不能直接访问舞台。

此脚本工作正常,直到出现两种情况。

第一种情况是在同一帧中创建两个影片剪辑,第二种情况是删除。

我知道这是因为在帧中创建或删除第一个影片剪辑时,会发生数组排序,使第二个影片剪辑位置为空。然而,我的问题是,有什么更有效的方法来控制这种情况以防止这种情况发生?

请记住,这是为了控制动态创建的影片剪辑实例。

非常感谢,

攻略00321

4

2 回答 2

1

尝试将您的模型和控制器类分开。将所有操纵您的飞船外观的东西留给您的控制器。确保当您的模型发生更改时,它会通过调度事件通知所有想要被通知的人。

这是你的模型

package
{
import flash.events.EventDispatcher;

public class ShipService extends EventDispatcher
{
    private var ships:Array = new Array();

    public function add(ship:Ship):void
    {
        var index:int = ships.indexOf(ship);
        if (index >= 0) { // check if we already have this ship
            return;
        }
        ships.push(ship);
        ships.forEach(setNewIndexes);
        dispatchEvent(new ShipEvent(ShipEvent.ADDED, ship));
    }

    public function remove(ship:Ship):void {
        var index:int = ships.indexOf(ship);
        if (index < 0) { // check if we don't have this ship
            return;
        }
        ships.splice(index, 1);
        ships.forEach(setNewIndexes);
        dispatchEvent(new ShipEvent(ShipEvent.REMOVED, ship));
    }

    public function getAllShips() {
        return ships;
    }

    private function setNewIndexes(ship:MovieClip, index:int, arr:Array):void {
        ship.id = index;
    }
}
}

这是您的简单控制器

package  
{
import flash.display.DisplayObjectContainer;
public class ShipController 
{
    private var shipService:ShipService;
    private var battleField:DisplayObjectContainer;

    public function ShipController(battleField:DisplayObjectContainer, shipService:ShipService) 
    {
        this.battleField = battleField;
        this.shipService = shipService;
        this.shipService.addEventListener(ShipEvent.ADDED, onShipAdded);
        this.shipService.addEventListener(ShipEvent.REMOVED, onShipRemoved);
    }

    private function onShipAdded(e:ShipEvent):void 
    {
        battleField.addChild(e.ship);
        e.ship.x = 15;
        e.ship.y = 15;
        ship.sayHello(); // sayHello must be implemented
    }

    private function onShipRemoved(e:ShipEvent):void 
    {
        e.ship.blow(); // blow must be implemented
        battleField.removeChild(e.ship);
        var ships:Array = shipService.getAllShips();
        for each (var ship:Ship in ships) 
        {
            ship.cry();
        }
    }

}
}

这是您的Event类,它将飞过您的应用程序并通知所有人所有事情:

package  
{
import flash.events.Event;

public class ShipEvent extends Event 
{
    public static const REMOVED:String = "ship_removed";
    public static const ADDED:String = "ship_added";

    public var ship:Ship;

    public function ShipEvent(type:String, ship:Ship; bubbles:Boolean = false, cancelable:Boolean = false) 
    { 
        super(type, bubbles, cancelable);
        this.ship = ship;
    } 

    public override function clone():Event 
    { 
        return new ShipEvent(type, ship, bubbles, cancelable);
    } 
}

}

这是 MVC 模式的简单(而且很少)部分。使用它或自己制作,您很快就会看到巨大的优势。


关于词典:为什么要使用它?我记得它是使用 Object 作为键的 HashMap 的实现(在使用简单类型对象作为键的 AS3 HashMap 中是 Object() )。我不清楚它能给你带来什么好处。

于 2013-07-11T18:16:38.160 回答
0

我相信我已经为我的问题找到了更合适的解决方案。我打算使用字典和唯一键来解决调整数组大小的问题。

于 2013-07-11T11:11:07.800 回答