0

我想先对大家说声谢谢。。

我已经编写了一个代码来使用 Runnable 生成精灵。但它只执行我写的最后一个可运行文件。

这是代码:

public class LevellingManager {

// ======================== //
// ======================== //
// THE PATH METHOD              //
// ======================== //
// ======================== //  
public static void path() {
    currType = Leveling2.TYPE_DIGGER;
    mHandler.post(mStartDigger);
    // === NOT EXECUTED === //
    generateSprite(Leveling2.TYPE_ROCK, 10, 2000, 2000, LevellingManager.EASY);
    // === EXECUTED === //
    generateSprite(Leveling2.TYPE_WORM, 8, 3000, 1000, LevellingManager.HARD);
}

generateSprite( )方法

    public static void generateSprite(int type, int nSprts, int delayPerSprt,
        int delayPerBtch, int[] difficulty) {
    currType = type;

    switch (type) {
    case Leveling2.TYPE_ROCK:
        System.out.println("Awalnya : " + GameResources2.lastIndexRock);

        // ---- Set Generate Data ---- //
        currIndex = 1 + GameResources2.lastIndexRock;
        currList= GameResources2.listRockToBeAdded;
        currLastIndex = GameResources2.lastIndexRock;
        currMaxCpcty= GameResources2.MAX_ROCK;

        // --- Method to Set the position, speed & delay per sprite ---- //
        set(nSprts, delayPerBtch, delayPerSprt, difficulty);

        mHandler.postDelayed(mStartRock, currDelay);

        GameResources2.lastIndexRock = currLastIndex;

        break;

    case Leveling2.TYPE_WORM:
        // ---- Set Generate Data ---- //
        currIndex = 1 + GameResources2.lastIndexWorm;
        currList=GameResources2.listWormToBeAdded;
        currLastIndex = GameResources2.lastIndexWorm;
        currMaxCpcty = GameResources2.MAX_WORM;

        // --- Method to Set the position, speed & delay per sprite ---- //
        set(nSprts, delayPerBtch, delayPerSprite, difficulty);

        mHandler.postDelayed(mStartWorm, currDelay);

        GameResources2.lastIndexWorm = currLastIndex;
        break;      
    }
}

这是可运行代码:

    public static Runnable mStartRock = new Runnable() {
    public void run() {
        installAttach(this);
    }
};
public  static Runnable mStartWorm = new Runnable() {
    public void run() {
        installAttach(this);
    }
};

这是一个将在 Runnable 中执行的方法。 installAttach()方法:

    private static void installAttach(Runnable r) {

    System.out.println("Lewat sini, type : " + currType);

    currSpriteGen = currList.get(currIndex);

    currSpriteGen
            .registerEntityModifier(new SequenceEntityModifier(
                    new MoveModifier(currSpeed,
                            currSpriteGen.getX(),
                            currSpriteGen.getX(),
                            GameResources2.CAMERA_HEIGHT,
                            GameResources2.POSITION_DIGGER_HEIGHT)));

    currSpriteGen.setVisible(true);

    if (currSpriteGen.getParent() == null)
        GameResources2.scene.attachChild(currSpriteGen);
    else
        currLastIndex = currIndex;

    switchLastIndex();

    if (currIndex == currMaxCpcty - 1) {
        currNSprts = currNSprts - currIndex;
        currIndex = -1;
    }

    System.out.println("Index : " + currIndex);
    System.out.println("Max index : " + currMaxCpcty);

    ++currIndex;

    // --- For looping ---//
    if (currIndex < currNSprts) {
        mHandler.postDelayed(r, currDelay);
    }
}   

所以当我在onLoadScene() 调用 LevellingManager.path()方法时,只有最后一个执行的 generate 方法。

请帮我。谢谢你。

4

1 回答 1

0

显然只有一个 Runnable 有一个地方。我相信我有一个解决方法。创建一个类 RunnableBatch,其实例将包含一个 Runnables 数组。它将有一个add(Runnable r)方法

1) 将 Runnable 添加到数组中并

2)创建一个新的 Runnable 将包含类似for (Runnable r : runnableArray) {r.run();}

于 2012-07-24T13:51:27.837 回答