Create SurfaceView for our game

Start reading here: Create a SurfaceView Game step-by-step

In this step, we are going to create our dummy SurfaceView:

- Click to select our package (com.MyGame) in Package Explorer. Then click File of Eclipse, -> New -> Class
New a class, MyGameSurfaceView.java

- Enter "MyGameSurfaceView" in the Name field, then click Browse... to select Superclass.
select Superclass

- Enter SurfaceView in the search box to choice SurfaceView - android.view, then click OK.
Choice superclass of SurfaceView - android.view

- Then click Finish in the New Java Class dialog.

- A new class of MyGameSurfaceView.java extends SurfaceView will be generated for you. But, you will be prompted with error of undefined default constructor. Move mouse over the word with error, MyGameSurfaceView. A hints box will open for you, click to Add constructor 'MyGameSurfaceView(Context)'.
Add constructor 'MyGameSurfaceView(Context)'

- Modify the code of MyGameSurfaceView class declaration from:

public class MyGameSurfaceView extends SurfaceView

to:

public class MyGameSurfaceView extends SurfaceView implements SurfaceHolder.Callback

- You will be prompt for error again: SurfaceHolder cannot be resolved! Move mouse over the word with error, SurfaceHolder. And select Import 'SurfaceHolder'(android.view).
Import 'SurfaceHolder'(android.view)

- MyGameSurfaceView will be prompt with error again: The type MyGameSurfaceView must implement the inherited abstract method SurfaceHolder.Callback.surfaceDestroyed(SurfaceHolder). Move mouse over the word with error, MyGameSurfaceView, select Add unimplemented methods.
Add unimplemented methods

- Now three Auto-generated method will be added:
surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
surfaceCreated(SurfaceHolder arg0)
surfaceDestroyed(SurfaceHolder arg0)
Three Auto-generated method will be added

- In order to make our code more human readable, modify arg(s) with meaningful words:
package com.MyGame;

import android.content.Context;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MyGameSurfaceView extends SurfaceView implements SurfaceHolder.Callback{

public MyGameSurfaceView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub

}

}



- Save it in this moment.

0 Response to "Create SurfaceView for our game"

Posting Komentar