我创建了这个简单的示例,因为我使用了一个更复杂的类,一个菜单项,我想初始化 Main 类中的所有设置,然后在需要时将其添加到 Game 类中(并更新它)(两个类是分开的)
类:Main(文档类,(理想情况下)是初始化/创建所有内容的地方)
public class Main extends MovieClip
{
//testing passing on reference to Game
private var testBitmap:Bitmap;
private var testBitmapData:BitmapData;
private var testArray:Array;
public function Main():void
{
testBitmapData = new BitmapData(256, 256, false, 0xCCDDEE);
testBitmap = new Bitmap(testBitmapData);
testArray = [];
testArray.push(testBitmap); //Array for reference
game = new Game(540, 960, testArray);
//create Game class, pass reference to Object
game.x = 0;
game.y = 0;
}
}
类:游戏(由文档类创建,(理想情况下)是一切运行的地方)
public class Game extends MovieClip
{
private var testingArray:Array
public function Game(gameWidth:int, gameHeight:int, testArray:Array):void
{
this.testingArray = testArray; //assign to local Array and access
addChild(testingArray[0]);
//addChild to Game from an item intialised in Main, doesn't work >___<
}
}
.
.
.
问题是,在我原来的游戏课上;它接收缓存的 BitmapData 的初始包和一个列表数组,告诉它它需要在此处循环通过削减的 BitmapData(并且该引用仅用于更新作品(如果我已经在 Main 中添加了Child):
public function Game(gameWidth:int, gameHeight:int, cachedBitmapClipArray:Array)
{
this.cachedBitmapClipArray = cachedBitmapClipArray;
private function update():void
{
for each (tempCachedBitmapClip in cachedBitmapClipArray)
{
tempCachedBitmapClip.updateCurrentTile();
//but updating through the reference to an item initialised in Main works !!
}
}
}
.
如何使引用和传入的对象(或有权访问)的行为与原始实例一样?(能够添加孩子)
即对象可以跨越“范围”(??)还是应该只在它们已初始化的类中控制(实例化)对象