Tinniam V. Ganesh
Tinniam V. Ganesh

Reputation: 2077

How to add dynamic text in AndEngine?

I am creating a simple game using AndEngine. I have sprites that move around with constant velocity. I need to be able to display points (+1),(+2) etc when a sprite hits a wall or collides with one another at the point of contact. How can I do this using Text?

I am using a modified version of the AndEngine Example "MovingBallExample.java". The code below shows how the ball reverses direction when it hits a wall. I need to display Text at the point of contact preferably in a circle for a couple of seconds.

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

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

    @Override
    protected void onManagedUpdate(final float pSecondsElapsed) {
        if(this.mX < 0) {
            this.mPhysicsHandler.setVelocityX(BALL_VELOCITY);
        } else if(this.mX + this.getWidth() > CAMERA_WIDTH) {
                             //**** Need to add Points text here **********/
            this.mPhysicsHandler.setVelocityX(-BALL_VELOCITY);
        }

        if(this.mY < 0) {
            this.mPhysicsHandler.setVelocityY(BALL_VELOCITY);
        } else if(this.mY + this.getHeight() + 80 > CAMERA_HEIGHT) {
                             //**** Need to add Points text here **********/
            this.mPhysicsHandler.setVelocityY(-BALL_VELOCITY);
        }

Any and all thoughts are welcome.

Upvotes: 0

Views: 1286

Answers (1)

Tinniam V. Ganesh
Tinniam V. Ganesh

Reputation: 2077

I have found the answer. I just added the following code

            x = this.getX();
            y = this.getY();
            bText.setPosition(x+10,y+10);               
            bText.setText("+1");

where "this" is the sprite object and I was able to set the text at the approximate position where the sprite bounces off a wall

Upvotes: 4

Related Questions