【问题标题】:My game custom input adapter is not running rendering system我的游戏自定义输入适配器没有运行渲染系统
【发布时间】:2020-10-04 17:19:02
【问题描述】:

我正在使用 Ashley 和 Guicer 使用 LibGDX 和 Kotlin 制作 ECS 游戏。一切正常,除了当我单击时,实体生成时没有纹理。当我在主文件中调用 addEntity 时,纹理被加载,但在 MyInputAdapter 纹理中却没有加载。

这是 MyInputAdapter 类:

class MyInputAdapter @Inject constructor(private val camera: OrthographicCamera,
                                     private val engine: Engine,
                                     private val world: World) :
    InputAdapter() {
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
    val worldCords = camera.unproject(Vector3(screenX.toFloat(), screenY.toFloat(), 0f))

    engine.addEntity(Entity().apply {
        add(TextureRegionComponent(TextureRegion(Main.img)))
        add(TransformComponent(Vector2(worldCords.x, worldCords.y), 50f, 0.2f))

        val body = world.createBody(BodyDef().apply {
            type = BodyDef.BodyType.DynamicBody
        })
        body.createFixture(PolygonShape().apply {
            setAsBox(1f, 1f)
        }, 1.0f)
        body.setTransform(transform.position, 0f)
        add(PhysicsComponent(body))
    })

    return true
  }
}

这是适配器在 Main 类中的注册:

 override fun create() {
    batch = SpriteBatch()
    img = Texture("badlogic.jpg")
    injector = Guice.createInjector(GameModule(this))
    injector.getInstance(Systems::class.java).list.map { injector.getInstance(it) }.forEach { system ->
        engine.addSystem(system)
    }

    createEntities()

    Gdx.input.inputProcessor = injector.getInstance(MyInputAdapter::class.java)
}

TextureRegionComponent 在输入适配器中被忽略,但它不在 Main 类中:

private fun createEntities() {
    val world = injector.getInstance(World::class.java)
    engine.addEntity(Entity().apply {
        add(TextureRegionComponent(TextureRegion(img)))
        add(TransformComponent(Vector2(5F, 5F)))

        val body = world.createBody(BodyDef().apply {
            type = BodyDef.BodyType.DynamicBody
        })
        body.createFixture(PolygonShape().apply {
            setAsBox(img.width.pixelToMeter / 2f, img.height.pixelToMeter / 2f)
        }, 1.0f)
        body.setTransform(transform.position, 0f)
        add(PhysicsComponent(body))
    })
    ...
}

这是一些图片: example

【问题讨论】:

  • 在您的两个示例中,您使用了不同的 TransformComponent 构造函数。你能展示你的 TransformComponent 类吗?我想知道您的区域是否被画在屏幕外。
  • //Transform component class TransformComponent(val position: Vector2, var angleRadian: Float, var scale: Float) : Component { constructor(position: Vector2) : this(position, 0f, 1f) companion object : ComponentResolver<TransformComponent>(TransformComponent::class.java) }

标签: kotlin libgdx entity-component-system


【解决方案1】:

解决方案

所以我现在想通了。问题是我没有在我的 GameModule 类中正确设置活页夹

class GameModule(private val main: Main) : Module {
  override fun configure(binder: Binder) {
    binder.requireAtInjectOnConstructors() //<-- this was missing
    binder.requireExactBindingAnnotations() //<-- and this too
    binder.bind(SpriteBatch::class.java).toInstance(main.batch)
  }
  ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    相关资源
    最近更新 更多