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

6

我正在使用libGDX作为Android项目中的一个片段(fragment),并使用Kotlin编写,一切正常运行。

我现在想做的是从Android部分(MainActivity)调用项目中的libgdx方法。

例如,如果用户按下由Android部分创建的按钮,则游戏中的对象将移动。

首先,这是项目结构:

enter image description here

MainActivity:

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() {

    }
}

在MainActivity中,注意moveRightButton。它从fragment调用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()


    }
}

我的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.



    }


}

我希望在按下向右移动按钮后,能够调用mylibgdxgame中的moveRight函数,并改变精灵的位置。
 kotlin.UninitializedPropertyAccessException: lateinit property sprite has not been initialized

即使已经初始化了,但因某些原因它无法被识别。

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

1个回答

1
在使用MyGdxGame实例之前,请调用create()方法。同时,在片段中仅使用单个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()
    }
}

精灵批处理抛出错误:java.lang.NullPointerException: 尝试在空对象引用上调用接口方法' int com.badlogic.gdx.Graphics.getWidth() ' 在com.badlogic.gdx.graphics.g2d.SpriteBatch.<init>(SpriteBatch.java:108) 在com.badlogic.gdx.graphics.g2d.SpriteBatch.<init>(SpriteBatch.java:80) 在com.krytasoft.mygdxgame.core.MyGdxGame.create(MyGdxGame.kt:17) 在com.krytasoft.gdxandroid.AndroidGameFragment.onCreateView(AndroidGameFragment.kt:21) - lastpeony4
@lastpeony4 这是另一个问题。我认为你需要为此创建一个新的问题。 - Andrew Churilo
我认为不需要新问题。主要问题是Android项目和libgdx之间的通信。 - lastpeony4
Gdx.graphicsnull。这意味着您在库的初始化/配置方面遇到了一些问题。我无法帮助您解决此问题。 - Andrew Churilo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接