0

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

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

我怎样才能做到这一点 ??

提前致谢。

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());
    }

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

4

2 回答 2

1

得到解决方案。

更改代码如下。

实施 IAccelerometerListener 时使用了 IAccelerationListener。

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);

}
于 2013-08-01T07:56:01.140 回答
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也正常,请检查您是否使您的精灵无效。

于 2013-08-01T07:31:09.750 回答