物理主体不遵循路径修改器

时间:2012-03-14 22:27:40

标签: android andengine

我有一个带身体的精灵。我想通过路径移动精灵。我尝试用PathModifier做这个,并且精灵移动它应该但它的身体不遵循精灵位置。

我可以将精灵和身体一起移动吗?我是否真的必须计算路径的速度并将其应用于身体对象,因为这似乎很难?

精灵像这样初始化

  ...
  this.sprite = new Sprite(this.x, this.y, textureRegion);
  this.rectangleBody = PhysicsFactory.createBoxBody(
        physicsWorld,
        this.sprite,
        BodyDef.BodyType.StaticBody,
        this.fixtureDef);
  pPhysicsConnector = new PhysicsConnector(this.sprite, rectangleBody, true, false);
  physicsWorld.registerPhysicsConnector(pPhysicsConnector);
  scene.attachChild(this.sprite);

然后随后移动此代码

     Shape shape = entity.getShape();
     Float[] reverseXPath = ArrayUtils.clone(xPath.toArray(new Float[xPath.size()]));
     ArrayUtils.reverse(reverseXPath);
     Float[] reverseYPath = ArrayUtils.clone(yPath.toArray(new Float[yPath.size()]));
     ArrayUtils.reverse(reverseYPath);
     SequenceEntityModifier oneSequence = new SequenceEntityModifier(
           new PathModifier(
                 duration,
                 new PathModifier.Path(
                       ArrayUtils.toPrimitive(xPath.toArray(new Float[xPath.size()])),
                       ArrayUtils.toPrimitive(yPath.toArray(new Float[yPath.size()]))),
                 IEaseFunction.DEFAULT),
           new PathModifier(
                 duration,
                 new PathModifier.Path(
                       ArrayUtils.toPrimitive(reverseXPath),
                       ArrayUtils.toPrimitive(reverseYPath)),
                 IEaseFunction.DEFAULT));
     shape.registerEntityModifier(new LoopEntityModifier(oneSequence));

2 个答案:

答案 0 :(得分:1)

我想你会发现这篇文章很有用

http://www.andengine.org/forums/post28936.html

答案 1 :(得分:0)

按路径移动精灵,更新this.scene.registerUpdateHandler中的主体(新的IUpdateHandler() 喜欢这里

https://stackoverflow.com/a/16122813/2233069

相关问题