actionscript 3 - How to deal with multiple referencing of movieclips -


im looking both efficient , effective way create, control , delete movieclips in effective manner. have thought doing create controller class handle creation , deletion of movieclip using arrays , script...

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; < array.length;i++)         {             array[i].id -=1;          }     } 

for quick , simple explanation of variables, globals there allow creation of instances inside class no direct access stage.

this script works fine until 2 cases occur.

the first case when 2 movieclips being created in same frame, , second case deletion.

i know because of when the first movieclip created or removed in frame, sorting of array occurs, making second movieclip position null. question is, more effective way control such instances prevent occurance?

keep in mind control dynamically created movieclip instances.

much obliged,

raider00321

try separate model , controller classes. leave manipulates ship appearance controller. make sure when model changes notifies everyone, wants notified, dispatching events.

this model:

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 have 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 don't have 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;     } } } 

this simple controller:

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 implemented     }      private function onshipremoved(e:shipevent):void      {         e.ship.blow(); // blow must implemented         battlefield.removechild(e.ship);         var ships:array = shipservice.getallships();         each (var ship:ship in ships)          {             ship.cry();         }     }  } } 

this event class fly through application , notify everything:

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

this simple(and little) part of mvc pattern. use or make own, , see big advantages soon.


about dictionaries: why want use it? remember implementation of hashmap (in as3 hashmap use simple-type objects keys object() ) uses object keys. don't understand benefits give you.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -