绘制方法不起作用

时间:2018-02-13 09:08:38

标签: java android

所以我开始从这个系列开始学习Android工作室游戏开发:https://www.youtube.com/watch?v=Rliwg0sELJo而且我似乎找不到我的绘制方法不起作用的原因。我对我的代码和视频进行了三重检查并运行了一些测试以尝试查找我的错误,但结果空白。

请记住我是stackoverflow和编程的新手,所以如果你看到任何方法我可以改进格式化的东西我喜欢听它

MainActivity:

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(new GamePanel(this));
}
}

的GamePanel:

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class GamePanel extends SurfaceView implements SurfaceHolder.Callback {
private MainThread thread;

private RectPlayer player;
private Point playerPoint;

public GamePanel(Context context){
    super(context);
    getHolder().addCallback(this);
    thread=new MainThread(getHolder(),this);
    player = new RectPlayer(new Rect(100,100,200,200), Color.rgb(255,0,0));
    playerPoint=new Point(150,150);
    setFocusable(true);
}
@Override
public void surfaceChanged(SurfaceHolder holder,int format,int width,int height){

}
@Override
public void surfaceCreated(SurfaceHolder holder){
    thread = new MainThread(getHolder(),this);

    thread.setRunning(true);
    thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    while(retry){
        try{
            thread.setRunning(false);
            thread.join();
        }catch(Exception e){e.printStackTrace();}
        retry=false;
    }
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
    switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
        playerPoint.set((int)event.getX(),(int)event.getY());
    }
    return true;
    //return super.onTouchEvent(event);
}

public void update(){
    player.update(playerPoint);
    System.out.println(playerPoint.x);
    System.out.println(playerPoint.y);
}
@Override
public void draw(Canvas canvas){
    super.draw(canvas);

    canvas.drawColor(Color.BLUE);

    player.draw(canvas);
}
}

MainThread:

package com.example.student.game_core1;

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

public class MainThread extends  Thread {
public static final int MAX_FPS=30;
private double averageFPS;
private SurfaceHolder surfaceHolder;
private GamePanel gamePanel;
private  boolean running;
public static Canvas canvas;

public void setRunning(boolean running){
    this.running=running;
}


public MainThread(SurfaceHolder surfaceHolder,GamePanel gamePanel){
    super();
    this.surfaceHolder = surfaceHolder;
    this.gamePanel = gamePanel;
}

@Override
public void run(){

    long startTime;
    long timeMillis;
    long waitTime;
    int frameCount=0;
    long totalTime=0;
    long targetTime=1000/MAX_FPS;

    while (running){
        startTime=System.nanoTime();
        canvas=null;
        try{
            canvas=this.surfaceHolder.lockCanvas();
            synchronized (surfaceHolder){
                this.gamePanel.update();
                this.gamePanel.draw(canvas);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if(canvas!=null){
                try{
                    surfaceHolder.unlockCanvasAndPost(canvas);
                }catch(Exception e){e.printStackTrace();}
            }
        }
        timeMillis=(System.nanoTime()-startTime)/1000000;
        waitTime=targetTime - timeMillis;
        try{
            if(waitTime>0)
                this.sleep(waitTime);
        }catch(Exception e) {e.printStackTrace();}
        totalTime+=System.nanoTime()-startTime;
        frameCount++;
        if(frameCount==MAX_FPS){
            averageFPS=1000/(totalTime/frameCount/1000000);
            frameCount=0;
            totalTime=0;
            System.out.println(averageFPS);
        }
    }
}
}

RectPlayer:

package com.example.student.game_core1;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

public class RectPlayer implements GameObject {

private Rect rectangle;
private int color;

public RectPlayer(Rect rectangle, int color){
    this.rectangle=rectangle;
    this.color=color;
}

@Override
public void draw(Canvas canvas){
    Paint paint=new Paint();
    paint.setColor(color);
    canvas.drawRect(rectangle,paint);
}

@Override
public void update(){

}

public void update(Point point){
    rectangle.set((point.x)-(rectangle.width()/2),(point.y)-(rectangle.height()/2),(point.x)+(rectangle.width()/2),(point.y)+(rectangle.height()/2));
}

public Rect getRectangle() {
    return rectangle;
}
}

1 个答案:

答案 0 :(得分:0)

如果我没有错,您使用的是Canvas,尝试使用draw方法draw()您的项目。

您似乎需要使用其他方法( onDraw(画布)):

在Canvas对象中绘制图形。要使用此选项,请将画布传递给相应的类' onDraw(Canvas)方法。您还可以在Canvas中使用绘图方法。此选项还可让您控制任何动画。

如果您需要有关画布的更多信息,请转到此处 - > https://developer.android.com/guide/topics/graphics/2d-graphics.html