-1

我正在尝试控制选择精灵以添加到我的游戏场景中的百分比。

我有四个根据较高百分比挑选的精灵。我希望能够在整个游戏过程中更改百分比。每次调用该方法时,我都需要将精灵添加到场景中。我有一个计时器,它大约每 2 秒调用一次该方法。我需要每次根据百分比添加精灵。

这是我到目前为止所拥有的,但无法正常工作。增加百分比不会增加添加精灵的概率。有时它会增加错误的值,有时根本不起作用。我知道必须有更好更简单的方法来做到这一点。

有没有人有任何想法?

这是我在场景中添加精灵的方法

private void addSprite() {
    switch (pickSprite()) {
    // Persons Sprite with area touched code
    case 1:
        red_Sprite = red_pool.obtainPoolItem();
        SequenceEntityModifier loop1 = new SequenceEntityModifier(
                    new RotationModifier(.5f, 0, -5),
                    new RotationModifier(.5f, -5, 5),
                    new RotationModifier(.5f, 5, 0));
        red_Sprite.registerEntityModifier(new LoopEntityModifier(loop1));
        scene.registerTouchArea(red_Sprite);
        if (!red_Sprite.hasParent())
            scene.getFirstChild().attachChild(red_Sprite);
        else
            red_Sprite.setVisible(true);
        red_personList.add(red_Sprite);
        break;
    case 2:
    // Persons Sprite with area touched code
        green_Sprite = green_pool.obtainPoolItem();
        SequenceEntityModifier loop2 = new SequenceEntityModifier(
                    new RotationModifier(.5f, 0, -5),
                    new RotationModifier(.5f, -5, 5),
                    new RotationModifier(.5f, 5, 0));
        green_Sprite.registerEntityModifier(new LoopEntityModifier(loop2));
        scene.registerTouchArea(green_Sprite);
        if (!green_Sprite.hasParent())
            scene.getFirstChild().attachChild(green_Sprite);
        else
            green_Sprite.setVisible(true);
        green_personList.add(green_Sprite);
        break;
    case 3:
    // Persons Sprite with area touched code
        orange_Sprite = orange_pool.obtainPoolItem();
        SequenceEntityModifier loop = new SequenceEntityModifier(
                    new RotationModifier(.5f, 0, -5),
                    new RotationModifier(.5f, -5, 5),
                    new RotationModifier(.5f, 5, 0));
        orange_Sprite.registerEntityModifier(new LoopEntityModifier(loop));
        scene.registerTouchArea(orange_Sprite);
        if (!orange_Sprite.hasParent())
            scene.getFirstChild().attachChild(orange_Sprite);
        else
            orange_Sprite.setVisible(true);
        orange_personList.add(orange_Sprite);
        break;
    case 4:
    // Persons Sprite with area touched code
        blue_Sprite = blue_pool.obtainPoolItem();
        SequenceEntityModifier loop4 = new SequenceEntityModifier(
                    new RotationModifier(.5f, 0, -5),
                    new RotationModifier(.5f, -5, 5),
                    new RotationModifier(.5f, 5, 0));
        blue_Sprite.registerEntityModifier(new LoopEntityModifier(loop4));
        scene.registerTouchArea(blue_Sprite);
        if (!blue_Sprite.hasParent())
            scene.getFirstChild().attachChild(blue_Sprite);
        else
            blue_Sprite.setVisible(true);
        blue_personList.add(blue_Sprite);
        break;
    }

这是我的选择精灵方法:

public int pickSprite() {
    double rand = Math.random();
    for (int i = 0; i < mOdds.length; i++) {
        if (rand < mOdds[i])
            return i;
        rand -= mOdds[i];
    }
    return -1; // should never reach this
}

这是我尝试增加添加精灵的几率或百分比的方法。

public void increaseOdds(int index, double value) {
    double valueFromIndex = mOdds[index];
    mOdds[index] += value;
    // Make sure odds don't turn mOdds to less than zero
    if (mOdds[index] < 0.0) {
        value = value - mOdds[index];
        mOdds[index] = 0.0;
    } else if (mOdds[index] > 1.0) {
        value = mOdds[index] - 1;
        mOdds[index] = 1.0;
    }
    double changeToOthers = value / 3;
    double extraForNext = 0.0f;
    for (int i = 0; i < mOdds.length; i++) {
        if (i != index) {
            mOdds[i] -= changeToOthers;
            if (mOdds[i] < 0) {
                extraForNext = mOdds[i];
                mOdds[i] = 0.0;
            } else if (mOdds[i] > 1) {
                extraForNext = 1 - mOdds[i];
                mOdds[i] = 1.0;
            } else {
                mOdds[i] += extraForNext;
            }
        }
    }
}
4

1 回答 1

2

increaseOdds()我可以看到有几件事会导致问题。假设您在 , 上调用它index 0value 0.6并且mOdds[0]当前等于 0.8。然后,您的代码将两者相加,给出mOdds[0] = 1.4. 然后你会减去 1,赋值value = 0.4,即使你实际上只改变了 0.2。所以你最终将 0.2 添加到索引中,并为其他三个拆分 -0.4,导致它们不等于 1。

另外,如果最后一个元素是给你“额外”数量的元素,会发生什么?您需要检查并在遍历数组后将其分配给另一个。

试穿它的大小,看看它对你的效果如何。我在笔和纸上用一些奇数设计来测试它,它似乎站得住脚:

编辑:更改了一些代码。

public void changeOdds(int index, double value) 
{
    double old = mOdds[index];
            mOdds[index] = value;

    if(mOdds[index] > 1)
        mOdds[index] = 1;
    if(mOdds[index] < 0)
        mOdds[index] = 0;   

    double changeToOthers = (mOdds[index] - old) / -3.0;
    double extra = 0;
    for(int i=0;i<mOdds.length;i++)
    {
        if(i == index)
            continue;
        double add = changeToOthers + extra;
        if(mOdds[i] + add > 1)
        {
            extra = 1 - mOdds[i];
            mOdds[i] = 1;
        } else if(mOdds[i] + add < 0)
        {       
            extra = -mOdds[i];
            mOdds[i] = 0
        } else
        {
            mOdds += add;
            extra = 0;
        }
    }   
            // if there's still a negative extra, assign it to the first non-index that can take it and stop
    if(extra < 0)
    {
        for(int i=0;i<mOdds.length;i++)
        {
            if(i == index)
                continue;
            if(mOdds[i] + extra >= 0)
            {
                mOdds[i] += extra;
                extra = 0;
                break;
            }
        }
    }
            // ditto for positive extras
    if(extra > 0)
    {
        for(int i=0;i<mOdds.length;i++)
        {
            if(i == index)
                continue;
            if(mOdds[i] + extra <= 1)
            {
                mOdds[i] += extra;
                extra = 0;
                break;
            }
        }
    }
}
于 2012-07-03T02:51:07.553 回答