简体   繁体   English

画布不会画圆

[英]Canvas doesn't draw circles

I'm trying to make a small pong game, using a custom view that draws the platforms on each side and the ball as a circle. 我正在尝试使用自定义视图制作一个小型的乒乓球游戏,该视图将每侧的平台和球画成一个圆圈。 However when i run the app to see if the drawing is done correctly, while the platforms are drawn correctly the circle wont appear. 但是,当我运行该应用程序以查看绘图是否正确完成时,尽管平台绘制正确,但圆圈不会出现。 so my question is, why is the ball not drawn at all? 所以我的问题是,为什么根本不拉球? This is the custom view: 这是自定义视图:

public class GameView extends SurfaceView implements Runnable {
private Context mContext; // a private instance of the context
Thread gameThread=null; //the thread on which the game will run
Ball ball; //a ball
Platform lPlat,rPlat; // a platform for the left side and the right
//tools that will draw the view and the objects
Canvas canvas;
Paint paint;
Color color;
SurfaceHolder mHolder;
//variable for frame management and objects movement
long lastFrame;
long fps;

volatile boolean playing;

public GameView(Context context){
    super(context);
    this.mContext=context;
    mHolder=getHolder();
    color=new Color();
    paint=new Paint();
    gameThread=new Thread(this);
    lPlat=new Platform(2592,620,50,200);
    rPlat=new Platform(120,620,50,200);
    ball=new Ball(2400,620,20);
    setWillNotDraw(false);
    init();
}
public void init() {
    render();
    playing=true;
    gameThread.start();
}
//the game Thread
@Override
public void run(){
    while (playing){
        long currentTime=System.currentTimeMillis();
        render();
        lastFrame=System.currentTimeMillis()-currentTime;
        //dividing the time difference between the start of the count and the end of the render (which is a frame) by 1000 to get an approximation of the fps;
        if (lastFrame>=1)
            fps=1000/lastFrame;
    }
}
//renders the view, calling update method, and draw method afterwards
public void render(){
    update();
    draw();
}
public void update(){
    ball.updateBallPosition();
    rPlat.updatePlatformPosition();
    lPlat.updatePlatformPosition();
}
@Override
public void onDraw(Canvas canvas){
    super.onDraw(canvas);
    Log.i("@CanvasStatus","Canvas onDraw");
    canvas.drawColor(Color.argb(255, 100, 120, 70));

    //the drawRect methods work properly
    canvas.drawRect(lPlat.shape,paint);
    canvas.drawRect(rPlat.shape,paint);
    canvas.drawCircle(ball.x,ball.y,ball.radius,paint); //this is where I try to draw the circle 
}


public void draw(){
    if (mHolder.getSurface().isValid()) {
        Log.i("@CanvasStatus","valid");
        canvas=mHolder.lockCanvas();
        paint.setColor(Color.argb(255,10,200,157));
        paint.setStyle(Paint.Style.FILL);
        this.draw(canvas);

        mHolder.unlockCanvasAndPost(canvas);
    }
    else  Log.i("@CanvasStatus","invalid");
}
}

and this is the ball class: 这是球类:

public class Ball {
float x;
float y;
float radius;
double yspeed, xspeed;

public Ball(float x, float y, float radius) {
    this.x = x;
    this.y = y;
    this.radius = radius;
    yspeed = 200;
    xspeed = 200;
}

public void updateBallPosition() {
    x+=xspeed;
    y+=yspeed;
    //checks for wall collisions
    if(x>=GameActivity.screenCoordinates.x) {
        x = GameActivity.screenCoordinates.x;
        xspeed=-xspeed;
    }
    else if (x<=0){
        x=0;
        xspeed=-xspeed;
    }

}

}

Not everything is implemented in regards to the game, i'm just trying to get the canvas to draw the objects correctly at the moment. 并不是所有有关游戏的东西都实现了,我只是想让画布此时正确地绘制对象。 Thanks in advance 提前致谢

You don't need to override onDraw method and call draw(canvas) in your draw method, all the work with canvas should be done there. 您不需要重写onDraw方法并在draw方法中调用draw(canvas) ,所有使用canvas的工作都应在此完成。 Remove onDraw method and change draw method to look like this: 删除onDraw方法并更改draw方法,如下所示:

public void draw(){
    if (mHolder.getSurface().isValid()) {
        Log.i("@CanvasStatus","valid");
        canvas=mHolder.lockCanvas();
        paint.setColor(Color.argb(255,10,200,157));
        paint.setStyle(Paint.Style.FILL);

        canvas.drawColor(Color.argb(255, 100, 120, 70));

        //the drawRect methods work properly
        canvas.drawRect(lPlat.shape,paint);
        canvas.drawRect(rPlat.shape,paint);
        canvas.drawCircle(ball.x,ball.y,ball.radius,paint);

        mHolder.unlockCanvasAndPost(canvas);
    }
    else  Log.i("@CanvasStatus","invalid");
}

By the way, you've set up boundaries for x coordinate only, you should add it for y as well. 顺便说一句,您只为x坐标设置了边界,还应该为y添加边界。 Besides that, you will possibly need to add some delay to canvas drawing (with Thread.sleep in a simplest case) or else your view will re-draw too often. 除此之外,您可能需要为画布绘制添加一些延迟(在最简单的情况下使用Thread.sleep ),否则您的视图将过于频繁地重新绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM