为什么我的 For 循环不能在屏幕上创建多个对象?
Why doesn't my For loop create more than one object on the screen?
有点问题。我正在尝试向舞台添加 10 个项目,但它只添加了 1 个。对我做错了什么有任何见解吗?
public var numCells:Array = [];
public function addCell():void
{
var cell:Cell = new Cell();
var i:int = 0;
cell.x = Math.floor(Math.random() * 1366);
cell.y = Math.floor(Math.random() * 768);
for(var i:int = 0; i < 10; i++)
{
numCells.push(cell);
addChild(cell);
}
return;
}
您只会实例化一个单元格。使用传入的同一对象第二次调用 addChild
不会复制该对象,它只是将其移动到最顶部 'layer'.
您需要在 for 循环中实例化一个新单元格。像这样:
private function addCells():void {
for(var i:int = 0; i < 10; i++){
var cell:Cell = new Cell();
cell.x = Math.floor(Math.random() * 1366);
cell.y = Math.floor(Math.random() * 768);
numCells.push(cell);
addChild(cell);
}
}
有点问题。我正在尝试向舞台添加 10 个项目,但它只添加了 1 个。对我做错了什么有任何见解吗?
public var numCells:Array = [];
public function addCell():void
{
var cell:Cell = new Cell();
var i:int = 0;
cell.x = Math.floor(Math.random() * 1366);
cell.y = Math.floor(Math.random() * 768);
for(var i:int = 0; i < 10; i++)
{
numCells.push(cell);
addChild(cell);
}
return;
}
您只会实例化一个单元格。使用传入的同一对象第二次调用 addChild
不会复制该对象,它只是将其移动到最顶部 'layer'.
您需要在 for 循环中实例化一个新单元格。像这样:
private function addCells():void {
for(var i:int = 0; i < 10; i++){
var cell:Cell = new Cell();
cell.x = Math.floor(Math.random() * 1366);
cell.y = Math.floor(Math.random() * 768);
numCells.push(cell);
addChild(cell);
}
}