Android 和动画精灵的引擎设置位置

Android 和动画精灵的引擎设置位置,android,box2d,andengine,Android,Box2d,Andengine,我想用安德尼创造一场足球比赛, 当球进入球门并从屏幕上消失时,我想更新比分并将球重新定位在屏幕中央。 在工作正常时更新分数,但不更新重新定位。 以下是球的代码: final AnimatedSprite ball; final Body bodyBall; ball = new AnimatedSprite(centerX, centerY,this.mBallTextureRegion, this.getVertexBufferObjectManager());

我想用安德尼创造一场足球比赛, 当球进入球门并从屏幕上消失时,我想更新比分并将球重新定位在屏幕中央。 在工作正常时更新分数,但不更新重新定位。 以下是球的代码:

    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(100100,0)。

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

bodyBall.setTransform(新向量2(x/32,y/32),0);

您需要除以32,因为box2d不以像素为单位运行,Body类没有方法setPosition。 要设置新坐标,请使用

bodyBall.setTransform(新向量2(x/32,y/32),0);

您需要除以32,因为box2d不以像素为单位运行。setTransform正在工作,但它的值应除以像素与米的比率,默认值为32.0f,或者您可以使用physicConstants.pixel\u to\u meter\u ratio\u default。还请注意,setTransform使用主体的中心作为锚定中心,因此如果使用的是GLES2分支(不是GLES2锚定中心),则应减去精灵宽度和高度的一半,例如:

yourBody.setTransform((yourNewX - yourSprite.getWidth() / 2) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, (yourNewY - yourSprite.getHeight() / 2) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, yourBody.getAngle());

setTransform正在工作,但其值应除以像素米比率,默认情况下为32.0f,或者您可以使用physicConstants.pixel米比率默认值。还请注意,setTransform使用主体的中心作为锚定中心,因此如果使用的是GLES2分支(不是GLES2锚定中心),则应减去精灵宽度和高度的一半,例如:

yourBody.setTransform((yourNewX - yourSprite.getWidth() / 2) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, (yourNewY - yourSprite.getHeight() / 2) / PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, yourBody.getAngle());

32是org.andengine.extension.physics.box2d.util.constants32中的常量physicConstants.PIXEL\u与\u METER\u比率\u默认值是org.andengine.extension.physics.box2d.util.constants中的常量physicConstants.PIXEL\u与\u METER\u比率\u默认值