磯野ー!2Dゲーム開発しようぜー! HTML5編 その1
磯野ー!2Dゲーム開発しようぜー! HTML5編 その2
磯野ー!2Dゲーム開発しようぜー! HTML5編 その3
磯野ー!2Dゲーム開発しようぜー! Android編 その1
磯野ー!2Dゲーム開発しようぜー! Android編 その2
磯野ー!2Dゲーム開発しようぜー! Android編 その3
Text
1 2 3 4 5 6 7 8 9 10 11 12 13 | private Font _font; @Override protected void onCreateResources() { _font = FontFactory.create( this .getFontManager(), this .getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 24, Color.WHITE_ARGB_PACKED_INT); _font.load(); } @Override protected Scene onCreateScene() { _scene = new Scene(); Text text = new Text(100, 10, _font, "Click on the characters!" , this .getVertexBufferObjectManager()); text.setColor( new Color(0, 0, 0)); // 色ごとにFontを用意するのは難儀なので、WhiteでFontを読み込み、後から色を設定する _scene.attachChild(text); } |
フォントを指定して読み込む方法は下記を参照してほしい。
src/org/andengine/examples/CustomFontExample.java
EntityModifier
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | final ButtonSprite ship = new ButtonSprite(50, 50, _shipRegion.getTextureRegion(0), this .getVertexBufferObjectManager()); final AnimatedSprite shipAnimation = new AnimatedSprite(10, 10, _shipRegion, this .getVertexBufferObjectManager()); shipAnimation.animate(50); shipAnimation.setPosition(50, 50); // コンストラクタで指定しているけれど、このコードがないとRotationの挙動がおかしい ship.setOnClickListener( new ButtonSprite.OnClickListener(){ @Override public void onClick(ButtonSprite pButtonSprite, float pTouchAreaLocalX, float pTouchAreaLocalY) { _scene.detachChild(pButtonSprite); shipAnimation.setRotation(0); SequenceEntityModifier shipModifier = new SequenceEntityModifier( new IEntityModifier.IEntityModifierListener() { @Override public void onModifierStarted(IModifier<ientity> pModifier, IEntity pItem) { } @Override public void onModifierFinished(final IModifier<ientity> pEntityModifier, final IEntity pEntity) { MainActivity. this .mEngine.runOnUpdateThread( new Runnable() { @Override public void run() { _scene.attachChild(ship); _scene.detachChild(shipAnimation); pEntity.unregisterEntityModifier((IEntityModifier)pEntityModifier); } }); } }, new RotationByModifier(0.5f, 90) , new MoveModifier(1.0f, 50, CAMERA_WIDTH+50, 50, 50, EaseExponentialOut.getInstance()) , new RotationByModifier(0.1f, 180) , new MoveModifier(1.0f, CAMERA_WIDTH+50, 50, 50, 50, EaseExponentialOut.getInstance()) , new RotationByModifier(0.5f, 90) ); shipAnimation.registerEntityModifier(shipModifier); _scene.attachChild(shipAnimation); } }); _scene.registerTouchArea(ship); _scene.attachChild(ship); |
1 2 3 4 5 6 7 8 | // 省略 new RotationByModifier(0.5f, 90), new ParallelEntityModifier( new ScaleModifier(3, 0.5f, 5), new RotationByModifier(3, 90) ), new RotationByModifier(0.5f, 90), // 省略 |
AndEngine / src / org / andengine / util / modifier / ease /
実際に挙動を見るのが一番早いのでAndEngineExamplesをローカルに設定してEase関数周りのサンプルを動作させてみるのが一番良いだろう。その際にはExtensionsも必要なので忘れずに取得しよう。詳しくはAndEngineのReadMeを参照してほしい。
EntityModifierの開始、終了のイベントハンドラーでUI要素をいじくる場合は注意してほしい。左記2つの関数はワーカスレッドから呼び出されるのでUI要素をいじくるとアプリがクラッシュする。それなので17行目のようにmEngine.runOnUpdateThreadでUIスレッドを呼び出して処理しよう。
EntityModifierのざっくりとした説明は以上になるけれど、今回EntityModifierをいじくりまわしていていくつか不明な点があった。1つ目は、EnityModifierを使いまわせないかとEnityModifier.reset()をいじくりまわしてみたけれど上手くいかなかったこと。2つ目は、unregisterEntityModifierの使いどころ。Entityが不要なEntityModifierをいつまでも参照しているのが嫌だったので一応呼んでいるけれど、AndEngineのサンプルコードを参考にしてもunregisterEntityModifierを呼んでいるものがなかった。AndEngineのコードを追っかけていけば分かることなのだろうけれど現状はどうするのが一番良いのかは不明。
これでAndEngineの解説を終わりとする。AndEngineの基本的な使い方がどういったものか理解できたかと思う。今回は実装しなかったけれどもちろんゲームのメインループにあたる機能や、あたり判定の便利機能などなどゲーム開発に必要な機能は大体そろっている。そういった今回解説しなかった機能はAndEngineExamplesで詳説されているのでそちらを参照してほしい。