【发布时间】:2013-07-11 07:34:53
【问题描述】:
我目前有一个游戏,其中有一张 480x3200 的地图,一个人从顶部坠落。摄像机跟随人,人落下时有平台。平台需要是可触摸的,这样我才能在游戏过程中旋转和移动它们,所以我把它做成了一个图像,而它最初只是一个精灵。
当我将它从精灵更改为图像时,一切正常,除了当人摔倒时(相机开始向下移动并跟随人),平台也随着相机移动,所以看起来它也在下降.平台应该只停留在一个位置,而不是随着相机移动,这样随着相机不断向下移动,平台最终会从视野中消失。
设置平台并将其添加到舞台
@Override
public void show() {
...
platform = new Image(new Texture(Gdx.files.internal("img_platform.png")));
platform.setX(2);
platform.setY(110);
platform.setOrigin(platform.getWidth() / 2, platform.getHeight() / 2);
@Override
public void render(float delta) {
....
stage.addActor(platform);
}
我查看了 API,无法确定是否需要更改相机、舞台或图像,或者其他我没有想到的东西。有什么想法吗?
编辑:
public WorldRenderer(FallDown game, SpriteBatch batch, World w) {
this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
this.cam.position.set(CAMERA_WIDTH / 2, CAMERA_HEIGHT / 2, 0);
this.cam.setToOrtho(false, CAMERA_WIDTH, CAMERA_HEIGHT);
cam.position.set(CAMERA_WIDTH / 2, 105, 0);
stage = new Stage(480, 800, true);
platformTexture = new Image(new Texture(Gdx.files.internal("img_platform.png")));
platformTexture.setX(2);
platformTexture.setY(100);
platformTexture.setOrigin(platformTexture.getWidth() / 2, platformTexture.getHeight() / 2);
...
}
public void render(float delta) {
stage.addActor(platformTexture);
moveCamera();
...
}
private void moveCamera() {
if (Person.getPosition().y < cam.position.y)
cam.position.y = Person.getPosition().y;
cam.update();
}
...
【问题讨论】:
-
移动 stage.addActor(platform) 到 show() 方法。您当前正在每个渲染循环中将演员重新添加到舞台。
-
@Aert 我在 show() 中添加了 stage.addActor(platform),它给了我一个 NullPointerException。但我认为添加到 show() 对我的情况不起作用,因为我还需要在平台上进行碰撞检测