bilal ahmed
bilal ahmed

Reputation: 31

Bouncing object in Android

I want a program where an object (sphere) falls from top of screen, bounces back few times and settles in the middle of screen. I am new developer in android. Can anyone help me?

Upvotes: 0

Views: 681

Answers (1)

Viacheslav Rodionov
Viacheslav Rodionov

Reputation: 2345

Well, it has been said already this is not an Android-specific question, but there's an Android-specific answer. This sample is from AndEngine's framework examples. It will give you an idea how to bounce:

        private static class Ball extends AnimatedSprite {
            private final PhysicsHandler mPhysicsHandler;

            public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion) {
                    super(pX, pY, pTextureRegion);
                    this.mPhysicsHandler = new PhysicsHandler(this);
                    this.registerUpdateHandler(this.mPhysicsHandler);
            }

            @Override
            protected void onManagedUpdate(final float pSecondsElapsed) {
                    if(this.mX < 0) {
                            this.mPhysicsHandler.setVelocityX(DEMO_VELOCITY);
                    } else if(this.mX + this.getWidth() > CAMERA_WIDTH) {
                            this.mPhysicsHandler.setVelocityX(-DEMO_VELOCITY);
                    }

                    if(this.mY < 0) {
                            this.mPhysicsHandler.setVelocityY(DEMO_VELOCITY);
                    } else if(this.mY + this.getHeight() > CAMERA_HEIGHT) {
                            this.mPhysicsHandler.setVelocityY(-DEMO_VELOCITY);
                    }

                    super.onManagedUpdate(pSecondsElapsed);
            }
    }

Upvotes: 1

Related Questions