无法识别数组中的 Actionscript 3 子项

Actionscript 3 child from array not recognized

我是 actionscript 的新手,我仍在尝试找出数组和 classes。我正在制作一个类似于 Shinobi 的奖励回合的游戏,但我使用的球棒会飞入舞台,垂直飞出舞台,然后落回舞台但距离更近。这是将蝙蝠添加到场景的代码:

package  {
import flash.display.MovieClip;
import flashx.textLayout.formats.BackgroundColor;
import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;

public class PlayScreen extends MovieClip
{
    public var background:Background;
    public var batArmy:Array;
    public var addBatTimer:Timer;

    public function PlayScreen() 
    {
        background = new Background
        background.x = 0;
        background.y = 0;
        addChild( background );
        batArmy = new Array();
        var newBat = new Bat( 480, -50);
        batArmy.push (newBat);
        addChild (newBat);
        addBatTimer = new Timer(7000,2)
        addBatTimer.addEventListener(TimerEvent.TIMER, addBat);
        addBatTimer.start();
    }
    public function addBat( e:Event ):void
    {
        var newBat = new Bat( 480, -50);
        batArmy.push ( newBat )
        addChild (newBat);
    }
}

蝙蝠的所有动作都在另一个class叫做蝙蝠。

我有两个问题:

问题 #1:近距离的蝙蝠仍会落后于较远的蝙蝠。

问题 #2:我将 setChildIndex(PlayScreen.newBat, 0) 添加到 Class Bat 和 setChildIndex(PlayScreen.newBat, 1) 的构造函数代码中,当蝙蝠靠近但我得到 "Access of possibly undefined property newBat through a reference with static type Class."

似乎 newBat 是在 PlayScreen class 中定义的,所以我不确定我做错了什么。有任何想法吗?就像我之前说过的,我对此很陌生,而且几乎是自学的,所以我可能会做一些完全错误的事情。谢谢!

PlayScreen.newBat 表示尝试访问 PlayScreen class 中的 public static var newBatnewBat 变量是在函数内部本地创建的,因此它仅在那里可见。

据我了解第一个问题——你新添加的蝙蝠需要落后于之前添加的蝙蝠:

addChild (newBat);
setChildIndex(newBat, 0);

addBat 函数中应该有所帮助。

当蝙蝠靠近时(这似乎是在 Bat class 内部处理的)如果你只需要将它添加到顶部深度:

if(parent){//I prefer checking it - just in case
    parent.setChildIndex(this, parent.numChildren - 1);
}

另一种方法是在您的 PlayerScreen 中为每个蝙蝠距离创建一个子层 Sprite,并在它接近时将每个蝙蝠添加到 Sprite

更新
要使 PlayScreen 实例对 Bat 可见,您需要在那里传递引用。最简单的方法之一是向 Bat 构造函数再添加一个参数,如下所示:
Bat class:

private var _screen: PlayerScreen;
public function Bat(xVal:int, yVal:int, screen:PlayerScreen){
    //...
    _screen = screen;
} 

PlayerScreen class:

var newBat = new Bat( 480, -50, this);

并且 PlayerScreen 的每个 public 成员都可以从 Bat 获得,例如 e。 G。 _screen.background