大通播放器相机AndEngine,并限制全球范围 [英] Chase player with camera in AndEngine and limit world's bounds

查看:233
本文介绍了大通播放器相机AndEngine,并限制全球范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AndEngine为Android,我想有我的场景是这样的:

Using AndEngine for Android, I would like to have my scene look like this:

红色框是世界必须被限制在一个给定的尺寸,比如 2000px * 450px

The red box is the world which must be limited to a given size, say 2000px*450px.

蓝盒子是摄像机,这也限制(像往常一样),例如 750px * 450px

The blue box is the Camera, which is limited as well (as usual), for example to 750px*450px.

有关整个场景,我有一个背景图像,这正是 450px 高。所以,我的摄像机可以扩展到任何大小很合适,但背景必须完全适合的高度。在摄像机的宽度是可变的。

For the whole scene, I have a background image that is exactly 450px high. So my Camera can be scaled to whatever size is appropriate, but the background must exactly fit to the height. The width of the Camera may be variable.

的播放机(圆)必须始终是在中心(水平),但可能没有留出世界上边界

The player (circle) must always be in the center (horizontally) but may not leave the world's boundaries.

要做到这一点,我已经试过增加大小两种类型:

To achieve this, I've tried adding two types of sizes:

  • 在摄像头的大小( CAMERA_WIDTH CAMERA_HEIGHT
  • 在世界的大小( WORLD_WIDTH WORLD_HEIGHT
  • camera size (CAMERA_WIDTH, CAMERA_HEIGHT)
  • world size (WORLD_WIDTH, WORLD_HEIGHT)

而这个功能是边界添加到世界中,这样的物理引擎$ P $离开这些边界pvents播放器:

And this function was to add boundaries to the world so that the physics engine prevents the player from leaving those boundaries:

private void createWorldBoundaries() {
    Body body;
    final Rectangle wall_top = new Rectangle(0, WORLD_HEIGHT-5, WORLD_WIDTH, 10, mVertexManager);
    final Rectangle wall_bottom = new Rectangle(0, 5, WORLD_WIDTH, 10, mVertexManager);
    final Rectangle wall_left = new Rectangle(5, 0, 10, WORLD_HEIGHT, mVertexManager);
    final Rectangle wall_right = new Rectangle(WORLD_WIDTH-5, 0, 10, WORLD_HEIGHT, mVertexManager);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_top, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_top.setUserData(body);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_bottom, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_bottom.setUserData(body);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_left, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_left.setUserData(body);
    body = PhysicsFactory.createBoxBody(mPhysicsWorld, wall_right, BodyType.StaticBody, new PhysicsFactory.createFixtureDef(0.0f, 0.5f, 0.5f));
    wall_right.setUserData(body);
    attachChild(wall_top);
    attachChild(wall_bottom);
    attachChild(wall_left);
    attachChild(wall_right);
}

<打击>但是,这是行不通的,很遗憾。(参见编辑)

将相机设置为追逐玩家有错误的结果对我来说:玩家是否真的留在了屏幕上的所有时间的中心,但我想玩家只留在中心的水平,而不是垂直

Setting the camera to chase the player has the wrong result for me: The player does really stay in the center of the screen all time, but I want the player only to stay in the center horizontally, not vertically.

我是什么做错了,我有什么可以改变?而基本的问题是:我怎样才能让这个世界比相机更广泛地来看,而高度等于相机视图。结果应该是,你可以通过水平你的世界(移动摄像头)走路,你总能看到完整的高度。

What am I doing wrong and what can I change? And the basic question is: How can I make the world wider than the camera view, while the height is equal to the camera view. The result should be that you can horizontally walk through your world (moving camera) and you can always see the full height.

编辑:

当你定义矩形的中心,而不是其左上角的坐标,你必须像这样做,似乎:

As you define the coordinates of the Rectangle's center and not its top-left corner, you have to do it like this, it seems:

final Rectangle wall_top = new Rectangle(WORLD_WIDTH/2, WORLD_HEIGHT-1, WORLD_WIDTH, 2, mVertexManager);
final Rectangle wall_bottom = new Rectangle(WORLD_WIDTH/2, FIELD_BASELINE_Y+1, WORLD_WIDTH, 2, mVertexManager);
final Rectangle wall_left = new Rectangle(1, WORLD_HEIGHT/2, 2, WORLD_HEIGHT, mVertexManager);
final Rectangle wall_right = new Rectangle(WORLD_WIDTH-1, WORLD_HEIGHT/2, 2, WORLD_HEIGHT, mVertexManager);

不过,我已经发现了一些教程其他的解决办法。难道这些作者没有测试他们的code写的教程之前,还是没从GLES1行为变化GLES2或与任何新版本?

However, I had found the other solution in several tutorials. Are these authors not testing their code before writing the tutorials or did the behaviour change from GLES1 to GLES2 or with any recent version?

推荐答案

我认为你对这个世界的边界问题是自我回答,不是吗?

i think your question about the world boundaries is self answered, isn't it?

PhysicsWorld边界

为进一步的研究可以从Play商店下载Nicolas的AndEngine示例应用程序,并期待不同的例子在这里(GLES_2,没有考虑到AnchorCenter还):的https://github.com/nicolasgramlich/AndEngineExamples/tree/GLES2/src/org/andengine/examples

for further research you can download nicolas' AndEngine Examples App from the Play Store and look up the different examples here (GLES_2, didn't look for AnchorCenter yet): https://github.com/nicolasgramlich/AndEngineExamples/tree/GLES2/src/org/andengine/examples

从PhysicsExample服用,code的矩形应该是这样的,如果边界被设定在摄像头范围。你的情况,你可以扩展宽度像你想(3次CAMERA_WIDTH?)

Taken from the PhysicsExample, the code for the rectangles should look like this, if the bounds are set to the camera bounds. in your case, you can extend width like you want (3 times CAMERA_WIDTH?)

    final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, WORLD_WIDTH, 2, vertexBufferObjectManager);
    final Rectangle roof = new Rectangle(0, 0, WORLD_WIDTH, 2, vertexBufferObjectManager);
    final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
    final Rectangle right = new Rectangle(WORLD_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);

摄像头以下的球员 您的相机按照您的播放器,你可以查找的BoundCameraExample的code <一个href="https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/BoundCameraExample.java" rel="nofollow">https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/BoundCameraExample.java

Camera following player for the Camera to follow your player, you can lookup the code of the BoundCameraExample https://github.com/nicolasgramlich/AndEngineExamples/blob/GLES2/src/org/andengine/examples/BoundCameraExample.java

为你最有趣的部分应该是在底部的addFace方法

the interesting part for you should be the addFace method at the bottom

private void addFace(final float pX, final float pY) {
    final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
final AnimatedSprite face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager()).animate(100);
final Body body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, objectFixtureDef);
this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));

this.mBoundChaseCamera.setChaseEntity(face);
}

该方法您的播放器(在这种情况下,盒装面)创建一个物理体+雪碧,并将精灵作为chaseEntity相机跟随。由于相机有一定的限度,它不能超越,你的相机将有你的PhysicWorld边界的高度,你可以用它来让你的相机跟随玩家x中,但不是在y轴方向。

this method creates a physics body + sprite for "your player" (in this case, a boxed face) and sets the sprite as a chaseEntity for the camera to follow. Since the camera has bounds, that it can't exceed and your camera will have the height of your PhysicWorld boundaries, you can use this to let your camera follow the player in x, but not in y direction.

如果你(我不知道为什么)不希望使用这些界限,你可以覆盖你雪碧的OnUpdate方法和重新定位你的相机只有在X方向,而不是XY COORDS

if you (i don't know why) don't want to use these boundaries, you can overwrite the onUpdate method of your Sprite and re-locate your camera only in x-direction, instead of xy coords

face.registerUpdateHandler(new IUpdateHandler() {
   @Override
   public void onUpdate(final float pSecondsElapsed) {
      float[] coord = face.getSceneCenterCoordinates();
      this.mBoundChaseCamera.setCenter(sceneCenterCoordinates[0], CAMERA_Y_POSITION);
   }
}

其中 CAMERA_Y_POSITION 与Y位置静态最终字段。

where the CAMERA_Y_POSITION is a static final field with the y-position.

我希望这回答你的问题(S)。 : - )

I hope this answers your question(s). :-)

编辑:哎呀,我忘了提,如何实现相机的约束,我将编辑的世界宽度上面:

edit: oops, i forgot to mention, how to achieve the camera to be bound and i will edit the world width above:

    this.mBoundChaseCamera.setBounds(0, 0,
            WORLD_WIDTH, CAMERA_HEIGHT);

都像你的形象给所有设置(除面部的准确位置,一个必须给予 addFace(PX,PY)

编辑:现场边界之间差异Andengine GLES2 VS GLES2-AnchorCenter

据我了解的问题,我还以为你会用GLES2,我认为(旧的)默认GLES2 AndEngine的分支,并张贴的界限。当你发现自己之前并指出在评论中,你用另一种方法来设置长方形 - 你需要设置矩形的中心像素和PY。这样做的原因是,其实,与AnchorCenter分支,你不会设置一个实体的左上位置了,而使用它的中心位置。

As far as i understood the question, i thought you would use GLES2, i thought of the (older) default GLES2 branch of AndEngine and posted the boundaries. As you found out yourself before and stated in the comments, you use another approach to set the rectangles - where you need to set the rectangles center as pX and pY. The reason for this is in fact, that with the AnchorCenter branch, you won't set the upper left position of an entity anymore and instead use it's center position.

这篇关于大通播放器相机AndEngine,并限制全球范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆