我已经创建了一个libgdx游戏和一个android工作室项目。我希望能够使用游戏按钮从应用程序中打开我的游戏。如何将游戏添加到android项目中?
发布于 2020-08-12 00:14:23
创建您的libgdx project.
现在,你需要在你的模块上使用这些类(有些会自动出现):
a) android
b)核心
在您的AndroidManifest.xml上,您必须更改启动程序:
(在应用程序标记代码之间)
<activity
android:name="com.ongngo.game.AndroidLauncher"
android:label="@string/app_name"
android:screenOrientation="yourchoice..."
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
</activity>
<activity
android:name="com.ongngo.game.MainActivity" //Your launch activity, not necessarily MainActivity
android:label="@string/app_name"
android:screenOrientation="yourchoice..."
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>AndroidLauncher.kt或java:
class AndroidLauncher : AndroidApplication() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val config = AndroidApplicationConfiguration()
config.useAccelerometer = false
config.useCompass = false
initialize(MyGame(MainActivity.VARIABLE_YOU_WANT_TO_UPDATE, MainActivity.coreAndroidInterface), config) //You pass your data to the game
}
}MainActivity.kt:
class MainActivity : AppCompatActivity() {...
companion object {//I need to see this from AndroidLauncher
var coreAndroidInterface: CoreAndroidInterface? = null
var VARIABLE_YOU_WANT_TO_UPDATE: Int? = null
}
override fun onCreate(savedInstanceState: Bundle?) {
...
coreAndroidInterface = object : CoreAndroidInterface {
override fun updateData(data: Int) { //you retrieve data from the game
MainActivity.VARIABLE_YOU_WANT_TO_UPDATE = data
}
}
...
override fun onClick(v: View?) {
when (v?.id) {
R.id.play_MyGame ->{ //your Button id to start the game
val playGame = Intent(this.context, AndroidLauncher::class.java)
startActivity(playGame)
}
...CoreAndroidInterface (仅当您需要从游戏中检索数据时才需要)
interface CoreAndroidInterface {
fun updateData(data : Int)
}MyGame.java:
public class MyGame extends Game implements ApplicationListener {
...
public static int DATA = 0;
//constructor
public MyGame(int data, CoreAndroidInterface coreAndroidInterface) {
this.DATA = data;//data comes from the android side
this.coreAndroidInterface = coreAndroidInterface;
}
//I modify DATA
//you can use the interface method at any point of your game when necessary, to get your data back to android.
@Override
public void pause() {
super.pause();
SavedDataManager.getInstance().save();
coreAndroidInterface.updateData(DATA);
}这是我的第一个游戏,所以请记住,我只是个乞讨者。
发布于 2016-03-07 12:00:04
两周前,我试着做这件事,但发现这是不可能的。好吧,通过深入研究项目设置文件,这可能是可能的,但我觉得这不值得我花时间。
我求助于做一个新的LibGDX project creation,然后是importing the build.gradle file在Android Studio中作为新项目导入。从那里开始,将代码从现有的Android Studio项目拖放到新项目中。
发布于 2021-02-01 21:24:15
我的回答是有点晚了,但你可以查看解释过程的视频。我是按照描述中指定的教程来做的:
https://stackoverflow.com/questions/35830792
复制相似问题