0

私がする必要があるのは、必要に応じて停止できるループを作成することです...新しいスレッドを作成し、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;
        }
    }
}
4

1 に答える 1

1

2つのネストされたwhileループを次のように使用します。

@Override public void run() {
    while (gameShouldRun) {
        while (iterateGame) {
            // do game stuff
        }
    }
}

また、runデフォルトではループされません。ゲームを停止するまでループさせたい場合は、runメソッドにwhileループが必要になります。

于 2012-06-05T20:21:18.923 に答える