Andengine 2d 横向卷轴角色移动

发布于 2024-12-22 10:48:58 字数 2190 浏览 2 评论 0原文

我这里有一个脚本:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帅哥哥的热头脑 2024-12-29 10:48:58

我建议使用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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文