我正在使用 AndEngine 并尝试检测游戏中的滑动手势。andengine中有一个类(SurfaceGestureDetector.java)。但我不知道如何使用它。当我尝试在 onCreateScene() 方法中实例化时,它会引发异常。是否为它提供了任何方法或内部类或任何其他方法来检测和引擎中的滑动。这是我的代码。
public class AETopDownBackground extends SimpleBaseGameActivity implements IOnSceneTouchListener{
private int CAMERA_WIDTH=800;
private int CAMERA_HEIGHT=480;
private Camera mCamera;
private Scene mScene;
private BuildableBitmapTextureAtlas atlas,txchrAtlas;
private TextureRegion trHill;
//private Sprite mSpriteHill;
private AnimatedSprite mSpriteMan;
private TiledTextureRegion trTiledMan;
@Override
public EngineOptions onCreateEngineOptions() {
// TODO Auto-generated method stub
mCamera=new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), mCamera);
}
@Override
protected void onCreateResources() {
// TODO Auto-generated method stub
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
atlas= new BuildableBitmapTextureAtlas(getTextureManager(), 800, 1300);
trHill=BitmapTextureAtlasTextureRegionFactory.createFromAsset(atlas, this, "road_bg.png");
try {
atlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
} catch (TextureAtlasBuilderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
atlas.load();
txchrAtlas= new BuildableBitmapTextureAtlas(getTextureManager(), 256, 92,TextureOptions.BILINEAR);
trTiledMan= BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(txchrAtlas, this, "player_forword.png",4, 1);
try {
txchrAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
} catch (TextureAtlasBuilderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
txchrAtlas.load();
}
@Override
protected Scene onCreateScene() {
// TODO Auto-generated method stub
this.mEngine.registerUpdateHandler(new FPSLogger());
mScene = new Scene();
mScene.setBackground(new Background(Color.CYAN));
mScene.setOnSceneTouchListener(this);
//gdetectorl= new MySurfaceSwipeDetector(getApplicationContext());
//final float textureHeight = trHill.getHeight();
/* Create the hill which will appear to be the furthest
* into the distance. This Sprite will be placed higher than the
* rest in order to retain visibility of it */
Sprite hillFurthest = new Sprite(0, 0, trHill,
mEngine.getVertexBufferObjectManager());
//ParallaxBackground background = new ParallaxBackground(0.3f, 0.3f,0.9f) {
AutoParallaxBackground background = new AutoParallaxBackground(0.3f,0.3f, 0.9f, 50){
/* We'll use these values to calculate the parallax value of the
background */
float cameraPreviousY = 0;
float parallaYValueOffset = 0;
/* onUpdates to the background, we need to calculate new
* parallax values in order to apply movement to the background
* objects (the hills in this case) */
@Override
public void onUpdate(float pSecondsElapsed) {
/* Obtain the camera's current center X value */
final float cameraCurrentY = mCamera.getCenterY();
/* If the camera's position has changed since last
* update... */
if (cameraPreviousY != cameraCurrentY) {//cameraPreviousX != cameraCurrentX
/* Calculate the new parallax value offset by
* subtracting the previous update's camera x coordinate
* from the current update's camera x coordinate */
parallaYValueOffset += cameraCurrentY - cameraPreviousY;
//parallaxValueOffset=parallaxValueOffset+10;
/* Apply the parallax value offset to the background, which
* will in-turn offset the positions of entities attached
* to the background */
this.setParallaxValue(parallaYValueOffset);
System.out.println("Camera parallaxValueOffset:"+parallaYValueOffset);
//this.setParallaxValue(0);
/* Update the previous camera X since we're finished with
this
* update */
cameraPreviousY = cameraCurrentY;
}
// this.setParallaxValue(2);
super.onUpdate(pSecondsElapsed);
}
};
//negative value in this method indicates movement.To change the direction of movement use (+)positive value
background.attachParallaxEntity(new ParallaxEntity(10,hillFurthest));
/* Set & Enabled the background */
mScene.setBackground(background);
mScene.setBackgroundEnabled(true);
mSpriteMan = new AnimatedSprite(310, 250, trTiledMan, getVertexBufferObjectManager());
mSpriteMan.setScale(3);
mSpriteMan.animate(100);
mScene.attachChild(mSpriteMan);
return mScene;
}
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
if(pSceneTouchEvent.isActionUp()){
//Toast.makeText(this, "touched", Toast.LENGTH_SHORT).show();
mSpriteMan.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
}
return true;
}
} @Override
protected boolean onSwipeLeft() {
// TODO Auto-generated method stub
onSurfaceGesture("test");
return true;
}
@Override
protected boolean onSwipeDown() {
// TODO Auto-generated method stub
onSurfaceGesture("test");
return true;
}
@Override
protected boolean onSingleTap() {
// TODO Auto-generated method stub
onSurfaceGesture("test");
return true;
}
@Override
protected boolean onDoubleTap() {
// TODO Auto-generated method stub
onSurfaceGesture("test");
return true;
}
};
this.mSGDA.setEnabled(true);
return mScene;
}
@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
// TODO Auto-generated method stub
if(pSceneTouchEvent.isActionUp()){
//Toast.makeText(this, "touched", Toast.LENGTH_SHORT).show();
//mSpriteMan.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
}
return true;
}
private void onSurfaceGesture(final String str){
try {
Looper.prepare();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Handler().post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
System.out.println("Action is :"+str);
}
});
}