0
votes

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

1
Did you try painting the ball a different color from its surroundings? - 0xCursor
I did, to no avail :( - A. Vulpe

1 Answers

0
votes

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. Remove onDraw method and change draw method to look like this:

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. 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.