Create SurfaceView for our game
Start reading here: Create a SurfaceView Game step-by-stepIn 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 New a class, MyGameSurfaceView.java](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhP-b2w_CJruNuXGsEkJbsfno_N3VM3vll-bGqpENeaEsOgJWpH9H_8Zl6KZkHnVN9my-feBorWEBwDgNYKwuu1FEdG2Frnwybo_5fk1-E7qh89oJAORaC7ueqS3lNVNVrm09VW09h49mET/s400/01_MyGameSurfaceView.png)
- Enter "MyGameSurfaceView" in the Name field, then click Browse... to select Superclass.
![select Superclass select Superclass](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZwCo2lxUUyjDDXA_aQo8RDUCMDdEvq3ketIfr2CiSFc8SA-NTc565kAdb0Gpacp6AfC-21yI1p7OGSS6WeWQPAdW4ReK2UifUDxhYK_YyhAF5g66mmqcY0eqNjFSoHvp550-99yHCZ_8Z/s400/02_MyGameSurfaceView.png)
- Enter SurfaceView in the search box to choice SurfaceView - android.view, then click OK.
![Choice superclass of SurfaceView - android.view Choice superclass of SurfaceView - android.view](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQX3TAbPZO-2SqSFxBFocjDAdnFUjBqbjMTII_fe87hJBXC_Y-nTK8uqDepXa1vsFjXWcl3UxoESkK-rblU8sWAsvJDJQiCH3s3xGYocZ-eBIRa8Rozq2oMhW1d5MrAkV-uTnGQHCKARSM/s400/03_MyGameSurfaceView.png)
- 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)' Add constructor 'MyGameSurfaceView(Context)'](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_Ff7Wp6MnAI-tDxyzcThfD90p6d6hUj2_xzKxBHB99TShu7QgkB_sn7bNGlzWUboefzizN2Swds2zG_s2hvxckpknWpfOKtF0J_v5xFoyzsm-Te9wuCMXcVcdW1vGR6rXoRI3nz77ONgQ/s400/04_MyGameSurfaceView.png)
- 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) Import 'SurfaceHolder'(android.view)](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuxUn50EcQ8FAbiSBcYegd-7Ot43N4k3i8_iQtus-XhxSsmxieO0Ku6t0g-FjfV1DwJjXRC0YpMcdVeOWbRjK1upSt1m7WfvdxPRVoyykc2VV6m34DDkm2vTXNalomNEoCj_elYckcBFrt/s400/05_MyGameSurfaceView.png)
- 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 Add unimplemented methods](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV2x9qPA7-G4Pst_Q2CPWicz50gcW2supC6Gt0MWqivsJG1qbsfCf3Lbg35Fcz7PzOyFO7rih2iPIFOKEPjta6K4QxSos-ilS88fauT_loPcUWSzzOU3mc_Jg1cynZuyd9iYmmI6vh7Utw/s400/06_MyGameSurfaceView.png)
- 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 Three Auto-generated method will be added](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhiNbPO8LMObkzHo0qktXax1roS_-hReH2rSOrFKb7Ti4XatiUoLPbimEwCyQVBpRpl2j13Azpy50gHOH9Q17Nxt6RCzrsf3qY3B6A0nLEcr3WTkt-WQvNeYBFShXhSUjh11V9wqw60zRDV/s400/07_MyGameSurfceView.png)
- 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 komentar:
Posting Komentar