As the title of the tutorial suggests we will be using interfaces. Interfaces are a special type of class and we already know how to create classes. However, we will not be creating any of our own interfaces until we get to the level two tutorials. There is no need, we can still make some really cool games without doing so but we will use interfaces that have been created for us as they can bring complex functionality to our games with great ease.
[widgets_on_pages id=”udemy_advert_java_1″][widgets_on_pages id=”udemy_code_details”]
An interface is a class which has no method implementations. This might sound a bit odd but one of the things this achieves is to provide us with the ability to override these methods in our code. This is especially helpful as these methods are called by the operating system at events that are useful to us.
We saw something similar in our thread tutorial. We overrode the run method by using the @override keyword. When we do this we can be sure that our code, enclosed in the run method, as well as any other methods we call from this method, (like update and draw) will work in the thread.
Interfaces can also provide critical functionality like allowing us to interact with the user’s input. For example, for Android, all we need to do is use the onTouchListener interface and we can then @override the onTouchEvent method. Then, every time the player touches the screen we can deal with it, according to the needs of our game. Just like this.
@Override public boolean onTouchEvent(MotionEvent motionEvent) { // Handle the player input here }
Note that the method and interface names are different when dealing with desktop Java but the concept is identical.
The final explanation that is necessary before we build a game is that in Java everything is a class or part of a class. Our game is no exception. You will have seen this already if you completed any of the mini projects associated with earlier tutorials. When we declare our class all we have to do is add the implements keyword followed by the name of the interface or interfaces we want to use.
Let’s put into action and clarify everything we have learned in the previous 10 tutorials and build a simple game engine.
[widgets_on_pages id=”udemy_advert_java_3″][widgets_on_pages id=”udemy_code_details”]
Is this lesson up yet? I can’t seem to go into it.
Hi there,
I am not sure what it is you “can’t get into”? Have you found a broken link? Thanks for your time, hope you can let me know in more detail.
John
We saw this in our thread tutorial. We overrode the run method by using the @override keyword and then we can be sure that our code enclosed in the run method as well as any other methods we call from it, like update and draw will <>work in the thread. (did you mean to say in the last sentence “like update and draw will <> work in the thread)?
Hi Vincent, Thanks for your question. I must be having a bad day as I am not sure what you mean. Can you rephrase the question and I will try and answer it?
I think he’s saying “did you accidentally add the last sentence after the comma.” I’m getting tired, so my brain isn’t understanding so well, but that sounds right as a dependent clause to me.
Hey, i was wandering where the level two tutorials are or is it just the android part?
Hi Justin, I will start adding them from June 2016. Sorry for the delay.