控制在AndEngine中将精灵添加到场景的概率

时间:2012-07-03 00:59:59

标签: java android sprite andengine

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

我有四个基于较高百分比挑选的精灵。我希望能够在整个游戏过程中改变百分比。每次调用方法时,我都需要将一个精灵添加到场景中。我有一个计时器,大约每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;
            }
        }
    }
}

1 个答案:

答案 0 :(得分: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;
            }
        }
    }
}