我遇到在外部类“Level”中创建的“DiamondEnemy”对象没有出现在舞台上的问题。我试图每隔一段时间从“EnemyNotReleasedArray”中检索一个随机敌人,并通过“enemyOnScreen”精灵将它们添加到屏幕上。
请注意我没有100%完成所有功能;所以看起来有点奇怪。我不想再进一步,直到我能真正开始工作。
更新:我从名为“main”的单独文档类创建一个新的“级别”对象。
package {
import DiamondEnemy;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.display.Sprite;
import flash.display.MovieClip;
public class Level extends MovieClip {
private const START_DELAY_SECONDS:uint = 1000;
private var EnemyNotReleasedArray:Array = new Array();
private var startDelay:Timer;
private var enemyOnScreen: Sprite;
public function Level(NumberDiamonds:uint)
{
// load the required enemies into the array
loadEnemyArray(NumberDiamonds);
//setup up sprite, for enemies that will appear on the screen
enemyOnScreen = new Sprite();
addChildAt(enemyOnScreen, numChildren);
// create delay timer before enemies can start appearing on screen
startDelay = new Timer(START_DELAY_SECONDS,1);
// set eventlistener that once delay finishes
startDelay.addEventListener(TimerEvent.TIMER_COMPLETE, releaseRandomEnemy);
startDelay.start();
//setup up sprite, for enemies that will appear on the screen
enemyOnScreen = new Sprite();
addChild(enemyOnScreen);
}
// creates the requested number of enemies type into EnemyNotReleasedArray so they can be released later
private function loadEnemyArray(numDiamonds:uint)
{
// use requested number diamonds enemies - to create diamond enemy objects
for (var i:uint = 0; i < numDiamonds; i++)
{
var diamond:DiamondEnemy = new DiamondEnemy();
EnemyNotReleasedArray.push(diamond);
}
}
// selects a random enemy from EnemyNotReleasedArray and resizes the array so enemy is no longer in it
private function releaseRandomEnemy(evt:TimerEvent)
{
var arrayLength:uint = EnemyNotReleasedArray.length;
// check make sure array is not empty, if empy level is over
if (arrayLength > 0)
{
var randomArrayIndex = Math.ceil(Math.random() * arrayLength) -1;
/// adding random enemy to sprite object
enemyOnScreen.addChild(EnemyNotReleasedArray[randomArrayIndex]);
trace(EnemyNotReleasedArray[randomArrayIndex]);
//remove the enemy from array and make element null
EnemyNotReleasedArray.removeAt(randomArrayIndex)
//tempory array to store non-null values
var tempArray:Array = new Array();
// cycle through EnemyNotReleasedArray and store all values that are not null into temp array
for each(var enemy in EnemyNotReleasedArray)
{
if (enemy != null)
{
tempArray.push(enemy)
}
}
// save temp array value into EnemyNotReleasedArray
EnemyNotReleasedArray = tempArray;
}
else
{
trace("no more enemies left in array");
}
}
}
}
文档类“Main”:
package {
import Level;
import DiamondEnemy;
import flash.display.MovieClip;
public class Main extends MovieClip
{
public function Main()
{
var level:Level = new Level(1);
}
}
}
答案 0 :(得分:2)
显示列表是一个分层图,通常称为树。
显示直接或间接连接到根节点的所有内容。根节点是Stage
对象。虽然可能,但您自己的代码实际上不应该addChild()
到此对象。 (出于本回答范围之外的原因)
Stage
的唯一子项是执行.swf文件时创建的文档类的实例。此实例也会自动添加到Stage
对象中,这就是为什么您永远不必将文档类添加到任何内容,但它仍然可见。
Main
类的构造函数如下所示:
public function Main()
{
var level:Level = new Level(1);
}
问题在于,当您成功创建Level
对象时,它永远不会添加到上面通常称为&#34;显示列表&#34;的上述层次结构中。 level
未连接到根节点,这就是它不显示的原因。您仍然可以将子项添加到level
,但出于同样的原因,它们不会被显示:即level
不可见。
要解决此问题,请将level
添加到您的文档类中,如下所示:
public function Main()
{
var level:Level = new Level(1);
addChild(level);
}
顺便说一下。你有两次这个代码:
//setup up sprite, for enemies that will appear on the screen
enemyOnScreen = new Sprite();
addChildAt(enemyOnScreen, numChildren);
和
//setup up sprite, for enemies that will appear on the screen
enemyOnScreen = new Sprite();
addChild(enemyOnScreen);
但你只需要一次。第二个是你所需要的。
你的两个班级中的任何一个都不应extends MovieClip
,因为他们没有时间线。除非您实际处理MovieClip,否则请使用extends Sprite
。