Andengine 2d 横向卷轴角色移动
我这里有一个脚本:
final FixtureDef characterfictur = PhysicsFactory.createFixtureDef(0.5f, 0.1f,0.1f); // Maybe other fixture?
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
final Body body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, charactersprite, BodyType.DynamicBody, characterfictur);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(charactersprite, body, true, false));
mScene.attachChild(charactersprite);
createUnwalkableObjects(mTMXTiledMap);
// HUD
HUD my = new HUD();
Sprite forward = new Sprite( 50, CAMERA_HEIGHT - 170, forwardr) {
@Override
public boolean onAreaTouched(TouchEvent pEvent, float pX, float pY) {
if(!pEvent.isActionUp()) {
charactersprite.getTextureRegion().setFlippedHorizontal(false);
body.setLinearVelocity(new Vector2(CHAR_MOVING_SPEED,body.getLinearVelocity().y)); // Velocity
//body.applyLinearImpulse(new Vector2(2,0), body.getPosition());
} else {
//body.applyLinearImpulse(new Vector2(0,0), body.getPosition());
body.setLinearVelocity(new Vector2(0,body.getLinearVelocity().y)); // LinearVelocity
}
return false;
}
};
还有一点:
private void createUnwalkableObjects(TMXTiledMap map) {
// Loop through the object groups
for(final TMXObjectGroup group: map.getTMXObjectGroups()) {
//if(group.getTMXObjectGroupProperties().containsTMXProperty("Zeme", "true")){
// This is our "wall" layer. Create the boxes from it
for(final TMXObject object : group.getTMXObjects()) {
final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0,3f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
mScene.attachChild(rect);
}
//}
}
}
所以,当我移动角色时,它的移动非常奇怪。走路的时候会摇晃。那么,也许我需要更换固定装置,或者使用其他移动方法?
I have a script here:
final FixtureDef characterfictur = PhysicsFactory.createFixtureDef(0.5f, 0.1f,0.1f); // Maybe other fixture?
this.mScene.registerUpdateHandler(this.mPhysicsWorld);
final Body body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, charactersprite, BodyType.DynamicBody, characterfictur);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(charactersprite, body, true, false));
mScene.attachChild(charactersprite);
createUnwalkableObjects(mTMXTiledMap);
// HUD
HUD my = new HUD();
Sprite forward = new Sprite( 50, CAMERA_HEIGHT - 170, forwardr) {
@Override
public boolean onAreaTouched(TouchEvent pEvent, float pX, float pY) {
if(!pEvent.isActionUp()) {
charactersprite.getTextureRegion().setFlippedHorizontal(false);
body.setLinearVelocity(new Vector2(CHAR_MOVING_SPEED,body.getLinearVelocity().y)); // Velocity
//body.applyLinearImpulse(new Vector2(2,0), body.getPosition());
} else {
//body.applyLinearImpulse(new Vector2(0,0), body.getPosition());
body.setLinearVelocity(new Vector2(0,body.getLinearVelocity().y)); // LinearVelocity
}
return false;
}
};
and little forward :
private void createUnwalkableObjects(TMXTiledMap map) {
// Loop through the object groups
for(final TMXObjectGroup group: map.getTMXObjectGroups()) {
//if(group.getTMXObjectGroupProperties().containsTMXProperty("Zeme", "true")){
// This is our "wall" layer. Create the boxes from it
for(final TMXObject object : group.getTMXObjects()) {
final Rectangle rect = new Rectangle(object.getX(), object.getY(),object.getWidth(), object.getHeight());
final FixtureDef boxFixtureDef = PhysicsFactory.createFixtureDef(0, 0,3f);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
rect.setVisible(false);
mScene.attachChild(rect);
}
//}
}
}
So, when i move the character it moves very weird. It gitters when walks. So, maybe i need to change fixtures, or use other moving method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议使用fixedPhysicsWorld 和fixedStepEngine。这将消除“紧张感”。此外,当关卡首次开始时,您可能希望有一个“准备就绪!”的提示。提示等待几秒钟让物理引擎加速,因为它往往开始时有点慢然后加速。
I'd suggest using a fixedPhysicsWorld and a fixedStepEngine. That will get rid of the 'jitters' Also when the level first starts you might want to have a "Ready!" prompt that waits a few seconds to let the physics engine ramp up as it tends to start out a little slow then speeds up.