Thursday, September 5, 2013

[Tut] How to perform AndEngine Examples of Nicolas Gramlich - ChangeableTextExample



Here're some resources for this part:


Create new folder named font in assets folder of the project
Copy font file above to the folder assets/font


Create new class named ChangeableTextExample in the package org.anddev.andengine.examples of the AndEngineDemo project, here is the code of this class:
package org.anddev.andengine.examples;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;


import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.text.ChangeableText;
import org.anddev.andengine.entity.util.FPSCounter;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.graphics.Color;
import android.graphics.Typeface;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 20:06:15 - 08.07.2010
 */
public class ChangeableTextExample extends BaseGameActivity {
// ===========================================================
// Constants
// ===========================================================

private static final int CAMERA_WIDTH = 320;
private static final int CAMERA_HEIGHT = 240;

// ===========================================================
// Fields
// ===========================================================

private Camera mCamera;
private BitmapTextureAtlas mFontTexture;
private Font mFont;

// ===========================================================
// Constructors
// ===========================================================

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public Engine onLoadEngine() {
this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
}

@Override
public void onLoadResources() {
this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);

this.mFont = new Font(this.mFontTexture, Typeface.createFromAsset(this.getAssets(), "font/vivabee.ttf"), 14, true, Color.argb(90, 51, 102, 102));

this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
this.mEngine.getFontManager().loadFont(this.mFont);
}

@Override
public Scene onLoadScene() {
final FPSCounter fpsCounter = new FPSCounter();
this.mEngine.registerUpdateHandler(fpsCounter);

final Scene scene = new Scene();
scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));

final ChangeableText elapsedText = new ChangeableText(10, 160, this.mFont, "Seconds elapsed:", "Seconds elapsed: XXXXX".length());
final ChangeableText fpsText = new ChangeableText(150, 140, this.mFont, "FPS:", "FPS: XXXXX".length());

scene.attachChild(elapsedText);
scene.attachChild(fpsText);

scene.registerUpdateHandler(new TimerHandler(1 / 20.0f, true, new ITimerCallback() {
@Override
public void onTimePassed(final TimerHandler pTimerHandler) {
elapsedText.setText("Seconds elapsed: " + ChangeableTextExample.this.mEngine.getSecondsElapsedTotal());
fpsText.setText("FPS: " + fpsCounter.getFPS());
}
}));

return scene;
}

@Override
public void onLoadComplete() {

}

// ===========================================================
// Methods
// ===========================================================

// ===========================================================
// Inner and Anonymous Classes
// ===========================================================
}
Open the AndroidManifest.xml and append the activity element (in red color) below the previous activity element (in light color) like this:
<activity
            android:name=".AutoParallaxBackgroundExample"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="org.anddev.andengine.examples.AUTOPARALLAXBACKGROUNDEXAMPLE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ChangeableTextExample"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="org.anddev.andengine.examples.CHANGEABLETEXTEXAMPLE" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


Run the project (in debug mode) to re-generate apk file in the bin folderOpen and Edit the batch command file named r.bat in the bin folder with some commands like this:

adb install -r AndEngineDemo.apk
adb shell am start -n org.anddev.andengine.examples/.ChangeableTextExample

Run android emulator with virtualbox
Then run the batch file r.bat

The result's like this with changing text



[ttaiit.blogspot.com] - please write clearly the source on copying this tutorial :)

0 comments:

Post a Comment

Choose an Android item