如何使用AndEngine移动球?

时间:2013-08-01 07:23:48

标签: android andengine

它在screnn上显示球但无法移动。

我想在加速度计改变时移动球。

我该怎么做?

提前致谢。

public class MainActivity extends SimpleBaseGameActivity implements IAccelerometerListener  {
    private static int CAMERA_WIDTH = 800;
    private static int CAMERA_HEIGHT = 480;
    Context ctx;
    Sprite ballSprite;

    private ITextureRegion mBackgroundTextureRegion,ball, mTowerTextureRegion, mRing1, mRing2, mRing3;


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

    @Override
    protected void onCreateResources() {
        try {
            ITexture ballTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
                @Override
                public InputStream open() throws IOException {
                    return getAssets().open("ball.png");
                }
            });

            ballTexture.load();

            this.ball = TextureRegionFactory.extractFromTexture(ballTexture);
            this.enableAccelerationSensor((IAccelerationListener) this);

        } catch (IOException e) { e.printStackTrace(); }        
    }

    @Override
    protected Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        final Scene scene = new Scene();

        ballSprite = new Sprite(0, 0, this.ball, getVertexBufferObjectManager());
        scene.attachChild(ballSprite);      
        return scene;
    }

    @Override
    public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
        ballSprite.setPosition(ballSprite.getX() + pAccelerometerData.getX(),
                               ballSprite.getY() + pAccelerometerData.getY());
    }

球根本没动。我只是想让它随着加速度计的变化而移动。 我做错了什么.. ??

2 个答案:

答案 0 :(得分:1)

得到了解决方案。

更改了以下代码。

而不是实现IA加速度监听器使用的IAccelerometerListener。

public class MainActivity extends SimpleBaseGameActivity implements IAccelerationListener  {
private static int CAMERA_WIDTH = 800;
private static int CAMERA_HEIGHT = 480;
Context ctx;
Sprite ballSprite;

private ITextureRegion mBackgroundTextureRegion,ball, mTowerTextureRegion, mRing1, mRing2, mRing3;


@Override
public EngineOptions onCreateEngineOptions() 
{
    // TODO Auto-generated method stub
    final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);

}

@Override
protected void onCreateResources() 
{

    try {

        ITexture ballTexture = new BitmapTexture(this.getTextureManager(), new IInputStreamOpener() {
            @Override
            public InputStream open() throws IOException {
                return getAssets().open("ball.png");
            }
        });

        ballTexture.load();

        this.ball = TextureRegionFactory.extractFromTexture(ballTexture);

        this.enableAccelerationSensor(this); //Enable Sensor here

} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }       
}

@Override
protected Scene onCreateScene() {
    // TODO Auto-generated method stub
    this.mEngine.registerUpdateHandler(new FPSLogger());      
    final Scene scene = new Scene();    
    ballSprite = new Sprite(0, 0, this.ball, getVertexBufferObjectManager());
    final PhysicsHandler physicsHandler = new PhysicsHandler(ballSprite);
    ballSprite.registerUpdateHandler(physicsHandler);
    scene.attachChild(ballSprite);      
    return scene;
}

@Override
public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
    // TODO Auto-generated method stub

}

@Override
public void onAccelerationChanged(AccelerationData pAccelerationData) {
    // TODO Auto-generated method stub
    Log.d("x=","x="+pAccelerationData.getX());
    ballSprite.setPosition(
            ballSprite.getX() + pAccelerationData.getX(),
            ballSprite.getY() + pAccelerationData.getY()
            );
    //body.setLinearDamping(1.5f);

}

答案 1 :(得分:0)

您的代码是否已被调用?

public void onAccelerometerChanged(AccelerometerData pAccelerometerData) {
    // TODO Auto-generated method stub
    ballSprite.setPosition(
            ballSprite.getX() + pAccelerometerData.getX(),
            ballSprite.getY() + pAccelerometerData.getY()
            );
}

你检查过里面的日志了吗? x和y的值是多少?如果已调用此代码块,并且x和y也正常,请检查您的精灵是否已经无效。