Creating a sample game loop using the Android SDK
Android SDK development starts with an activity, and the game runs on single or multiple views. Most of the time, it is considered to have a single view to run gameplay.
Unfortunately, the Android SDK does not provide a predefined game loop. However, the loop can be created in many ways, but the basic mechanism remains the same.
In the Android SDK library, the View
class contains an abstract method OnDraw()
in which every possible rendering call is queued. This method is called upon any change in the drawing, which invalidates the previous rendering pipeline.
The logic is as follows:
Let's have a look at a basic game loop created with Android View
. Here, a custom view is extended from the Android View
:
/*Sample Loop created within OnDraw()on Canvas * This loop works with 2D android game development */ @Override public void onDraw(Canvas canvas) { //If the game loop is active then only update and render if(gameRunning) { //update...