1

我一直在尝试开始一个新项目,但当我开始时,我什么都做不了。在过去的几个小时里,我一直在查看代码并使用它,但我不记得上次它发生时我是如何解决这个问题的。

package org.waldev.canvascollisiontest;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.Menu;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CollisionTest extends Activity {
private Panel game;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    game = new Panel(this);
    setContentView(game);
}

public class Panel extends SurfaceView implements SurfaceHolder.Callback{
    Threads thread;

    public Panel(Context context) {
        super(context);
        thread = new Threads(this.getHolder(), this);
        setFocusable(true);
    }

    public void onDraw(Canvas c)
    {
        c.drawColor(Color.BLUE);
    }
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        // TODO Auto-generated method stub
    }

    //when the game starts, run the thread\
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        holder.setFormat(PixelFormat.RGB_565);
        thread.setRunning(true);
        thread.start();
    }

    //if its destroyed, then destroy the thread
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        boolean retry = true;
        thread.setRunning(false);
        while (retry) {
            try {
                thread.join();
                //                  bitmaps.recycleAll();
                retry = false;
            } catch (InterruptedException e) {
                // we will try it again and again...
            }
        }
    }

}
}

和线程:

package org.waldev.canvascollisiontest;

import org.waldev.canvascollisiontest.CollisionTest.Panel;

import android.graphics.Canvas;
import android.view.SurfaceHolder;

public class Threads extends Thread{
SurfaceHolder surfaceHolder;
private Panel game;

private Canvas c;
private boolean isRunning = true;

// desired fps
private final static int    MAX_FPS = 24;
// maximum number of frames to be skipped
private final static int    MAX_FRAME_SKIPS = 0;
// the frame period
private final static int    FRAME_PERIOD = 1000 / MAX_FPS;  


public Threads(SurfaceHolder surfaceHolder, Panel panel){
    this.surfaceHolder = surfaceHolder;
    game = panel;
}

public void setRunning(boolean run){
    isRunning = run;
}

public boolean getRunning(){
    return isRunning();
}

public SurfaceHolder getSurface(){
    return surfaceHolder;
}

@Override
public void run() {
    Canvas canvas;

    long beginTime;     // the time when the cycle begun
    long timeDiff;      // the time it took for the cycle to execute
    int sleepTime;      // ms to sleep (<0 if we're behind)
    int framesSkipped;  // number of frames being skipped 
    boolean updated = false;

    sleepTime = 0;

    while (isRunning) {
        canvas = null;
        try {
            canvas = this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder) {
                beginTime = System.currentTimeMillis();
                framesSkipped = 0;
                if(canvas != null)
                {
                    game.onDraw(canvas);
                }
                timeDiff = System.currentTimeMillis() - beginTime;
                sleepTime = (int)(FRAME_PERIOD - timeDiff);

                if (sleepTime > 0) {
                    try {
                        Thread.sleep(sleepTime);
                    } catch (InterruptedException e) {}
                }
                else
                {
                    updated = false;
                }
            }
        } finally {
            // in case of an exception the surface is not left in
            // an inconsistent state
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }   // end finally
    }
}
public boolean isRunning() {
    return isRunning;
}
}

我在那里扔了一些断点,它从来没有在 CollisionTest 中调用 surfaceCreated。我知道我在这里想念的东西一定很愚蠢,所以如果你看到它,请告诉我。谢谢!

威廉

4

1 回答 1

2

我得到它的工作,我忘了添加:

getHolder().addCallback(this);

到面板的构造函数,这就是导致它不起作用的原因。谢谢!

于 2012-12-21T16:14:06.987 回答