Uday
Uday

Reputation: 1699

How to Move a ball using AndEngine?

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..??

Upvotes: 0

Views: 799

Answers (2)

Uday
Uday

Reputation: 1699

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

}

Upvotes: 1

yushulx
yushulx

Reputation: 12140

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.

Upvotes: 0

Related Questions