[Tutorial] Collision objects from TMX map
リンク先のTileCollision.zipをダウンロードしてTileの画像等を利用しました。
事前に以下のことをする。
- andengine.jarをライブラリに追加
- タイルの画像を用意
- Tiledを使ってtmxファイルを用意
ここでは単にTMX mapを表示するまでの最小限のコードとする。
public class WtMiniTestActivity extends BaseGameActivity {
static final int CAMERA_WIDTH = 480;
static final int CAMERA_HEIGHT = 320;
private TMXTiledMap mTMXTiledMap;
private BoundCamera mBoundChaseCamera;
//Scene
private Scene mScene;
public Engine onLoadEngine() {
this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
this.mBoundChaseCamera));
}
public void onLoadResources() {
}
public Scene onLoadScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
mScene = new Scene();
//TMX mapの読み込み
try {
final TMXLoader tmxLoader = new TMXLoader(this,
this.mEngine.getTextureManager(), // TextureOptions.BILINEAR_PREMULTIPLYALPHA,
TextureOptions.NEAREST);
this.mTMXTiledMap = tmxLoader.loadFromAsset(this, "tmx/ground1.tmx");
} catch (final TMXLoadException tmxle) {
Debug.e(tmxle);
}
// レイヤーをSceneに結びつける
for (int i = 0; i < this.mTMXTiledMap.getTMXLayers().size(); i++){
TMXLayer layer = this.mTMXTiledMap.getTMXLayers().get(i);
mScene.attachChild(layer);
}
// カメラがTMXLayerの境界を超えないようにする
final TMXLayer tmxLayer = this.mTMXTiledMap.getTMXLayers().get(0);
this.mBoundChaseCamera.setBounds(0, tmxLayer.getWidth(), 0,
tmxLayer.getHeight());
this.mBoundChaseCamera.setBoundsEnabled(true);
return mScene;
}
public void onLoadComplete() {
}
}
上記のコードで表示されるのが以下の様な感じ
今日はここまで。

0 件のコメント:
コメントを投稿