シーンの分割と素材の管理
シーンを分割
シーンはゲームの場面の単位
タイトルとか、ゲーム画面、リザルト画面とかはそれぞれのシーンと考える。
簡単なゲームとかAndengineのサンプルではアクティビティの中でシーンを作成する。
けど、複数のシーンとかを作成したい場合は、それぞれのシーンでクラスを作成すると読みやすかったりメリットがある。
素材の管理
画像とか音とかを読み込んで利用可能にするのは、アクティビティの中のonLoadResourcesメソッドの中で行う。
つまり、素材はアクティビティが管理するのが典型的な仕組みになっている。
けれど、分割した複数のシーンからアクティビティにアクセスして素材を読み込んだりする方法だと、必要以上にアクティビティにアクセスすることになる。
なので、素材の管理を一括して行うクラスを作ってしまう。
素材の読み込みを各シーンの作成時に行う方法が考えられるが、その度に読み込むと時間がちょくちょくかかるので良くないっぽい。
なのでアクティビティの作成時に必要な素材を一括で読み込んで保持しておく。
ゲーム内で新たに必要な素材を追加する場合は、そのクラスの中に追加していくことになる。
ソース
GameActivity.java
package com.example.blockgame;
import org.andengine.entity.scene.Scene;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.andengine.ui.activity.SimpleBaseGameActivity;
public class GameActivity extends SimpleBaseGameActivity {
private Camera camera;
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 720;
public ResourceManager resourceManager;
@Override
public EngineOptions onCreateEngineOptions() {
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true,
ScreenOrientation.PORTRAIT_FIXED, new FillResolutionPolicy(),
camera);
return engineOptions;
}
@Override
protected void onCreateResources() {
resourceManager = new ResourceManager(this);
}
@Override
protected Scene onCreateScene() {
Scene scene = new GameScene(resourceManager);
return scene;
}
}
GameScene.java
package com.example.blockgame;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.SpriteBackground;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
public class GameScene extends Scene {
private ResourceManager manager;
private VertexBufferObjectManager vbom;
private GameActivity activity;
private Sprite face;
private Sprite background;
public GameScene(ResourceManager manager) {
super();
this.manager = manager;
this.vbom = manager.vbom;
createScene();
}
private void createScene(){
//背景の作成
background = new Sprite(0, 0, manager.mBackgroundRegion, vbom);
this.setBackground(new SpriteBackground(background));
//キャラの作成
face = new Sprite(300, 400, manager.mFaceRegion, vbom);
this.attachChild(face);
}
}
ResourceManager.java
package com.example.blockgame;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
public class ResourceManager {
GameActivity activity;
VertexBufferObjectManager vbom;
// background
private BitmapTextureAtlas mBackgroundAtlas;
public ITextureRegion mBackgroundRegion;
//face
private BitmapTextureAtlas mFaceAtlas;
public ITextureRegion mFaceRegion;
public ResourceManager(GameActivity activity) {
this.activity = activity;
this.vbom = activity.getVertexBufferObjectManager();
loadResources();
}
private void loadResources() {
// background
this.mBackgroundAtlas = new BitmapTextureAtlas(
activity.getTextureManager(), 1024, 1024,
TextureOptions.BILINEAR);
this.mBackgroundRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mBackgroundAtlas, activity,
"gfx/background.png", 0, 0);
this.mBackgroundAtlas.load();
//face
this.mFaceAtlas = new BitmapTextureAtlas(
activity.getTextureManager(), 128, 128,
TextureOptions.BILINEAR);
this.mFaceRegion = BitmapTextureAtlasTextureRegionFactory
.createFromAsset(this.mFaceAtlas, activity,
"gfx/face.png", 0, 0);
this.mFaceAtlas.load();
}
}
実行結果
最終更新:2013年07月15日 03:11