2013-05-19 42 views
1

with andengine我试图创建一个足球比赛, 当球进球并消失在屏幕上时,我想更新比分并将球重新定位在屏幕中央。 更新得分运行良好,但不是调整位置。 这里是球代码:我也尝试bodyBall.setPosition(100,100,0)andengine setposition of animatedsprite

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

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

,这里的代码来检查目标。

回答

1

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

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

你需要通过32因为Box2D的不以像素为单位

+1

32是org.andengine.extension.physics.box2d.util.constants中的常量PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT – Alexey

0

的setTransform正在运行,但它的价值应该由像素可分为计量比,通过分默认值是32.0f,或者你可以使用PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT。还要注意的是,一个setTransform用身体的中心,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());