动画精灵的andengine setposition

时间:2013-05-19 16:19:28

标签: 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)。

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

setTransform正在工作,但它的值应除以像素与米的比例,默认为32.0f,或者您可以使用PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT。另请注意,setTransform使用body的中心作为anchorcenter,所以如果你使用GLES2分支(而不是GLES2-AnchorCenter),你应该减去精灵的宽度和高度的一半,例如:

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