「銃」というスプライトがあり、ユーザーのタッチで回転させたい。
私の問題:タッチして下に移動すると回転してうまく機能しますが、タッチして上に移動すると回転しません。
これが私のコードです:
package com.example.samplegameone;
import java.io.IOException;
import java.io.InputStream;
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.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.bitmap.BitmapTexture;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TextureRegionFactory;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.adt.io.in.IInputStreamOpener;
import org.andengine.util.debug.Debug;
import android.util.Log;
import android.widget.Toast;
public class SampleGameOne extends SimpleBaseGameActivity {
private static final int CAMERA_WIDTH = 480;
private static final int CAMERA_HEIGHT = 320;
private ITexture mTexture;
private ITextureRegion mFaceTextureRegion;
Sprite gun = null;
@Override
public EngineOptions onCreateEngineOptions() {
final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_SENSOR,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
}
@Override
public void onCreateResources() {
try {
this.mTexture = new BitmapTexture(this.getTextureManager(),
new IInputStreamOpener() {
@Override
public InputStream open() throws IOException {
return getAssets().open("gfx/gun.png");
}
});
this.mTexture.load();
this.mFaceTextureRegion = TextureRegionFactory
.extractFromTexture(this.mTexture);
} catch (IOException e) {
Debug.e(e);
}
}
@Override
public Scene onCreateScene() {
this.mEngine.registerUpdateHandler(new FPSLogger());
final Scene scene = new Scene();
scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
final float centerX = (CAMERA_WIDTH - this.mFaceTextureRegion
.getWidth()) / 2;
final float centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion
.getHeight()) / 2;
gun = new Sprite(centerX, centerY, this.mFaceTextureRegion,
this.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
float pTouchAreaLocalX, float pTouchAreaLocalY) {
switch (pSceneTouchEvent.getAction()) {
case TouchEvent.ACTION_DOWN:
break;
case TouchEvent.ACTION_MOVE:
float pValueX = pSceneTouchEvent.getX();
float pValueY = CAMERA_HEIGHT - pSceneTouchEvent.getY();
float directionX = pValueX - gun.getX();
float directionY = (CAMERA_HEIGHT - pValueY) - gun.getY();
float rotationAngle = (float) Math.atan2(directionY,
directionX);
gun.setRotationCenter(0, gun.getHeight() / 2);
gun.setRotation(org.andengine.util.math.MathUtils
.radToDeg(rotationAngle));
break;
case TouchEvent.ACTION_UP:
break;
default:
break;
}
return super.onAreaTouched(pSceneTouchEvent, pTouchAreaLocalX,
pTouchAreaLocalY);
}
};
scene.registerTouchArea(gun);
scene.setTouchAreaBindingOnActionDownEnabled(true);
scene.attachChild(gun);
return scene;
}
}
この節で回転角を計算する問題だと思います
float pValueX = pSceneTouchEvent.getX();
float pValueY = CAMERA_HEIGHT - pSceneTouchEvent.getY();
float directionX = pValueX - gun.getX();
float directionY = (CAMERA_HEIGHT - pValueY) - gun.getY();
float rotationAngle = (float) Math.atan2(directionY,
directionX);
gun.setRotationCenter(0, gun.getHeight() / 2);
gun.setRotation(org.andengine.util.math.MathUtils
.radToDeg(rotationAngle));
どうもありがとう