android - 和动画 Sprite 的引擎设置位置

标签 android box2d andengine

我正在尝试使用 andengine 创建一款足球游戏, 当球进入球门并从屏幕上消失时,我想更新比分并将球重新定位在屏幕中央。 更新分数工作良好,但不是重新定位。 这是球的代码:

    final AnimatedSprite ball;
    final Body bodyBall;
    ball = new AnimatedSprite(centerX, centerY,this.mBallTextureRegion, this.getVertexBufferObjectManager());
    bodyBall = PhysicsFactory.createBoxBody(this.mPhysicsWorld, ball, BodyType.DynamicBody, FIXTURE_DEF);
    ball.setUserData(bodyBall);
    ball.animate(200);
    this.mScene.registerTouchArea(ball);
    this.mScene.attachChild(ball);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, bodyBall, true, true));

这里是检查目标的代码:

    @Override
    public void onUpdate(final float pSecondsElapsed) {
        if(UpperGoal.collidesWith(ball)) {
            ScoreA = ScoreA+1;
            ScoreText.setText(ScoreA+" - " + ScoreB);
            bodyBall.setTransform(new Vector2(CAMERA_WIDTH/2,CAMERA_HEIGHT/2),0);               
            UpperGoal.setColor(1, 0, 0);

我也试试 bodyBall.setPosition(100,100,0)。

最佳答案

Body 类没有方法 setPosition。 要设置新坐标,请使用

bodyBall.setTransform(new Vector2(x/32,y/32), 0);

你需要除以 32,因为 box2d 不以像素为单位运行

关于android - 和动画 Sprite 的引擎设置位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16636780/

相关文章:

android - 类似于谷歌地图的 map fragment 中的向上滑动面板

java - openGL以伪3D视角绘制扭曲的 Sprite (图像)

java - 如何在 Intent 中传递 Parcelable?

iphone - Cocos2d CCSpirte runAction问题

Java/LibGDX : Get custom object properties on box2D collision

ios - b2Body 移动时更新 CC Sprite 位置?

android - AndEngine中的投影

Android AndEngine 动态改变文本

android - 如何在 linux 内核模式下将 '\n' 传递到文件中

android - 移动 Android 手机应用程序上的保持按钮