Andengine 2d 横向卷轴游戏,如马里奥

发布于 2024-12-22 12:47:18 字数 2817 浏览 3 评论 0原文

我有一个问题。 我如何使角色具有重力并可以正常行走,即我需要使用哪些功能以及如何定义固定装置?我需要 box2d 物理世界吗(我正在使用平铺地图)? 所以,如果可以的话,请告诉我如何使用 andengine 制作像马里奥这样的 2D 横向滚动平台游戏。

我的代码我正在尝试做的事情:

    // Character:
    charactersprite = new Sprite(40, 0, this.character);
    charactersprite.setScaleX(0.65f);

    this.mScene.setOnSceneTouchListener( this);

    // PHYSICS
    final FixtureDef characterfictur = PhysicsFactory.createFixtureDef(0, 0f,0.5f);


    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);
    final PhysicsHandler physicsHandler = new PhysicsHandler(charactersprite);
    charactersprite.registerUpdateHandler(physicsHandler);


    // 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)); // Don't look at there
            //body.applyLinearImpulse(new Vector2(2,0), body.getPosition());

            }else{
                //body.applyLinearImpulse(new Vector2(0,0), body.getPosition());
                physicsHandler.setVelocity(0, 0);
                body.setLinearVelocity(new Vector2(0,body.getLinearVelocity().y)); // Don't look at there 
            }
            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,1f);
                            PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
                            rect.setVisible(false);
                            mScene.attachChild(rect);
                     }
             //}
     }

}

所以它无法正常工作。那么我做错了什么?请帮我。 非常感谢!

I have a question there.
How i make character with gravity and available to walk properly i.e. what functions i need to use and how do i define fixtures? And do i need box2d physics world(i'm using tiled maps)?
So if you can, please tell me how to do 2d side scrolling platformer like mario with andengine.

My code what i'm trying to do :

    // Character:
    charactersprite = new Sprite(40, 0, this.character);
    charactersprite.setScaleX(0.65f);

    this.mScene.setOnSceneTouchListener( this);

    // PHYSICS
    final FixtureDef characterfictur = PhysicsFactory.createFixtureDef(0, 0f,0.5f);


    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);
    final PhysicsHandler physicsHandler = new PhysicsHandler(charactersprite);
    charactersprite.registerUpdateHandler(physicsHandler);


    // 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)); // Don't look at there
            //body.applyLinearImpulse(new Vector2(2,0), body.getPosition());

            }else{
                //body.applyLinearImpulse(new Vector2(0,0), body.getPosition());
                physicsHandler.setVelocity(0, 0);
                body.setLinearVelocity(new Vector2(0,body.getLinearVelocity().y)); // Don't look at there 
            }
            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,1f);
                            PhysicsFactory.createBoxBody(this.mPhysicsWorld, rect, BodyType.StaticBody, boxFixtureDef);
                            rect.setVisible(false);
                            mScene.attachChild(rect);
                     }
             //}
     }

}

So it didn't work properly. So what i'm doing wrong? Please help me.
Thank you very much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

烟凡古楼 2024-12-29 12:47:18

你需要的东西:

  • BoundCamera(为你的地图设定边界)
  • 追逐实体(你的玩家,所以相机会跟随你的实体)
  • FixtureDef 具有很小的弹性(以防止玩家突然停在地面上)
  • 为你的静态对象创建盒体(例如例如墙壁等)
  • 诸如跳跃之类的东西 - 只需使用 setLinearVelocity

上面提到的每个“功能”都有其示例 - 只需检查和引擎示例。

在这篇文章中,我提供了一些有关如何编写此类游戏的更多提示: 点击

Things you will need:

  • BoundCamera (to make bounds for your map)
  • chase entity (your player, so camera will follow your entity)
  • FixtureDef with little elasticity (to prevent player stopping suddenly on the ground)
  • create box bodies for your static objects (such as walls and so on)
  • things like jump - simply use setLinearVelocity

Every 'feature' mentioned above, has its example - simply check and engine examples.

In this thread, I provided some more tips about how to code such game: CLICK

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