我正在使用libGDX作为Android项目中的一个片段(fragment),并使用Kotlin编写,一切正常运行。
我现在想做的是从Android部分(MainActivity)调用项目中的libgdx方法。
例如,如果用户按下由Android部分创建的按钮,则游戏中的对象将移动。
首先,这是项目结构:
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
Gdx.graphics
是null
。这意味着您在库的初始化/配置方面遇到了一些问题。我无法帮助您解决此问题。 - Andrew Churilo