3
我有Sprite叫做「槍」,我想通過用戶觸摸旋轉它。
我的問題:當我觸摸和向下移動它旋轉,做工不錯,但是當我觸摸和移動了它不會轉動。
這裏是我的代碼:AndEngine觸摸旋轉
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));
非常感謝
你的筆記只適用於OP想用物理進行遊戲的情況,這裏可能不是這種情況。 – JohnEye 2012-08-22 20:02:37
好的,因爲我看到使用AndEngine的OP而不是本地繪圖表面,所以我在這裏建議使用引擎的消耗品。對於第一次看,我認爲'JustMe'與之前的情況是一樣的,因爲在嘗試投入大量數學時,浪費了很多時間來修復運行時問題。 – 2012-08-23 01:54:43
當然,沒關係。我只是想確保OP不會使用Box2D只是爲了避免進行一些數學運算,因爲這將是一個巨大的矯枉過正和性能/內存的巨頭。你的答案寫得很好,很有道理。 – JohnEye 2012-08-23 09:31:19