我需要做的是有一个循环,我可以停止,如果我想...我创建了一个新的线程并将其命名为GameLoop我希望这可以完成我的所有位图X和Y更改但是当我尝试运行时我什么都没动,它只是给我发了一张照片。继承我的代码,我做错了什么?
public GamePage(Context context, AttributeSet attrs)
{
super(context, attrs);
//other code here
Thread gameThread = new Thread(new GameLoop());
gameThread.start();
}
public class GameLoop implements Runnable
{
public void run()
{
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("LoopRunning", "The Game Loop Is Running!");
switch ((int)possition)
{
case 0:
if (counter < 110) {SunY --; counter++;}
if (counter >= 110 && counter < 240) {SunY --; SunX ++; counter++;}
if (counter >= 240 && counter < 360) {SunY -= 0.5; SunX ++; counter++;}
if (counter >= 360 && counter < 662) {SunY -= 0.333; SunX ++; counter++;}
if (counter >= 662 && counter < 1004) {SunY += 0.333; SunX++; counter++;}
if (counter >= 1004 && counter < 1104) {SunY += 0.5; SunX++; counter++;}
if (counter >= 1104 && counter < 1224) {SunY ++; SunX++; counter++;}
if (counter >= 1224 && counter < 1345) {SunY ++; counter++;}
break;
case 1:
if (ZombieX < canvasWidth/2 + 300) ZombieX += 10;
if (counter2 < 110) {MoonY --; counter2++;}
if (counter2 >= 110 && counter2 < 240) {MoonY --; MoonX ++; counter2++;}
if (counter2 >= 240 && counter2 < 360) {MoonY -= 0.5; MoonX ++; counter2++;}
if (counter2 >= 360 && counter2 < 662) {MoonY -= 0.333; MoonX ++; counter2++;}
if (counter2 >= 662 && counter2 < 1004) {MoonY += 0.333; MoonX++; counter2++;}
if (counter2 >= 1004 && counter2 < 1104) {MoonY += 0.5; MoonX++; counter2++;}
if (counter2 >= 1104 && counter2 < 1224) {MoonY ++; MoonX++; counter2++;}
if (counter2 >= 1224 && counter2 < 1345) {MoonY ++; counter2++;}
break;
}
}
}
答案 0 :(得分:1)
使用两个嵌套的while循环:
@Override public void run() {
while (gameShouldRun) {
while (iterateGame) {
// do game stuff
}
}
}
此外,run
默认情况下不会循环播放。如果你想让游戏循环直到停止,你需要在run方法中使用while循环。