AndEngineでLevelLoaderでEntiryの情報を読み込みます。
【準備】
1.「AndEngineをワークスペースにインポートする方法」の手順で、AndEngineをワークスペースにインポートしておきます。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndEngineSample031-LevelLoader」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.andenginesample031 |
| プロジェクト名(Project Name) | AndEngineSample031-LevelLoader |
| パッケージ名(Package Name) | com.example.andenginesample031 |
| Build SDK | API 8 |
| Minimum Required SDK | API 8 |
2.「AndEngineをライブラリ設定する方法」の手順でライブラリを設定。
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andenginesample031"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.andenginesample031.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「assets/sample.lvl」を作成し、以下の通り。
<?xml version="1.0" encoding="utf-8"?>
<level>
<entity x="48" y="16" width="16" height="16" />
<entity x="48" y="32" width="16" height="16" />
<entity x="48" y="48" width="16" height="16" />
<entity x="48" y="64" width="16" height="16" />
<entity x="48" y="80" width="16" height="16" />
<entity x="64" y="48" width="16" height="16" />
<entity x="80" y="48" width="16" height="16" />
<entity x="96" y="16" width="16" height="16" />
<entity x="96" y="32" width="16" height="16" />
<entity x="96" y="48" width="16" height="16" />
<entity x="96" y="64" width="16" height="16" />
<entity x="96" y="80" width="16" height="16" />
<entity x="128" y="16" width="16" height="16" />
<entity x="128" y="32" width="16" height="16" />
<entity x="128" y="48" width="16" height="16" />
<entity x="128" y="64" width="16" height="16" />
<entity x="128" y="80" width="16" height="16" />
<entity x="144" y="16" width="16" height="16" />
<entity x="144" y="48" width="16" height="16" />
<entity x="144" y="80" width="16" height="16" />
<entity x="160" y="16" width="16" height="16" />
<entity x="160" y="80" width="16" height="16" />
<entity x="176" y="16" width="16" height="16" />
<entity x="176" y="80" width="16" height="16" />
<entity x="208" y="16" width="16" height="16" />
<entity x="208" y="32" width="16" height="16" />
<entity x="208" y="48" width="16" height="16" />
<entity x="208" y="64" width="16" height="16" />
<entity x="208" y="80" width="16" height="16" />
<entity x="224" y="80" width="16" height="16" />
<entity x="240" y="80" width="16" height="16" />
<entity x="256" y="80" width="16" height="16" />
<entity x="288" y="16" width="16" height="16" />
<entity x="288" y="32" width="16" height="16" />
<entity x="288" y="48" width="16" height="16" />
<entity x="288" y="64" width="16" height="16" />
<entity x="288" y="80" width="16" height="16" />
<entity x="304" y="80" width="16" height="16" />
<entity x="320" y="80" width="16" height="16" />
<entity x="336" y="80" width="16" height="16" />
<entity x="368" y="16" width="16" height="16" />
<entity x="368" y="32" width="16" height="16" />
<entity x="368" y="48" width="16" height="16" />
<entity x="368" y="64" width="16" height="16" />
<entity x="368" y="80" width="16" height="16" />
<entity x="384" y="16" width="16" height="16" />
<entity x="384" y="80" width="16" height="16" />
<entity x="400" y="16" width="16" height="16" />
<entity x="400" y="80" width="16" height="16" />
<entity x="416" y="16" width="16" height="16" />
<entity x="416" y="32" width="16" height="16" />
<entity x="416" y="48" width="16" height="16" />
<entity x="416" y="64" width="16" height="16" />
<entity x="416" y="80" width="16" height="16" />
<entity x="208" y="112" width="16" height="16" />
<entity x="224" y="112" width="16" height="16" />
<entity x="240" y="112" width="16" height="16" />
<entity x="256" y="112" width="16" height="16" />
<entity x="272" y="112" width="16" height="16" />
<entity x="192" y="128" width="16" height="16" />
<entity x="176" y="144" width="16" height="16" />
<entity x="160" y="160" width="16" height="16" />
<entity x="160" y="176" width="16" height="16" />
<entity x="160" y="192" width="16" height="16" />
<entity x="160" y="208" width="16" height="16" />
<entity x="160" y="224" width="16" height="16" />
<entity x="160" y="240" width="16" height="16" />
<entity x="176" y="256" width="16" height="16" />
<entity x="192" y="272" width="16" height="16" />
<entity x="208" y="288" width="16" height="16" />
<entity x="224" y="288" width="16" height="16" />
<entity x="240" y="288" width="16" height="16" />
<entity x="256" y="288" width="16" height="16" />
<entity x="272" y="288" width="16" height="16" />
<entity x="288" y="272" width="16" height="16" />
<entity x="304" y="256" width="16" height="16" />
<entity x="320" y="240" width="16" height="16" />
<entity x="320" y="224" width="16" height="16" />
<entity x="320" y="208" width="16" height="16" />
<entity x="320" y="192" width="16" height="16" />
<entity x="320" y="176" width="16" height="16" />
<entity x="320" y="160" width="16" height="16" />
<entity x="304" y="144" width="16" height="16" />
<entity x="288" y="128" width="16" height="16" />
<entity x="208" y="160" width="32" height="32" />
<entity x="256" y="160" width="32" height="32" />
<entity x="192" y="224" width="16" height="16" />
<entity x="208" y="240" width="16" height="16" />
<entity x="224" y="256" width="16" height="16" />
<entity x="240" y="256" width="16" height="16" />
<entity x="256" y="256" width="16" height="16" />
<entity x="272" y="240" width="16" height="16" />
<entity x="288" y="224" width="16" height="16" />
<entity x="240" y="208" width="16" height="16" />
</level>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「src/com/example/andenginesample031/MainActivity.java」は以下の通り。
package com.example.andenginesample031;
import java.io.IOException;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.SAXUtils;
import org.andengine.util.debug.Debug;
import org.andengine.util.level.IEntityLoader;
import org.andengine.util.level.LevelLoader;
import org.andengine.util.level.constants.LevelConstants;
import org.xml.sax.Attributes;
public class MainActivity extends SimpleBaseGameActivity {
private static final float BG_R = 25 / 255.0f;
private static final float BG_G = 160 / 255.0f;
private static final float BG_B = 224 / 255.0f;
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
private static final String TAG_ENTITY = "entity";
private static final String TAG_ENTITY_ATTRIBUTE_X = "x";
private static final String TAG_ENTITY_ATTRIBUTE_Y = "y";
private static final String TAG_ENTITY_ATTRIBUTE_WIDTH = "width";
private static final String TAG_ENTITY_ATTRIBUTE_HEIGHT = "height";
@Override
public EngineOptions onCreateEngineOptions() {
Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
RatioResolutionPolicy policy = new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, policy, camera);
}
@Override
public void onCreateResources() {
}
@Override
public Scene onCreateScene() {
Scene scene = new Scene();
scene.setBackground(new Background(BG_R, BG_G, BG_B));
loadLevel(scene);
return scene;
}
private void loadLevel(final Scene scene) {
final LevelLoader levelLoader = new LevelLoader();
levelLoader.setAssetBasePath("");
levelLoader.registerEntityLoader(LevelConstants.TAG_LEVEL, new IEntityLoader() {
@Override
public IEntity onLoadEntity(final String pEntityName, final Attributes pAttributes) {
return scene;
}
});
levelLoader.registerEntityLoader(TAG_ENTITY, new IEntityLoader() {
@Override
public IEntity onLoadEntity(final String pEntityName, final Attributes pAttributes) {
int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_X);
int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_Y);
int width = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_WIDTH);
int height = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_HEIGHT);
return createRectangle(x, y, width, height);
}
});
try {
levelLoader.loadLevelFromAsset(this.getAssets(), "sample.lvl");
} catch (final IOException e) {
Debug.e(e);
}
}
private Rectangle createRectangle(int x, int y, int width, int height) {
Rectangle rectangle = new Rectangle(x, y, width, height, this.getVertexBufferObjectManager());
rectangle.setColor(0, 0, 1.0f);
return rectangle;
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順5】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順6】
1.以下の様に表示されれば成功です。
以上です。

0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。