我目前正在寻找一种高效且有效的方式来有效地创建,控制和删除动画片段。我想做的是创建一个控制器类,它将使用数组和这个脚本来处理movieclip的创建和删除......
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;
}
}
为了快速简单地解释一些变量,Globals允许在类中创建实例而不能直接访问该阶段。
此脚本可以正常工作,直到出现两种情况。
第一种情况是在同一帧中创建两个动画片段,第二种情况是删除。
我知道这是因为在帧中创建或删除第一个动画片段时,会发生数组排序,使第二个动画片段位置为空。然而,我的问题是,控制此类实例以防止这种情况的更有效方法是什么?
请记住,这是为了控制动态创建的动画片段实例。
很有责任,
Raider00321
答案 0 :(得分:1)
尝试将Model和Controller类分开。将操纵船舶外观的所有内容留给控制器。确保模型发生变化时,通过调度事件通知每个想要通知的人。
这是您的模型:
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();
}
}
}
}
这是您的活动课程,它将通过您的应用程序并通知每个人所有事情:
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模式的简单(和很少)部分。使用它或自己做,你很快就会看到很大的优势。
关于字典:你为什么要使用它?我记得它是HashMap的实现(在AS3 HashMap中使用简单类型的对象作为键是Object()),它使用Object作为键。我不清楚它能给你带来什么好处。
答案 1 :(得分:0)
我相信我找到了一个更适合我的问题的解决方案。我打算使用字典和唯一键来解决调整大小数组的问题。