2

スプライトの本体を線の一部の線と一緒に動かしたいのですが、スプライトしか動かせませんが、本体が動いていません。

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

4 に答える 4

8

ボディを移動するために使用できbody.setLinearVelocity();ます。物理コネクタを指定しているため、スプライトは自動的にボディを追跡します。

于 2012-01-06T18:06:23.390 に答える
1

体を動かすには setTransform を使用する必要があります

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

xとyのデフォルトを32で割る必要があることを覚えておいてください

于 2011-08-29T16:14:19.790 に答える
0

あなたも行くことができます

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

box2dはピクセル単位で動作しないため、32で割る必要があるため、ピクセルにするには32で割る必要があります

于 2013-02-21T22:27:32.443 に答える