Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using LibGDX inside Android Project as Fragment : How do i call libgdx method from android part?

I am using libGDX inside android project as fragment all with kotlin and its working fine.

What i am trying to do is call a method of libgdx part of project from android part(MainActivity) of the project.

So for example if user presses on button which is made by android part,object in game will move.

First of all this is the project structure:

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

    }
}

Here in MainActivity, notice moveRightButton.Its calling moveRight() function from fragment.

AndroidFragment class:

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 Game:

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.



    }


}

So what i expect is after pressing move right button,eventually call fun moveRight in mylibgdxgame and change position of sprite.

 kotlin.UninitializedPropertyAccessException: lateinit property sprite has not been initialized

eventhough its initialized.But for some reason its not seen.

I uploaded my project to github: https://github.com/lastpeony/libgdx-in-android-kotlin

like image 261
lastpeony4 Avatar asked Nov 06 '22 18:11

lastpeony4


1 Answers

Call create() method before using MyGdxGame instance. Also use a single instance of MyGdxGame across fragment.

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()
    }
}
like image 142
Andrew Churilo Avatar answered Nov 15 '22 06:11

Andrew Churilo