libgdx地面不工作

我沿着这个教程https://github.com/libgdx/libgdx/wiki/Box2d ,我目前卡在静态对象。 这是我的游戏课程:

package com.mygdx.physics import com.badlogic.gdx.ApplicationAdapter import com.badlogic.gdx.Gdx import com.badlogic.gdx.graphics.GL20 import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.math.Vector2 import com.badlogic.gdx.physics.box2d.World import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer import com.badlogic.gdx.graphics.OrthographicCamera import com.badlogic.gdx.physics.box2d.BodyDef.BodyType import com.badlogic.gdx.physics.box2d.Body import com.badlogic.gdx.physics.box2d.CircleShape import com.badlogic.gdx.physics.box2d.FixtureDef import com.badlogic.gdx.physics.box2d.Fixture import com.badlogic.gdx.physics.box2d.BodyDef import com.badlogic.gdx.physics.box2d.PolygonShape class Physics : ApplicationAdapter() { internal var batch: SpriteBatch? = null internal var world: World = World(Vector2(0.toFloat(), -10.toFloat()), true) internal var camera: OrthographicCamera = OrthographicCamera() internal var debugRenderer: Box2DDebugRenderer? = null internal var bodyDef: BodyDef = BodyDef() internal var body: Body? = null private var groundBodyDef: BodyDef = BodyDef() private var groundBox: PolygonShape = PolygonShape() private var groundBody: Body? = null override fun create() { createBody() createBall() createGround() camera.setToOrtho(false, 800f, 480f) debugRenderer = Box2DDebugRenderer() } override fun render() { Gdx.gl.glClearColor(1f, 0f, 0f, 1f) Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT) camera.update() debugRenderer?.render(world, camera.combined); world.step(1/45f, 6, 2); } override fun dispose() { } private fun createBody() { bodyDef.type = BodyType.DynamicBody bodyDef.position.set(100.toFloat(), 300.toFloat()) body = world.createBody(bodyDef) } private fun createBall() { BallHandler(body).createBall() } private fun createGround() { groundBodyDef = BodyDef() groundBodyDef.position.set(Vector2(0f, 10f)) groundBody = world.createBody(groundBodyDef) groundBox = PolygonShape() groundBox.setAsBox(camera.viewportWidth, 10.0f) groundBody?.createFixture(groundBox, 0.0f) groundBox.dispose() } } 

正如你所看到的,我以几乎相同的方式实现了它。 他们把部分作品当成了魅力,剩下的就是编辑,但是我看不到创作的地面,也看不到它的作用。 为什么那个或者我该如何有效地调试呢?

这是你的create方法:

 override fun create() { createBody() createBall() createGround() camera.setToOrtho(false, 800f, 480f) debugRenderer = Box2DDebugRenderer() } 

在调用camera.viewportWidth之后,您正在设置摄像机尺寸,但camera.viewportWidth已经使用了camera.viewportWidth 。 因此,地面的宽度为零。

将该调用移至setToOrtho以开始该方法。

另外,下面是一个Kotlin提示:不是使用多个可为空的变量,而是使用lateinit属性来避免所有不必要的空检查。

你也可以使用一些局部变量,比如groundBodyDef