Jan 15, 2012

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.

2 comments:

  1. This looks like an excellent tutorial, but trying it last night it wouldn't run, with an exception error. I'm now going through again copying exactly what you've done (no custom variable names).

    Your inserted methods are @Overrides, but when trying this myself, the inserted methods aren't @Overrides, and if I copy/paste your code, Eclipse complains about the @Override tags

    ReplyDelete
  2. Have you import android.view.SurfaceHolder and android.view.SurfaceView? Otherwise, Eclipse cannot recognize @Override, and complain on it.

    ReplyDelete

Infolinks In Text Ads