最近の更新

2014年1月10日金曜日

AndEngineでアナログコントローラで画像を移動する方法

【目的】
AndEngineでアナログコントローラで画像を移動します。



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



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

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.handler.physics.PhysicsHandler;
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.Sprite;
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;

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 bitmapTextureAtlas;
    private ITextureRegion textureRegion;
    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.bitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR);
        this.textureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.bitmapTextureAtlas, this, "box.png", 0, 0);
        this.bitmapTextureAtlas.load();

        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) {
        Sprite box = new Sprite(100, 100, this.textureRegion, this.getVertexBufferObjectManager());
        final PhysicsHandler physicsHandler = new PhysicsHandler(box);
        box.registerUpdateHandler(physicsHandler);
        scene.attachChild(box);

        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) {
                                                                                        physicsHandler.setVelocity(valueX * 100, valueY * 100);
                                                                                    }

                                                                                    @Override
                                                                                    public void onControlClick(AnalogOnScreenControl analogOnScreenControl) {
                                                                                    }
                                                                                });

        scene.setChildScene(analogOnScreenControl);
    }
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。
3.「Ctrl+S」を押し、ファイルを保存。



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







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



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

















以上です。

0 件のコメント:

コメントを投稿

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

関連記事