How to Convert one Vector Data to another Vector Data in Actionscript 3.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 iga开发者_StackOverflow社区mes: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]);
}
There is a more convenient way to do that.
var igames:Vector.<IGame> = Vector.<IGame>(shootGames);
Notice that when you use "Vector.< IGame >(shootgames)" you are not making a typecast, instead you are creating a new Vector instance and populating it with the content of the "shootGames" Vector.
For more detailed information, go here.
Sample code:
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
精彩评论