我正在AS3中创建一个隐藏的对象游戏。在大多数情况下,它正常工作。我看到的唯一问题是我希望代码将十个对象完全拉入单词列表,以便用户必须在场景中找到。我希望列表也是随机的,所以游戏不会太平凡。现在,我在场景中有大约30个对象,但如果它完全拉动,它只会向单词列表中拉出最多三个对象。谁能告诉我哪里出错了?
var obArr:Array = new Array();
var randArray:Array = new Array();
var chkCnt:Number=10;
stage.addEventListener(Event.ENTER_FRAME, setupStage);
stage.addEventListener(MouseEvent.CLICK, clickOb);
//set up current stage
function setupStage(e:Event) {
chkCnt=10;
randArray.length=0;
obArr=[];
//count the objects on stage
for (var n=0; n<gb1.numChildren; n++) {
//get the children
var ob=gb1.getChildAt(n);
//only take movie clips
if (ob is MovieClip) {
//only count the movie clips that have name declared
if (ob.myname!=null) {
//push to array
obArr.push(MovieClip(ob));
}
}
}
//clear the list
nameslist.text="";
//build objects list
for (n=0; n<obArr.length; n++) {
//add the name of object
nameslist.appendText(String (obArr[n].myname));
nameslist.appendText("\n\n");
}
//this is not needed anymore;
stage.removeEventListener(Event.ENTER_FRAME, setupStage);
}
//stage was clicked;
function clickOb(e:MouseEvent) {
var clicked=false;
//check which object was clicked
for (var n=0; n<obArr.length; n++) {
//add the name of object
if (obArr[n].hitTestPoint(mouseX,mouseY,true)) {
//object is clicked
clicked=true;
//hide
obArr[n].visible=false;
//play sound
ping.play();
//remove from array
obArr.splice(n,1);
}
}
//rebuild text list
if (clicked) {
//clear the list
nameslist.text="";
//build objects list
for (n=0; n<obArr.length; n++) {
//add the name of object
nameslist.appendText(String (obArr[n].myname));
nameslist.appendText("\n\n");
}
//check if array is empty meaning all objects were removed;
if (n==0) {
GB2Unlock.visible=true;
}
}
}
答案 0 :(得分:0)
尝试列出 obArr 中的对象,例如:
trace(obArr.length);
for (var i:int = 0; i < obArr.length; i++) {
trace(obArr.name, obArr.myname);
}
请记住, getChildAt 仅返回执行时父母的子项上的对象。因此,如果您稍后将某个对象添加到舞台上,例如在另一个帧上添加,则您的方法将无法找到这些对象。