在 Android 项目中使用 LibGDX 作为片段:如何从 android 部分调用 libgdx 方法?

las*_*ny4 5 android fragment libgdx kotlin

我在 android 项目中使用 libGDX 作为片段所有与 kotlin 和它的工作正常。

我想要做的是从项目的android部分(MainActivity)调用项目的libgdx部分的方法。

例如,如果用户按下由 android 部分制作的按钮,游戏中的对象将移动。

首先这是项目结构:

在此处输入图片说明

主要活动:

package com.krytasoft.androidwithlibgdx


import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
//import android.support.v4.app.Fragment  throws unresolved error.without this compiles fine and works but shows type mismatch error.

import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import com.krytasoft.gdxandroid.AndroidGameFragment

class MainActivity : AppCompatActivity(), AndroidFragmentApplication.Callbacks {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val libgdxGameFragment:AndroidGameFragment = AndroidGameFragment()
        val button = findViewById<Button>(R.id.openFlexBoxTestButton)
        val moveRightButton = findViewById<Button>(R.id.moveRightButton)



        //never mind if this supportFragmentManager... shows type mismatch error.Its working. this line puts libgdx into fragment.fragment is similar to component in react.
        supportFragmentManager.beginTransaction().replace(R.id.fragment_container, libgdxGameFragment, AndroidGameFragment::class.java.simpleName).commit()

        button.setOnClickListener{
            val intent = Intent(this, FlexBoxTestActivity::class.java)
               startActivity(intent)

        }
        moveRightButton.setOnClickListener {
            libgdxGameFragment.moveRight()

        }
    }




    override fun exit() {

    }
}
Run Code Online (Sandbox Code Playgroud)

在 MainActivity 中,注意 moveRightButton 的调用moveRight()函数。

AndroidFragment 类:

package com.krytasoft.gdxandroid

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration
import com.badlogic.gdx.backends.android.AndroidFragmentApplication
import com.krytasoft.mygdxgame.core.MyGdxGame



class AndroidGameFragment : AndroidFragmentApplication() {
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val config = AndroidApplicationConfiguration()
        return initializeForView(MyGdxGame(), config)
    }

    override fun startActivity(intent: Intent?) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    fun moveRight(){
        MyGdxGame().moveRight()


    }
}
Run Code Online (Sandbox Code Playgroud)

我的GDX游戏:

package com.krytasoft.mygdxgame.core

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.Sprite
import com.badlogic.gdx.graphics.g2d.SpriteBatch

class MyGdxGame : ApplicationAdapter() {
    lateinit var batch: SpriteBatch
    lateinit var img: Texture
    lateinit var sprite:Sprite


    override fun create() {
        batch = SpriteBatch()
        img = Texture("badlogic.jpg")
        sprite = Sprite(img)
        println("create done from libgdx")
    }

    override fun render() {
        Gdx.gl.glClearColor(1f, 0f, 0f, 1f)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
        batch.begin()
        batch.draw(sprite, 50f, 50f)
        batch.end()
    }

    override fun dispose() {

        batch.dispose()
        img.dispose()

    }

   fun moveRight(){
      sprite.x +=50f;    // throws lateinit var sprite is uninitialzied error.



    }


}
Run Code Online (Sandbox Code Playgroud)

所以我期望的是按下向右移动按钮后,最终在 mylibgdxgame 中调用 fun moveRight 并更改精灵的位置。

 kotlin.UninitializedPropertyAccessException: lateinit property sprite has not been initialized
Run Code Online (Sandbox Code Playgroud)

即使它已初始化。但由于某种原因它没有看到。

我将我的项目上传到 github:https : //github.com/lastpeony/libgdx-in-android-kotlin

And*_*ilo 2

使用实例create()之前调用 方法。MyGdxGame还使用跨片段的单个实例MyGdxGame

class AndroidGameFragment : AndroidFragmentApplication() {
     val myGdxGame = MyGdxGame()

     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val config = AndroidApplicationConfiguration()
        myGdxGame.create()
        return initializeForView(myGdxGame, config)
    }

    fun moveRight(){
        myGdxGame .moveRight()
    }
}
Run Code Online (Sandbox Code Playgroud)