我有一个有 body 的 Sprite 。我想通过路径移动 Sprite 。我已经尝试使用 PathModifier 执行此操作, Sprite 会按原样移动,但它的 body 不会跟随 Sprite 的位置。
我可以将 Sprite 和 body 一起移动吗?我真的必须计算路径的速度并将其应用于主体对象,因为这看起来很难吗?
Sprite 是这样初始化的
...
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));
最佳答案
我想你会发现这篇文章很有用
关于android - 物理 body 不遵循路径修改器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711259/