0

I try to learn Kotlin by working through the Book "Android Game Programming by Example". Now I can't get any further in creating threads. In Java, a thread is first defined with zero and later delayed with sleep (). Since I'm still a newbe, I can't customize the code to my needs. That's how I found an explanation for threads in Kotlin. But I can't put it into practice. Can someone tell me how to do this using my example? I cut out the code lines for the threads.

public class TDView extends SurfaceView implements Runnable {

//Thread related
volatile boolean playing;
Thread gameThread = null; //Line 29
...
private void control() {
    try {
        gameThread.sleep(17);          //Line 310
    } catch (InterruptedException e) {
        //catch things here
    }
}

public void pause() {
    playing = false;
    try {
        gameThread.join();             //Line 319
    } catch (InterruptedException e) {
        //catch things here
    }
}

public void resume() {
    playing = true;
    gameThread = new Thread(this);  //Line 327
    gameThread.start();
}

The whole code can be found here.

I thought I'd do it like this:

private val gameThread: Thread? = null
.
//Line 310 same as Java -- here I can't find the sleep-method
//Line 319 same as Java
.
gameThread? = Thread(this)
gameThread.start()

P.S. I have read this article, but I don't know how to fit it in.

WillHoh
  • 67
  • 11
  • I think it would be easier if you posted your full Kotlin code, rather than trying to skip certain sections. I can see what you were trying to achieve but the full code would be more helpful. – Michael Nov 02 '17 at 11:40
  • 1
    And what is the question exactly ? Not clear for me. – AxelH Nov 02 '17 at 11:46
  • I would like to know how to write that code correct in kotlin. How would you and why. @Michael here my code at the moment (https://gist.github.com/willhoh/9c6e8bc18983f86af3e6e0e2aa1fd499) – WillHoh Nov 02 '17 at 15:00

1 Answers1

0

You can convert code from Java to Kotlin

  1. On the main menu, point to Code menu.
  2. Choose Convert Java File to Kotlin File.

.

@Volatile internal var playing: Boolean = false
internal var gameThread: Thread? = null //Line 29

private fun control() {
    try {

        //because that don't exist you can try that
        //gameThread!!.sleep(17)          //Line 310

        Thread.sleep(17)
        gameThread!!.stop()  //Line 310
    } catch (e: InterruptedException) {
        //catch things here
    }

}

fun pause() {
    playing = false
    try {
        gameThread!!.join()             //Line 319
    } catch (e: InterruptedException) {
        //catch things here
    }

}

fun resume() {
    playing = true
    gameThread = Thread(this)  //Line 327
    gameThread!!.start()
}
gmetax
  • 3,853
  • 2
  • 31
  • 45
  • 1
    ...when you use Android Studio. – Peter Bruins Nov 02 '17 at 12:05
  • now I changed the control function and use "Thread.sleep(17)", without "gameThread!!.stop()", and it works. But why do I need to use the Class method and not the instance method gameThread.sleep ? – WillHoh Nov 02 '17 at 15:56
  • 2
    @WillHoh There is no instance method `sleep` for `Thread`s. [It is a static method](https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)). You can call static methods using instances, but it is usually bad practice and is considered by some to be a design flaw. [You can even call static methods on null pointers](https://stackoverflow.com/questions/3293353/how-come-invoking-a-static-method-on-a-null-reference-doesnt-throw-nullpointe). – Salem Nov 02 '17 at 15:58