我是 android 编程的新手,我不明白为什么我的渲染器中的 OnDraw() 方法没有被调用,因为 invalidate() 被调用了。然而,最终设置的背景资源确实被调用了。有 2 个类,Game.java 和 Renderer.java。似乎无效调用没有时间在游戏循环期间得到处理。
游戏.Java
public class Game extends Activity
{
Level currentLevel;
private static List<Entity> _renderList = new ArrayList<Entity>();
public void StartLevel(View view)
{
System.out.println("I HAVE STARTED THE LEVEL!");
// Hide the play button
Button playButton = (Button) findViewById(R.id.start_game);
playButton.setVisibility(View.GONE);
// Load the level
currentLevel = new Level1(this);
// Create the initial entities
// Add the entities to the screen
List<Bitmap> bitmapList;
bitmapList = new ArrayList<Bitmap>();
Bitmap enemy = BitmapFactory.decodeResource(getResources(), R.drawable.enemy);
bitmapList.add(enemy);
System.out.println("CREATING RENDERER!");
Renderer renderer = new Renderer(this, _renderList, bitmapList);
renderer.setBackgroundResource(R.drawable.beachbackground);
setContentView(renderer);
System.out.println("STARTING TRAVEL!");
while (currentLevel.GetDistTravelled() < currentLevel.GetDist())
{
Retrieve();
currentLevel.AddDistance(1);
System.out.println("DISTANCE +1!");
renderer.RenderFrame(_renderList);
System.out.println("RENDER LEVEL FRAME!");
try
{
Thread.currentThread().sleep(50);
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("SET BACKGROUND!");
renderer.setBackgroundResource(R.drawable.menubackground);
}
public List<Entity> GetRenderList()
{
return _renderList;
}
private void Retrieve()
{
// get the array
Entity[] enemies;
enemies = currentLevel.CheckSpawn();
if(enemies != null)
{
// parse the array into entities
int length = enemies.length;
System.out.println(length);
// remember to Get enemy strings
for(int i = 0; i < length; i++)
{
// add them to our array
_renderList.add(enemies[i]);
}
}
}
}
渲染器.java
class Renderer extends SurfaceView implements SurfaceHolder.Callback
{
PanelThread _thread;
List<Entity> _renderList = new ArrayList<Entity>();
List<Bitmap> _bitmapList = new ArrayList<Bitmap>();
public Renderer(Context context, List<Entity> renderList, List<Bitmap> bitmapList)
{
super(context);
getHolder().addCallback(this);
_bitmapList = bitmapList;
}
public void RenderFrame(List<Entity> renderList)
{
for (int i = 0; i < renderList.size(); i++)
{
_renderList.add(renderList.get(i));
}
invalidate();
}
@Override
public void onDraw(Canvas canvas)
{
Paint paint = null;
// Draw enemies
if(_renderList.size() != 0 && _renderList != null)
{
int size = _renderList.size();
for(int i = 0; i < size; i++)
{
canvas.drawBitmap(_bitmapList.get(_renderList.get(i).GetBitmapID()), _renderList.get(i).GetX(), _renderList.get(i).GetY(), paint);
}
}
}
// position the train on the screen in the appropriate place
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
setWillNotDraw(false); //Allows us to use invalidate() to call onDraw()
_thread = new PanelThread(getHolder(), this); //Start the thread that
_thread.setRunning(true); //will make calls to
_thread.start(); //onDraw()
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
try
{
_thread.setRunning(false); //Tells thread to stop
_thread.join(); //Removes thread from mem.
}
catch (InterruptedException e) {}
}
class PanelThread extends Thread
{
private SurfaceHolder _surfaceHolder;
private Renderer _renderer;
private boolean _run = false;
public PanelThread(SurfaceHolder surfaceHolder, Renderer renderer)
{
_surfaceHolder = surfaceHolder;
_renderer = renderer;
}
public void setRunning(boolean run)
{
_run = run;
}
@Override
public void run()
{
Canvas c;
while (_run)
{
c = null;
try
{
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder)
{
//Insert methods to modify positions of items in onDraw()
postInvalidate();
}
}
finally {
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}
任何有关解决此问题的帮助将不胜感激。如果需要更多信息,请询问。