本文介绍了使用线程作为GameLoop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要做的是有一个循环,如果我想,我可以停止......我犯了一个新的线程,并把它命名为GameLoop我想这是尽我的位图的X和Y的变化,但是当我tryed运行我什么都没有移动时,它只是给我发送图片。我的继承人code,我做了什么错了?
公共GamePage(上下文的背景下,ATTRS的AttributeSet)
{
超(背景下,ATTRS);
//其他code在这里
螺纹gameThread =新主题(新GameLoop());
gameThread.start();
}公共类GameLoop实现Runnable
{
公共无效的run()
{
尝试{
视频下载(速度);
}赶上(InterruptedException的E){
// TODO自动生成catch块
e.printStackTrace();
} Log.d(LoopRunning,游戏循环正在运行!); 开关((INT)possition)
{
情况下0:
如果(计数器< 110){SUNY - ;反++;}
如果(计数器> = 110安培;&安培;计数器< 240){SUNY - ; SUNX ++;反++;}
如果(计数器> = 240安培;&安培;计数器< 360){SUNY - = 0.5; SUNX ++;反++;}
如果(计数器> = 360安培;&安培;计数器< 662){SUNY - = 0.333; SUNX ++;反++;}
如果(计数器> = 662&安培;&安培;计数器< 1004){SUNY + = 0.333; SUNX ++;反++;}
如果(计数器> = 1004安培;&安培;计数器< 1104){SUNY + = 0.5; SUNX ++;反++;}
如果(计数器> = 1104&放大器;&安培;计数器< 1224){SUNY ++; SUNX ++;反++;}
如果(计数器> = 1224&放大器;&安培;计数器< 1345){SUNY ++;反++;}
打破; 情况1:
如果(ZombieX&下; canvasWidth / 2 + 300)ZombieX + = 10; 如果(计数器2< 110){发呆 - ;计数器2 ++;}
如果(C2的> = 110安培;&安培; C2的< 240){发呆 - ; MoonX ++;计数器2 ++;}
如果(C2的> = 240安培;&安培; C2的< 360){发呆 - = 0.5; MoonX ++;计数器2 ++;}
如果(C2的> = 360安培;&安培; C2的< 662){发呆 - = 0.333; MoonX ++;计数器2 ++;}
如果(C2的> = 662&安培;&安培; C2的< 1004){发呆+ = 0.333; MoonX ++;计数器2 ++;}
如果(C2的> = 1004安培;&安培; C2的< 1104){发呆+ = 0.5; MoonX ++;计数器2 ++;}
如果(C2的> = 1104&放大器;&安培; C2的< 1224){发呆++; MoonX ++;计数器2 ++;}
如果(C2的> = 1224&放大器;&安培; C2的< 1345){发呆++;计数器2 ++;}
打破;
}
}
}
解决方案
使用两个嵌套while循环像这样:
@覆盖公共无效的run(){
而(gameShouldRun){
而(iterateGame){
//做游戏的东西
}
}
}
此外,运行
默认情况下不循环。如果你想在游戏循环,直到停止,则需要在run方法while循环。
What I need to do is have a loop that I can stop if I want... I made a new thread and named it GameLoop I want this to do all of my bitmap X and Y changes but when I tryed to run what I have nothing moves, It just sends me a picture. Heres my code, what did I do wrong?
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;
}
}
}
解决方案
Use two nested while loops as so:
@Override public void run() {
while (gameShouldRun) {
while (iterateGame) {
// do game stuff
}
}
}
Also, run
is not looped by default. If you want the game to loop until stopped, you will need a while loop in the run method.
这篇关于使用线程作为GameLoop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!