最近の更新

2014年1月10日金曜日

AndEngineで画像をアニメーション(移動)する方法

【目的】
AndEngineで画像をアニメーション(移動)します。



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



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

import org.andengine.engine.camera.Camera;
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.AnimatedSprite;
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.TiledTextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
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 BitmapTextureAtlas bitmapTextureAtlas;
    private TiledTextureRegion textureRegion;

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

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

    private void drawSprite(Scene scene) {
        Ball ball = new Ball(100.0f, 100.0f, this.textureRegion, this.getVertexBufferObjectManager());
        scene.attachChild(ball);
    }

    private static class Ball extends AnimatedSprite {
        private static final float VELOCITY = 100.0f;
        private final PhysicsHandler mPhysicsHandler;

        public Ball(float pX, float pY, TiledTextureRegion pTextureRegion, VertexBufferObjectManager pVertexBufferObjectManager) {
            super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
            this.mPhysicsHandler = new PhysicsHandler(this);
            this.registerUpdateHandler(this.mPhysicsHandler);
            this.mPhysicsHandler.setVelocity(VELOCITY, VELOCITY);
        }

        @Override
        protected void onManagedUpdate(final float pSecondsElapsed) {
            if (this.mX < 0) {
                this.mPhysicsHandler.setVelocityX(VELOCITY);
            } else if (this.mX + this.getWidth() > CAMERA_WIDTH) {
                this.mPhysicsHandler.setVelocityX(-VELOCITY);
            }

            if (this.mY < 0) {
                this.mPhysicsHandler.setVelocityY(VELOCITY);
            } else if (this.mY + this.getHeight() > CAMERA_HEIGHT) {
                this.mPhysicsHandler.setVelocityY(-VELOCITY);
            }

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



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







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



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

















以上です。

0 件のコメント:

コメントを投稿

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

関連記事