2011-07-23 63 views
2

我想要將精靈的主體與線的某些部分一起移動,只是我只能移動精靈但身體不動。如何使用andEngine移動精靈的主體?

public Scene onLoadScene() { 
    this.mEngine.registerUpdateHandler(new FPSLogger()); 
    final Scene scene = new Scene(2); 
    scene.setBackground(new ColorBackground(0.09804f, 0.00274f, 0.0784f)); 
    this .enableAccelerometerSensor(this); 
    this.sPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); 
    final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH,2); 
    final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2); 
    final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT); 
    final Shape right = new Rectangle(CAMERA_WIDTH-2, 0,2, CAMERA_HEIGHT); 


    PhysicsFactory.createBoxBody(this.sPhysicsWorld, ground, 
      BodyType.StaticBody, wallFixtureDef); 
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, roof, 
      BodyType.StaticBody, wallFixtureDef); 
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, left, 
      BodyType.StaticBody, wallFixtureDef); 
    PhysicsFactory.createBoxBody(this.sPhysicsWorld, right, 
      BodyType.StaticBody, wallFixtureDef); 
    scene.getFirstChild().attachChild(ground); 
    scene.getFirstChild().attachChild(roof); 
    scene.getFirstChild().attachChild(left); 
    scene.getFirstChild().attachChild(right); 

    final int centerX = (CAMERA_WIDTH - this.mFaceTextureRegion.getWidth())/2; 
    final int centerY = (CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight())/2; 
    final AnimatedSprite face = new AnimatedSprite(centerX - 100, centerY, this.mFaceTextureRegion); 
    final Body bodyRedBall = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face, 
      BodyType.DynamicBody, wallFixtureDef); 
    this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, bodyRedBall, true, true)); 
    scene.attachChild(face); 

    final AnimatedSprite face2 = new AnimatedSprite(100, 100, this.mFaceTextureRegion); 
    final Body bodyRedBall2 = PhysicsFactory.createCircleBody(this.sPhysicsWorld, face2, 
      BodyType.KinematicBody, wallFixtureDef); 
    final Path path4 = new Path(3).to(682, 223).to(482, 223).to(682, 223); 

    face2.registerEntityModifier(new LoopEntityModifier(new PathModifier(30, path4, null, new IPathModifierListener() { 
      @Override 
      public void onWaypointPassed(final PathModifier pPathModifier, final IEntity pEntity, final int pWaypointIndex) {    
      } 
     }))); 
    this.sPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face2, bodyRedBall2, true, true){ 
     @Override 
     public void onUpdate(final float pSecondsElapsed) { 
       super.onUpdate(pSecondsElapsed); 
       face2.setPosition(face2.getX(), face2.getY()); 
     } 
}); 


     scene.attachChild(face2);   
    scene.registerUpdateHandler(this.sPhysicsWorld); 
    return scene; 
} 

@Override 
public void onLoadComplete() { 
} 

@Override 
public void onAccelerometerChanged(AccelerometerData pAccelerometerData) { 
    // TODO Auto-generated method stub 
    final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getY(), pAccelerometerData.getX()); 
    this.sPhysicsWorld.setGravity(gravity); 
    Vector2Pool.recycle(gravity); 
} 

回答

1

移動身體必須使用的setTransform

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

,記住,你有32

8

劃分x和y默認可以使用body.setLinearVelocity();移動身體,精靈將自動跟隨物體,因爲您已指定物理連接器。

+0

由於劃分張貼。它真的起作用 –

0

你也可以去

yourBody.setTransform(x/32,y/32), 0);

,你需要通過32分,因爲Box2D的不以像素爲單位工作,以便得到它的像素則需要通過32

2
public class MoveBodyModifier extends MoveModifier { 

private Body mBody; 

public MoveBodyModifier(float pDuration, float pFromX, float pToX, float pFromY, float pToY, Body body) { 
    super(pDuration, pFromX, pToX, pFromY, pToY); 
    mBody = body; 
} 

@Override 
protected void onSetInitialValues(IEntity pEntity, float pX, float pY) { 
    mBody.setTransform(pX/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT, 
      pY/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT, mBody.getAngle()); 
} 

@Override 
protected void onSetValues(IEntity pEntity, float pPercentageDone, float pX, float pY) { 
    mBody.setTransform(pX/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT, 
      pY/ PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT, mBody.getAngle()); 
} 
} 
+0

解釋你的代碼通常很有用,而不是隻是將代碼塊轉儲到答案中。 – ydaetskcoR

+1

**說明:**擴展MoveModifier類並添加代碼以將變形體與精靈一起轉換/移動。現在將使用MoveBodyModifier而不是MoveModifier創建一個修飾符對象,但是,繼續在精靈上註冊實體修飾符而不是在body上註冊。像這樣:'MoveBodyModifier mm = new MoveBodyModifier(0.25f,fromX,toX,fromY,toY,this.playerBody); \t \t \t playerFace.registerEntityModifier(mm);'我一直在努力爭取一段時間,並給了這一個鏡頭。完美工作。謝謝拉米。 :-) – AndroidMechanic

+0

不客氣;) – Rami