0

代码很好地说明了这个问题,我已经把所有不相关的东西都删掉了。

游戏开始时创建2-4个亲属

function createInitialKinsmen()
{
    for (var k:int = 1; k < 3 + Math.round(Math.random() * 2; k++)
    {
        if (k == 1)
        {
            createKinsmen(0);
            trace ("player");
        }
        else
        {
            createKinsmen(1);
            trace ("starting kinsmen");
        }
    }
}

亲属创造功能

function createKinsmen(arrivalTypeVar:int)
{
    var newKinsmen = new kinsmen;
    listPanel.kinsmenDivider.addChild(newKinsmen);
    totalKinsmen++;
    totalKinsmenAlive++;
    newKinsmen.name = "kinsmen" + totalKinsmen;
    newKinsmen.arrivalType = arrivalTypeVar;
}

亲属班

public class kinsmen extends MovieClip
{
    var arrivalType:int;
    function kinsmen()
    {
        trace(this.name);
        if (this.arrivalType = 0)
        {
            trace("player");
        }
        if (this.arrivalType = 1)
        {
            trace("starting kinsmen");
        }
    }
}

输出应该说:

kinsmen1
player
player
kinsmen2
starting kinsmen
starting kinsmen
kinsmen3
starting kinsmen
starting kinsmen
kinsmen4
starting kinsmen
starting kinsmen

而是说:

kinsmen1
player
player
kinsmen2
player
starting kinsmen
kinsmen3
player
starting kinsmen
kinsmen4
player
starting kinsmen

这意味着没有传递arrivalType变量。似乎可以传递硬编码的变量,因此目前我在 kinsmen 电影剪辑中有一个带有 alpha 0 的正方形,它的 x 位置决定了构造函数中到达类型的值,但这肯定不是一个好习惯,是有更好的方法吗?

4

1 回答 1

1

我认为您只想修改kinsmen类的构造函数以接受arrivalType和的参数name

您当前正在做的是构造一个新kinsmen对象,然后在该对象上设置arrivalTypename。因此,当构造函数代码运行时,arrivalType是 an 的默认值,int即 0。

在类中像这样修改您的构造函数kinsmen

function kinsmen(arrivalType:int, name:String)
{
    this.name = name;
    this.arrivalType = arrivalType;
    if (this.arrivalType == 0)
    {
        trace("player");
    }
    if (this.arrivalType == 1)
    {
        trace("starting kinsmen");
    }
}

并修改您的createKinsmen()函数,使其将值传递arrivalType 给构造函数方法,而不是在创建对象后设置arrivalTypeand :name

function createKinsmen(arrivalTypeVar:int)
{
    totalKinsmen++;
    totalKinsmenAlive++;
    var newKinsmen = new kinsmen(arrivalTypeVar, "kinsmen" + totalKinsmen);
    listPanel.kinsmenDivider.addChild(newKinsmen);
}
于 2013-02-12T01:42:43.660 回答