我正在尝试控制选择精灵以添加到我的游戏场景中的百分比。
我有四个根据较高百分比挑选的精灵。我希望能够在整个游戏过程中更改百分比。每次调用该方法时,我都需要将精灵添加到场景中。我有一个计时器,它大约每 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;
}
}
}
}