首先,我是Android游戏编程的新手。这是GameView java类。我没有编写代码并找到其他文件,您可以访问https://www.simplifiedcoding.net/android-game-development-tutorial-1/。当我编译一切时,android studio给了我错误。那是为什么?
package net.simplifiedcoding.spacefighter;
import android.graphics.Paint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class GameView extends SurfaceView implements Runnable {
volatile boolean playing;
private Thread gameThread = null;
//adding the player to this class
private net.simplifiedcoding.spacefighter.Player player;
//These objects will be used for drawing
private Paint paint;
private Canvas canvas;
private SurfaceHolder surfaceHolder;
public GameView(Context context) {
super(context);
//initializing player object
player = new net.simplifiedcoding.spacefighter.Player(context);
//initializing drawing objects
surfaceHolder = getHolder();
paint = new Paint();
}
@Override
public void run() {
while (playing) {
update();
draw();
control();
}
}
private void update() {
//updating player position
player.update();
}
private void draw() {
//checking if surface is valid
if (surfaceHolder.getSurface().isValid()) {
//locking the canvas
canvas = surfaceHolder.lockCanvas();
//drawing a background color for canvas
canvas.drawColor(Color.BLACK);
//Drawing the player
canvas.drawBitmap(
player.getBitmap(),
player.getX(),
player.getY(),
paint);
//Unlocking the canvas
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
private void control() {
try {
gameThread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void pause() {
playing = false;
try {
gameThread.join();
} catch (InterruptedException e) {
}
}
public void resume() {
playing = true;
gameThread = new Thread(this);
gameThread.start();
}
}