我正在制作一个简单的游戏循环和背景图像移动但我得到了黑屏。我的模拟器有问题吗?
Game.Java
public class Game extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//removing title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//setting contentview and passing this layout to it
setContentView(new GamePanel(this));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_game, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
游戏panel.java
public class GamePanel extends SurfaceView implements SurfaceHolder.Callback
{
public static final int WIDTH = 856;
public static final int HEIGHT = 480;
private MainThread thread;
private Background bg;
public GamePanel(Context context)
{
super(context);
//add the callback to the surfaceholder to intercept events
getHolder().addCallback(this);
thread = new MainThread(getHolder(), this);
//make gamePanel focusable so it can handle events
setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
boolean retry = true;
while(retry)
{
try{thread.setRunning(false);
thread.join();
}catch(InterruptedException e){e.printStackTrace();}
retry = false;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder){
bg = new Background(BitmapFactory.decodeResource(getResources(), R.drawable.grassbg1));
bg.setvector(-5);
//we can safely start the game loop
thread.setRunning(true);
thread.start();
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
return super.onTouchEvent(event);
}
public void update()
{
bg.update();
}
@Override
public void draw(Canvas canvas)
{
final float scaleFactorX = getWidth()/WIDTH;
final float scaleFactorY = getHeight()/HEIGHT;
if (canvas!=null) {
final int savedState = canvas.save();
canvas.scale(scaleFactorX, scaleFactorY);
bg.draw(canvas);
canvas.restoreToCount(savedState);
}
super.draw(canvas);
}
}
Background.java
public class Background {
private Bitmap image;
private int x,y,dx ;
public Background(Bitmap res)
{
image=res;
}
public void update()
{
x+=dx;
if(x<0)
{
x=0;
}
}
public void draw( Canvas canvas)
{
canvas.drawBitmap(image,x,y,null);
if(x<0)
{
canvas.drawBitmap(image,x+GamePanel.WIDTH,y,null);
}
}
public void setvector(int dx)
{
this.dx=dx;
}
}
MainThread.java
public class MainThread extends Thread
{
private int FPS = 30;
private double averageFPS;
private SurfaceHolder surfaceHolder;
private GamePanel gamePanel;
private boolean running;
public static Canvas canvas;
public MainThread(SurfaceHolder surfaceHolder, GamePanel gamePanel)
{
super();
this.surfaceHolder = surfaceHolder;
this.gamePanel = gamePanel;
}
@Override
public void run()
{
long startTime;
long timeMillis;
long waitTime;
long totalTime = 0;
int frameCount =0;
long targetTime = 1000/FPS;
while(running) {
startTime = System.nanoTime();
canvas = null;
//try locking the canvas for pixel editing
try {
canvas = this.surfaceHolder.lockCanvas();
synchronized (surfaceHolder) {
this.gamePanel.update();
this.gamePanel.draw(canvas);
}
} catch (Exception e) {
}
finally{
if(canvas!=null)
{
try {
surfaceHolder.unlockCanvasAndPost(canvas);
}
catch(Exception e){e.printStackTrace();}
}
}
timeMillis = (System.nanoTime() - startTime) / 1000000;
waitTime = targetTime-timeMillis;
try{
this.sleep(waitTime);
} catch(Exception e){}
totalTime += System.nanoTime()-startTime;
frameCount++;
if(frameCount == FPS)
{
averageFPS = 1000/((totalTime/frameCount)/1000000);
frameCount =0;
totalTime = 0;
System.out.println(averageFPS);
}
}
}
public void setRunning(boolean b)
{
running=b;
}
}
BackGround.java
public class Background {
private Bitmap image;
private int x,y,dx;
public Background(Bitmap res)
{
image=res;
}
public void update()
{
x+=dx;
if (x<0)
{
x=0;
}
}
public void draw( Canvas canvas)
{
canvas.drawBitmap(image,x,y,null);
if (x<0)
{
canvas.drawBitmap(image,x+GamePanel.WIDTH,y,null);
}
}
public void setvector(int dx)
{
this.dx=dx;
}
}
输出:
fahadpirzada.myfirstgame E/Surface: getSlotFromBufferLocked: unknown buffer: 0xac1790d0
11-07 00:36:20.540 15272-15279/com.example.fahadpirzada.myfirstgame W/art: Suspending all threads took: 87.622ms
11-07 00:37:11.150 15272-15279/com.example.fahadpirzada.myfirstgame W/art: Suspending all threads took: 5.025ms
11-07 00:43:29.528 15272-15279/com.example.fahadpirzada.myfirstgame W/art: Suspending all threads took: 17.819ms
11-07 00:43:47.556 15272-15279/com.example.fahadpirzada.myfirstgame W/art: Suspending all threads took: 5.342ms
11-07 00:45:31.867 15272-15279/com.example.fahadpirzada.myfirstgame W/art: Suspending all threads took: 25.416ms
11-07 00:45:48.921 15272-15279/com.example.fahadpirzada.myfirstgame W/art: Suspending all threads took: 25.667ms
11-07 00:46:42.139 15272-15279/com.example.fahadpirzada.myfirstgame W/art: Suspending all threads took: 63.547ms
11-07 00:46:54.088 15272-15283/com.example.fahadpirzada.myfirstgame I/art: CollectorTransition marksweep + semispace GC freed 5743(159KB) AllocSpace objects, 1(1608KB) LOS objects, 45% free, 611KB/1123KB, paused 46.409ms total 46.409ms
11-07 00:46:54.548 15272-15286/com.example.fahadpirzada.myfirstgame W/EGL_emulation: eglSurfaceAttrib not implemented
11-07 00:46:54.548 15272-15286/com.example.fahadpirzada.myfirstgame W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad9f54c0, error=EGL_SUCCESS
11-07 00:46:54.983 15272-15283/com.example.fahadpirzada.myfirstgame I/art: Background sticky concurrent mark sweep GC freed 99(9KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 2MB/2MB, paused 3.380ms total 237.021ms
11-07 00:46:55.123 15272-15272/com.example.fahadpirzada.myfirstgame I/Choreographer: Skipped 55 frames! The application may be doing too much work on its main thread.
11-07 00:46:57.236 15272-12238/com.example.fahadpirzada.myfirstgame I/System.out: 14.0