我有一個身體的精靈。我想通過路徑移動精靈。我嘗試過使用PathModifier來做這件事,而精靈會像它應該那樣移動,但它的身體不會跟隨精靈的位置。物理身體不遵循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));