所以我有这个超类网格类,以及名为 GrassTile1、GrassTile2 等的网格类的子类……所有子类的实例都存储在一个数组中。我想如何将子类的实例转换为引用数组的超类?
private var backgroundGrid = []; //the array which the grids are stored in, in the main class.
public class Grid extends MovieClip
{
protected var node :PathfindNode; //the variable I wish to access, from an instance of subclass.
public function Grid(){
node = new PathfindNode();
}
}
public class GrassTile1 extends Grid { //every subclass of Grid will extends Grid
public function GrassTile1() {
// constructor code
}
}
function getBackgroundGrid(i:int,j:int):Grid{ //in the main class
return Grid(backgroundGrid[i][j]); // this line gives me an error
}
TypeError:错误 #1034:类型强制失败:无法将 GrassTile1@2905d5f1 转换为 Grid。
我尝试访问 backgroundGrid[i][j].node 和其他我能想到的解决方法,但都失败了。任何想法?