android - 在 Android 项目中使用 LibGDX 作为 fragment : How do i call libgdx method from android part?

标签 android kotlin libgdx fragment

我在 android 项目中使用 libGDX 作为 kotlin 的 fragment ,它工作正常。

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

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

首先这是项目结构:

enter image description here

主要 Activity :

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


    }
}

MyGDX 游戏:

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 中调用 fun moveRight 并改变 Sprite 的位置。

 kotlin.UninitializedPropertyAccessException: lateinit property sprite has not been initialized

尽管它已初始化。但由于某种原因它没有被看到。

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

最佳答案

在使用MyGdxGame 实例之前调用create() 方法。还跨 fragment 使用 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()
    }
}

关于android - 在 Android 项目中使用 LibGDX 作为 fragment : How do i call libgdx method from android part?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55544767/

相关文章:

android - 在 Kinvey (Android) 中创建集合

android - 无法将 Kotlin Android 扩展添加到我的项目中

android - 使用改造和 kotlin channel 实现长轮询

java - Libgdx 测试直线是否与矩形相交

java - 我不明白一些 libgdx 编译错误

java - 等待线程不恢复

java - 如何从android中的静态方法调用非静态方法

java - 如何使我的 ListView 行高适合里面的内容?

exception - Kotlin 构造函数属性和调用不同的父类(super class)构造函数

java - libGDX:如何检测滚动 Pane 是否在最大 Y 处滚动?