我在下面使用ANDEngine
的代码假设在SimpleBaseGameActivity
上工作正常,其中2 Sprite
将动画并移动。但是,因为它声明了一个surfaceGestureDetector
对象,图像会变形。我甚至没有使用那个对象。通过评论
gDetector = new SurfaceGestureDetector(this){...}
一切都变得正常了。有人可以建议出了什么问题吗?谢谢!
public class MainActivity extends SimpleBaseGameActivity
{
private Camera camera;
private static final int CAMERA_WIDTH = 800;
private static final int CAMERA_HEIGHT = 480;
private BitmapTextureAtlas yourTexture;
private TiledTextureRegion yourTextureRegion;
private SurfaceGestureDetector gDetector;
@Override
public EngineOptions onCreateEngineOptions()
{
camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
new FillResolutionPolicy(), camera);
engineOptions.getTouchOptions().setNeedsMultiTouch(true);
return engineOptions;
}
@Override
public void onCreateResources()
{
loadGraphics();
}
private void loadGraphics()
{
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
yourTexture = new BitmapTextureAtlas(getTextureManager(), 50, 200, TextureOptions.BILINEAR);
yourTextureRegion =BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(yourTexture, this, "snowman_animate.png", 0, 0, 1, 4);
yourTexture.load();
}
@Override
public Scene onCreateScene() {
Scene scene = new Scene();
scene.setBackground(new Background(0.6004f, 0.6274f, 0.8784f));
gDetector = new SurfaceGestureDetector(this){
@Override
protected boolean onSwipeUp() {
return false;
}
@Override
protected boolean onSwipeRight() {
return false;
}
@Override
protected boolean onSwipeLeft() {
return false;
}
@Override
protected boolean onSwipeDown() {
return false;
}
@Override
protected boolean onSingleTap() {
return false;
}
@Override
protected boolean onDoubleTap() {
return false;
}
};
MySprite yourSprite = new MySprite(200, 400, yourTextureRegion, this.getVertexBufferObjectManager());
MySprite yourSprite2 = new MySprite(300, 200, yourTextureRegion, this.getVertexBufferObjectManager());
long[] frameDurration = {250, 250, 250, 250};
yourSprite.animate(frameDurration);
yourSprite2.animate(frameDurration);
scene.attachChild(yourSprite);
scene.attachChild(yourSprite2);
return scene;
}
private static class MySprite extends AnimatedSprite {
private final PhysicsHandler mPhysicsHandler;
public MySprite
(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(0, 120);
}
@Override
protected void onManagedUpdate(final float pSecondsElapsed) {
if(this.mY < 0) {
this.mPhysicsHandler.setVelocityY(120);
} else if(this.mY + this.getHeight() > CAMERA_HEIGHT) {
this.mPhysicsHandler.setVelocityY(-120);
}
super.onManagedUpdate(pSecondsElapsed);
}
}
}
答案 0 :(得分:0)
找到解决我问题的方法。虽然我不明白为什么。我需要在onCreate而不是onCreateScene中声明它,如下所示。
public void onCreate(final Bundle pSavedInstanceState) {
super.onCreate(pSavedInstanceState);
gDetector = new SurfaceGestureDetector(this){
@Override
protected boolean onSwipeUp() {
return false;
}
@Override
protected boolean onSwipeRight() {
return false;
}
@Override
protected boolean onSwipeLeft() {
return false;
}
@Override
protected boolean onSwipeDown() {
return false;
}
@Override
protected boolean onSingleTap() {
return false;
}
@Override
protected boolean onDoubleTap() {
return false;
}
};
}