2011-03-11 42 views
0
Class ShootGame implements IGame 
{ 

} 

//ShootGame Vector 

var shootGames:Vector.<ShootGame> = new Vector.<ShootGame>(); 
for(i=0;i<20;i++) 
{ 
    shootGames.push(new ShootGame()); 
} 



var igames:Vector.<IGame> = //What is the Simplest method to convert ShootGame to IGame Vector 

//I am doing like 

igames = new Vector.<IGame>(); 

for(i=0;i < shootGames.length;i++) 
{ 

igames.push(shootGames[i]); 

} 
+0

一些文本是不可见的,VAR igames:矢量。 < IGame > =新的矢量。 < IGame >(); – particle 2011-03-11 10:06:05

回答

1

有一个更方便的方法来做到这一点。

var igames:Vector.<IGame> = Vector.<IGame>(shootGames); 

注意,当您使用“矢量。<的iGame>(shootgames)”你是不是做一个类型转换,而是要创建一个新的Vector实例,并与“shootGames” Vector的内容填充它。

有关更多详细信息,请转至here

示例代码:

var shootGames:Vector.<ShootGame> = new Vector.<ShootGame>(); 
for (var i:int = 0; i < 20; i++) { 
    shootGames.push(new ShootGame()); 
} 

var igames:Vector.<IGame> = Vector.<IGame>(shootGames); 

trace("shootGames.length", shootGames.length); //20 
trace("igames.length", igames.length); //20 
+0

当我做var igames:Vector。 =矢量。 (shootGames);它仍然显示igames长度为零。 – particle 2011-03-13 05:04:48

+0

你确定shootGames矢量不是空的吗?我已经给答案添加了一个示例代码;当我运行它时,我得到了两个矢量的相同长度。 – bmleite 2011-03-13 12:22:04

+0

谢谢,我错误地使用了Vector的新关键字infront,它创建了这个错误。 – particle 2011-03-13 14:27:32

相关问题