我首先要感谢你们所有人。
我已经使用Runnable创建了一个生成sprite的代码。 但它只执行我写的最后一次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;
}
}
以下是 Runnable 代码:
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()方法时, 只执行最后一次生成的方法。
请帮帮我。 谢谢。
答案 0 :(得分:0)
显然只有一个Runnable的地方。我相信我有一个解决方法。创建一个类RunnableBatch,其实例将包含一个Runnables数组。它将有一个方法add(Runnable r)
1)将Runnable添加到数组和
2)创建一个包含for (Runnable r : runnableArray) {r.run();}