Akash
Akash

Reputation: 220

Error while creating a class from SurfaceView, which is to be run as a thread

I know the basic concepts of OOP, I am trying my hands on Android programming.(Games in particular). I am trying to implement a game project from this book Android Game programming by example. I am able to understand theoretically what is being said. But when I put together the pieces of code, into a single java class TDView.java file, I get an error in Android studio. Maybe I misunderstood some phrase and Put a piece where I wasn't suppose to put it.

The errors are labeled error1 and error2 in the code below:

package com.gamecodeschool.c1tappydefender;
import android.content.Context;
import android.view.SurfaceView;

public class TDView extends SurfaceView implements Runnable {
    volatile boolean playing;
    Thread gameThread = null;

    @Override
    public void run() {
        while (playing) {
            update();
            draw();
            control();
        } //error2: it says a semicolon is needed here.

        private void update(){
        }
        private void draw(){
        }
        private void control(){
        }
    }// error1: it says class or interface missing

    public TDView(Context context) {
        super(context);

    }

    // Clean up our thread if the game is interrupted or the player quits
    public void pause() {
        playing = false;
        try {
            gameThread.join();
        } catch (InterruptedException e) { 

        }
    }

    // Make a new thread and start it
    // Execution moves to our R
    public void resume() {
        playing = true;
        gameThread = new Thread(this);
        gameThread.start();
    }
}

Upvotes: 0

Views: 75

Answers (1)

Chris K.
Chris K.

Reputation: 46

Move your method declaration outside of the declaration of your run method. this should do the trick

public void run() {
  while (playing) {
    update();
    draw();
    control();
  }
}

private void update(){
}

private void draw(){
}

private void control(){
}

Upvotes: 1

Related Questions