足場 1 - 普通の固定足場。特別な機能はない。
|
足場 2 - 罠の足場。プレイヤーが接触したすると2秒後に落下する。
|
足場 3 - プレイヤーが触れた場合のみ落下する。
|
プレイヤーはコインを集めてスコアを獲得することができます。上記の画像を使用します。
// Game Texture public BuildableBitmapTextureAtlas gameTextureAtlas; // Game Texture Regions public ITextureRegion platform1_region; public ITextureRegion platform2_region; public ITextureRegion platform3_region; public ITextureRegion coin_region;
private void loadGameGraphics() { BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/game/"); gameTextureAtlas = new BuildableBitmapTextureAtlas(activity.getTextureManager(), 1024, 1024, TextureOptions.BILINEAR); platform1_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform1.png"); platform2_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform2.png"); platform3_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "platform3.png"); coin_region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(gameTextureAtlas, activity, "coin.png"); try { this.gameTextureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 1, 0)); this.gameTextureAtlas.load(); } catch (final TextureAtlasBuilderException e) { Debug.e(e); } }
ご覧の通り、新たにテクスチャを作成して、 gfx/game/フォルダからゲーム素材を読み込み、足場とコイン用のテクスチャ領域を4つ読み込んでいます。 これで、リソースの読み込みコードは完了です。次は別の観点について説明しましょう。
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_TYPE = "type"; private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM1 = "platform1"; private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM2 = "platform2"; private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM3 = "platform3"; private static final Object TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_COIN = "coin";
private void loadLevel(int levelID) { final SimpleLevelLoader levelLoader = new SimpleLevelLoader(vbom); final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0.01f, 0.5f); levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(LevelConstants.TAG_LEVEL) { public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException { final int width = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_WIDTH); final int height = SAXUtils.getIntAttributeOrThrow(pAttributes, LevelConstants.TAG_LEVEL_ATTRIBUTE_HEIGHT); // TODO later we will specify camera BOUNDS and create invisible walls // on the beginning and on the end of the level. return GameScene.this; } }); levelLoader.registerEntityLoader(new EntityLoader<SimpleLevelEntityLoaderData>(TAG_ENTITY) { public IEntity onLoadEntity(final String pEntityName, final IEntity pParent, final Attributes pAttributes, final SimpleLevelEntityLoaderData pSimpleLevelEntityLoaderData) throws IOException { final int x = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_X); final int y = SAXUtils.getIntAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_Y); final String type = SAXUtils.getAttributeOrThrow(pAttributes, TAG_ENTITY_ATTRIBUTE_TYPE); final Sprite levelObject; if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM1)) { levelObject = new Sprite(x, y, resourcesManager.platform1_region, vbom); PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF).setUserData("platform1"); } else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM2)) { levelObject = new Sprite(x, y, resourcesManager.platform2_region, vbom); final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF); body.setUserData("platform2"); physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false)); } else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_PLATFORM3)) { levelObject = new Sprite(x, y, resourcesManager.platform3_region, vbom); final Body body = PhysicsFactory.createBoxBody(physicsWorld, levelObject, BodyType.StaticBody, FIXTURE_DEF); body.setUserData("platform3"); physicsWorld.registerPhysicsConnector(new PhysicsConnector(levelObject, body, true, false)); } else if (type.equals(TAG_ENTITY_ATTRIBUTE_TYPE_VALUE_COIN)) { levelObject = new Sprite(x, y, resourcesManager.coin_region, vbom) { @Override protected void onManagedUpdate(float pSecondsElapsed) { super.onManagedUpdate(pSecondsElapsed); /** * TODO * we will later check if player collide with this (coin) * and if it does, we will increase score and hide coin * it will be completed in next articles (after creating player code) */ } }; levelObject.registerEntityModifier(new LoopEntityModifier(new ScaleModifier(1, 1, 1.3f))); } else { throw new IllegalArgumentException(); } levelObject.setCullingEnabled(true); return levelObject; } }); levelLoader.loadLevelFromAsset(activity.getAssets(), "level/" + levelID + ".lvl"); }それでは、このコードを何をしているかを説明しましょう。registerEntityLoader内では、
@Override public void createScene() { createBackground(); createHUD(); createPhysics(); loadLevel(1); }この例では、assets/level/ ディレクトリから1.lvlという名前のファイルを読み込みます。
<?xml version="1.0" encoding="utf-8"?> <level width="1000" height="780"> <entity x="100" y="100" type="platform1"/> <entity x="200" y="200" type="platform1"/> <entity x="400" y="200" type="platform3"/> <entity x="550" y="300" type="platform2"/> <entity x="720" y="240" type="platform3"/> <entity x="400" y="270" type="coin"/> <entity x="500" y="400" type="coin"/> <entity x="600" y="400" type="coin"/> </level>
ゲームコードを実行すると、ゲームシーンに入った後に上記の結果が表示されます。 今のところは何もできません。ただxmlからゲームオブジェクトを読み込んだだけです。 次の記事では、プレイヤー・プレイヤーの移動を操作するゲームコントローラー・足場の挙動を扱うコードの作成方法を説明します。ゲームをプレイできるようにします。
前の記事 | 次の記事 |