最近の更新

2014年1月10日金曜日

AndEngineでアナログコントローラでクリックを処理する方法

【目的】
AndEngineでアナログコントローラでクリックの処理を行います。



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



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

import org.andengine.engine.camera.Camera;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl;
import org.andengine.engine.camera.hud.controls.AnalogOnScreenControl.IAnalogOnScreenControlListener;
import org.andengine.engine.camera.hud.controls.BaseOnScreenControl;
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.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.widget.Toast;

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 Camera camera;
    private BitmapTextureAtlas onScreenControlTexture;
    private ITextureRegion onScreenControlBaseTextureRegion;
    private ITextureRegion onScreenControlKnobTextureRegion;

    @Override
    public EngineOptions onCreateEngineOptions() {
        this.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("");

        this.onScreenControlTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 128, TextureOptions.BILINEAR);
        this.onScreenControlBaseTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.onScreenControlTexture, this, "onscreen_control_base.png", 0, 0);
        this.onScreenControlKnobTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.onScreenControlTexture, this, "onscreen_control_knob.png", 128, 0);
        this.onScreenControlTexture.load();
    }

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

    private void drawAnalogControl(Scene scene) {
        AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0,
                                                                                CAMERA_HEIGHT - this.onScreenControlBaseTextureRegion.getHeight(),
                                                                                this.camera,
                                                                                this.onScreenControlBaseTextureRegion,
                                                                                this.onScreenControlKnobTextureRegion,
                                                                                0.1f,
                                                                                200,
                                                                                this.getVertexBufferObjectManager(),
                                                                                new IAnalogOnScreenControlListener() {
                                                                                    @Override
                                                                                    public void onControlChange(BaseOnScreenControl baseOnScreenControl, float valueX, float valueY) {
                                                                                    }

                                                                                    @Override
                                                                                    public void onControlClick(AnalogOnScreenControl analogOnScreenControl) {
                                                                                        MainActivity.this.runOnUiThread(new Runnable() {
                                                                                            @Override
                                                                                            public void run() {
                                                                                                Toast.makeText(MainActivity.this, "onControlClick", Toast.LENGTH_SHORT).show();
                                                                                            }
                                                                                        });
                                                                                    }
                                                                                });
        scene.setChildScene(analogOnScreenControl);
    }
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



【手順4】
1.「onscreen_control_base.png」「onscreen_control_knob.png」の画像をassets以下に同名で保存。



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



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

















以上です。

0 件のコメント:

コメントを投稿

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

関連記事