Reputation: 618
I have quite alots of display objects to manage during runtime, so Im currently using Arrays to manage them. But the problem is I have few many types of displays objects(eg. Tile, Npc and Building) which are basically MovieClips linked to the library. Each display object plays different role where it will be checked on enter frame, a loop.
Method 2 sounds much more faster and extensible however Im worried if it would affect the checking rate of each display object during runtime as the displays:Array grow larger and probably making it glitchy
So which one of the following method is faster+less glitchy and explain why you choose it.
Thanks in advance.
Method 1
var tiles:Array = new Array()
var npcs:Array = new Array()
var buildings:Array = new Array()
function createTiles(){
for(var i:Number=0; i<10; i++){
var t:Tile = new Tile() //Display object in the library
t.x = i * 50
t.y = i * 50
addChild(t)
tiles.push(t)
}
}
function createNpcs(){...}
function createBuildings(){...}
addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event){
for(var i:Number=0; i<tiles.length; i++){
//some codes
}
for(var j:Number=0; j<npcs.length; j++){
//some codes
}
for(var k:Number=0; k<buildings.length; k++){
//some codes
}
}
Method 2
var displays:Array = new Array();
function createDispalys(){
for(var i:Number=0; i<10; i++){
var t:Tile = new Tile() //Display object in the library
t.x = i * 50
t.y = i * 50
addChild(t)
displays.push(t)
}
for(var j:Number=0; j<10; j++){
//some code
displays.push(t)
}
for(var k:Number=0;k<10; k++){
//some codes
displays.push(t)
}
}
addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event){
for(var i:Number=0; i<display.length; i++){
if(display[i] is Tile){
//some codes
}else if(display[i] is Npc){
//some codes
}else if(display[i] is Building){
//some codes
}
}
}
Upvotes: 0
Views: 388
Reputation: 29280
Have you considered refactoring method 2 to put the logic inside the class itself?
Ie:
public interface DisplayAsset
{
void onEnterFrame();
}
// Implementations ommitted
public class Npc implements DisplayAsset {}
public class Tile implements DisplayAsset {}
public class Building implements DisplayAsset {}
Then, your loop remains extensible (just add another DisplayAsset impl.), and fast -- your code becomes:
var displays:Array = new Array(); // As per Gregor Kiddie's comment, use a vector here if possible
// as a vector:
// var displays:Vector.<DisplayAsset> = new Vector.<DisplayAsset>();
function createDispalys(){
for(var i:Number=0; i<10; i++){
var t:Tile = new Tile() //Display object in the library
t.x = i * 50
t.y = i * 50
addChild(t)
displays.push(t)
}
for(var j:Number=0; j<10; j++){
//some code
displays.push(t)
}
for(var k:Number=0;k<10; k++){
//some codes
displays.push(t)
}
}
addEventListener(Event.ENTER_FRAME, loop)
function loop(e:Event){
for each (var displayAsset:DisplayAsset in displays)
{
displayAsset.onEnterFrame();
}
}
Upvotes: 3
Reputation: 7963
What about extending those classes (Tile, Npc, Building), from one basic class - let's say TypeClass, which has an overwritable method getType().
This method returns "None" by default, but by expanding the class and overwriting the method it can return "Tile", "Npc" or "Building"...
With this, it is possible to make switch statements, and basically easier to manage code...
switch (anObject.getType()){
case "etc"...
}
Upvotes: 0
Reputation: 3119
If performance is what you are after, you should be using Vectors instead.
Upvotes: 1