0

It's displaying ball on the screnn but not able to move.

I want to move the ball when Accelerometer changed .

How can i do this ??

Thanks in advance.

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

Ball is not moving at all. Just i mant to make it move with the accelerometer change. Am i doing something wrong in it..??

sschrass
  • 7,014
  • 6
  • 43
  • 62
Uday
  • 1,619
  • 3
  • 23
  • 48
  • 1
    Please visit [this][1] question for your problem, Hope It will help [1]: http://stackoverflow.com/questions/6479637/android-accelerometer-moving-ball – Kirtikumar A. Aug 01 '13 at 07:27
  • Did you check andengine samples? http://code.google.com/p/andengineexamples/ – dilix Aug 01 '13 at 07:31

2 Answers2

1

Got the Solution .

Changed the code as below .

Insted of implementing IAccelerometerListener used 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);

}
Uday
  • 1,619
  • 3
  • 23
  • 48
0

whether your code has been called?

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

Have you checked the log inside? what is the value of x and y? If this code block has been called, and the x and y is also normal, check whether you have invalidated your sprite.

yushulx
  • 11,695
  • 8
  • 37
  • 64
  • Nope it's not beeing called, I made this canges and tried . How do i call it ..?? `@Override public void onAccelerometerChanged(AccelerometerData pAccelerometerData) { // TODO Auto-generated method stub Log.d("x=","x="+pAccelerometerData.getX()); ballSprite.setPosition( ballSprite.getX() + pAccelerometerData.getX(), ballSprite.getY() + pAccelerometerData.getY() ); }` – Uday Aug 01 '13 at 07:37
  • use [SensorManager](http://developer.android.com/reference/android/hardware/SensorManager.html). check Google's doc to help you get sensor event. :) – yushulx Aug 01 '13 at 07:41
  • Probably AndEngine has encapsulated relevant API. You can also check AndEngine docs. – yushulx Aug 01 '13 at 07:46