【问题标题】:How to add dynamic text in AndEngine?如何在 AndEngine 中添加动态文本?
【发布时间】:2012-12-28 05:36:09
【问题描述】:

我正在使用 AndEngine 创建一个简单的游戏。我有以恒定速度移动的精灵。当精灵撞墙或在接触点相互碰撞时,我需要能够显示点 (+1)、(+2) 等。如何使用 Text 做到这一点?

我正在使用 AndEngine 示例“MovingBallExample.java”的修改版本。下面的代码显示了球在撞到墙上时如何反转方向。我需要在接触点处显示文本,最好以圆圈形式显示几秒钟。

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

欢迎提出任何想法。

【问题讨论】:

    标签: android text andengine sprite


    【解决方案1】:

    我找到了答案。我刚刚添加了以下代码

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

    “这个”在哪里 是精灵对象,我能够将文本设置在精灵从墙上反弹的大致位置

    【讨论】:

    • 接受你自己的答案作为正式化事情的答案。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    相关资源
    最近更新 更多