09-01 registerEntityModifier というか PhysicsHandler というか Modifier(?) というか

Sprite を移動させたいだけなのですが、単なる移動と物理学的(?) 移動とあるようです。
単純な移動の方がメモリを食わない気がしますが

package com.example.mytestproject;

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.Sprite;					// 削除
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.ITextureRegion;	// 削除
import org.andengine.opengl.texture.region.TiledTextureRegion;	// 追加 (ITextureRegion でも 動きます)
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.ui.activity.SimpleBaseGameActivity;

/**
 * (c) 2010 Nicolas Gramlich
 * (c) 2011 Zynga
 *
 * @author Nicolas Gramlich
 * @since 14:27:22 - 14.06.2011
 */
public class MyShowImage extends SimpleBaseGameActivity {
	// ===========================================================
	// Constants
	// ===========================================================
	private static final int CAMERA_WIDTH  = 480;
	private static final int CAMERA_HEIGHT = 720;
	private static final float DEMO_VELOCITY = 100.0f;		// 追加

	// ===========================================================
	// Fields
	// ===========================================================
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TiledTextureRegion mFaceTextureRegion;

	@Override
	public EngineOptions onCreateEngineOptions() {
		final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		return new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
	}

	@Override
	public void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
//		this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0);				// 削除
		this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box.png", 0, 0, 1, 1);	// 追加
		this.mBitmapTextureAtlas.load();
	}

	@Override
	public Scene onCreateScene() {
		final Scene scene = new Scene();
		scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		/* Calculate the coordinates for the face, so its centered on the camera. */
		final float centerX = (CAMERA_WIDTH  - this.mFaceTextureRegion.getWidth())  / 2;
		final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

//		final Sprite faceSprite = new Sprite(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager()); 	// 削除
		final Ball faceSprite = new Ball(centerX, centerY, this.mFaceTextureRegion, this.getVertexBufferObjectManager());		// 追加

		scene.attachChild(faceSprite);

		return scene;
	}

	// 以下、すべて追加
	private static class Ball extends AnimatedSprite {
		private final PhysicsHandler mPhysicsHandler;

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

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

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

			super.onManagedUpdate(pSecondsElapsed);
		}
	}
}

コメントを残す

メールアドレスが公開されることはありません。


*