最近の更新

2014年1月22日水曜日

AndEngineでボタン画像を使用する方法

【目的】
AndEngineでボタン画像を使用します。



【準備】
1.「AndEngineをワークスペースにインポートする方法」の手順で、AndEngineをワークスペースにインポートしておきます。



【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndEngineSample037-ButtonSprite」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
項目名 項目に設定する値
アプリケーション名(Application Name) com.example.andenginesample037
プロジェクト名(Project Name) AndEngineSample037-ButtonSprite
パッケージ名(Package Name) com.example.andenginesample037
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.andenginesample037"
    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.andenginesample037.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.「src/com/example/andenginesample037/MainActivity.java」は以下の通り。
package com.example.andenginesample037;

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.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.ButtonSprite;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.debug.Debug;

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 = 720;
    private static final int CAMERA_HEIGHT = 480;

    private ITextureRegion normalTexture;
    private ITextureRegion pressedTexture;

    @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() {
        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("");

        BuildableBitmapTextureAtlas bbta = new BuildableBitmapTextureAtlas(this.getTextureManager(), 512, 512);
        this.normalTexture= BitmapTextureAtlasTextureRegionFactory.createFromAsset(bbta, this, "box.png");
        this.pressedTexture = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bbta, this, "box_red.png");

        try {
            bbta.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
            bbta.load();
        } catch (TextureAtlasBuilderException e) {
            Debug.e(e);
        }
    }

    @Override
    public Scene onCreateScene() {
        Scene scene = new Scene();
        scene.setBackground(new Background(BG_R, BG_G, BG_B));
        scene.setTouchAreaBindingOnActionDownEnabled(true);
        drawButtonSprite(scene);
        return scene;
    }

    private void drawButtonSprite(Scene scene) {
        ButtonSprite buttonSprite = new ButtonSprite(100, 100, this.normalTexture, this.pressedTexture, this.getVertexBufferObjectManager());
        scene.registerTouchArea(buttonSprite);
        scene.attachChild(buttonSprite);
    }
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順4】
1.以下の画像をassets以下に「box.png」という名前で保存。




2.以下の画像をassets以下に「box_red.png」という名前で保存。





【手順5】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。



【手順6】
1.以下の様に表示されれば成功です。






以上です。

0 件のコメント:

コメントを投稿

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

関連記事