我正在尝试开发游戏app.I实现onResume和onPause方法。 onPause方法工作正常,但onResume方法崩溃。
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
resume();
}
public void resume()
{
addAnime();
myThread.setRunning(true);
myThread.start();
}
如果我没有在onResume方法中调用更新myApp工作正常,但如果我暂停游戏并再次恢复它,模拟器说app没有响应。
如果我在onResume方法中调用resume,则表示canvas在第一次运行时为空
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
view.resume();
}
当我在onResume上调用简历时,我不明白为什么画布在应用程序开始时为空。有人解释我吗?
修改 puase():
public void pause()
{
boolean repaet=true;
while(repaet)
{
try {
myThread.join();
myThread.setRunning(false);
repaet=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myThread=null;
}
EDIT2 MyThread的
public void run() {
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (running) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
EDIT3 这是我的set runnig函数
public void setRunning(boolean run) {
running = run;
}
答案 0 :(得分:0)
暂停方法:
public void pause()
{
boolean repeat = myThread != null;
while(repeat)
{
try {
myThread.setRunning(false);
myThread.join();
repeat=false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
myThread=null;
}
myThread:
public class gameThread extends Thread {
private SurfaceView view;
private boolean isRunning;
private static final int FPS; // set FPS with your value
private void sleep(long time) throws InterruptedException
{
Thread.sleep(time);
}
public void setRunning(boolean running)
{
synchronized(this)
{
this.isRunning = running;
}
}
public boolean isRunning()
{
synchronized(this)
{
return this.isRunning;
}
}
public void run()
{
long ticksPS = 1000 / FPS;
long startTime;
long sleepTime;
while (isRunning()) {
Canvas c = null;
startTime = System.currentTimeMillis();
try {
c = view.getHolder().lockCanvas();
synchronized (view.getHolder()) {
view.onDraw(c);
}
} finally {
if (c != null) {
view.getHolder().unlockCanvasAndPost(c);
}
}
sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
try {
if (sleepTime > 0)
sleep(sleepTime);
else
sleep(10);
} catch (Exception e) {}
}
}
简历方法:
public void resume()
{
addAnime();
// you must create a new thread. (You cannot use this same thread if it end)
myThread = new gameThread(surfaceView);
myThread.setRunning(true);
myThread.start();
}
答案 1 :(得分:0)
您好,您可以在文档中找到更多信息。 SurfaceHolder.lockCanvas
开始编辑曲面中的像素。返回的Canvas可用于绘制到曲面的位图。如果尚未创建表面 ,或者无法编辑,则返回null。您通常需要实现 Callback.surfaceCreated 以找出Surface可供使用的时间。 在unlockCanvas()和lockCanvas()之间,Surface的内容永远不会保留,因此,必须写入Surface区域中的每个像素。此规则的唯一例外是指定脏矩形时,在这种情况下,将保留非脏像素。 如果在Surface未准备好时(Callback.surfaceCreated之前或Callback.surfaceDestroyed之后)重复调用此方法,则会将您的调用限制为慢速,以避免消耗CPU。
关于Threads或Runneables的使用,你可以检查一下。 https://guides.codepath.com/android/Repeating-Periodic-Tasks http://developer.android.com/guide/components/processes-and-threads.html