【发布时间】:2013-11-24 15:02:47
【问题描述】:
所以我有这个超类网格类,以及名为 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 和其他我能想到的解决方法,但都失败了。有什么想法吗?
【问题讨论】:
-
我设置了一个简单的 .fla 并设置了文件,因为我假设你根据你所说的做了,我没有出错。
-
查看我的更新答案。如果您在构造函数中进行跟踪以确保您期望发生的事情实际上正在发生,那就太好了。我不需要调用 super,它对我来说效果很好。
标签: actionscript-3 types subclass superclass coercion