在libgdx中只用Kotlin(1)-配置环境

在libgdx中只用Kotlin(1)-配置环境

本篇文章主要介绍Kotlin在libgdx中的使用,用它写代替java的原因在于:它是JAVA中的Swift,将会带来全新的体验哦:

  • 创建一种兼容Java的语言
  • 编译速度至少同Java一样快
  • 比Java更安全
  • 比Java更简洁
  • 比最成熟的竞争者Scala还简单

下面开始讲解怎么在libgdx中配置Kotlin

  1. 安装idea插件

  2. 导入libgdx项目
    用最新的setup.jar创建libgdx项目, 然后导入到idea(as)中去,如下图
    这里写图片描述

  3. 配置build.gradle
    在根目录的build.gradle里面修改配置
buildscript {
    repositories {
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'org.robovm:robovm-gradle-plugin:1.9.0'
        #下面这个是新添加的
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:+'
    }
}
project(":android") {
    apply plugin: "android"
    #下面这个是新添加的
    apply plugin: 'kotlin-android'

    configurations { natives }

    dependencies {
        compile project(":core")
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        #下面这个是新添加的
        compile 'org.jetbrains.kotlin:kotlin-stdlib:+'
        compile fileTree(dir: 'libs', include: '*.jar')
    }
}
project(":ios") {
    apply plugin: "java"
    apply plugin: "robovm"
    #下面这个是新添加的
    apply plugin: "kotlin"


    dependencies {
        compile project(":core")
        compile "org.robovm:robovm-rt:$roboVMVersion"
        compile "org.robovm:robovm-cocoatouch:$roboVMVersion"
        compile "com.badlogicgames.gdx:gdx-backend-robovm:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-ios"
        compile "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-ios"
        compile "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-ios"
    }
}
project(":core") {
    apply plugin: "java"
    #下面这个是新添加的
    apply plugin: "kotlin"

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        compile "net.dermetfan.libgdx-utils:libgdx-utils:0.13.1"
        compile "net.dermetfan.libgdx-utils:libgdx-utils-box2d:0.13.1"
        #下面这个是新添加的
        compile 'org.jetbrains.kotlin:kotlin-stdlib:+'
        compile fileTree(dir: '../android/libs', include: '*.jar')
    }
}

仔细比较下不同之处.

书写Kotlin文件:

  1. 在core里面新建kt文件
    这里写图片描述
    这里我新建的是class文件, 然后写进代码就可以了.
/**
 1. Created by tian on 15/11/25.
 */
class TestKotlin : ApplicationAdapter() {
    var batch : SpriteBatch? = null;
    var img : Image? = null;

    override fun create() {
        batch = SpriteBatch();
        img = Image(Texture("textures/gamebg.png"))
        img?.setSize(Data.GameWidth, Data.GameHeight);
    }

    override fun render() {
        //super.render()
        Gdx.gl.glClearColor(1f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch?.begin();
        img?.draw(batch, 1f);
        batch?.end();
    }
}
  1. 替换运行
public class DesktopLauncher {
    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.width = (int) Data.GameWidth;
        config.height= (int) Data.GameHeight;
        //new LwjglApplication(new SaveTree(), config);
        new LwjglApplication(new TestKotlin(), config);
    }
}
public class IOSLauncher extends IOSApplication.Delegate {
    @Override
    protected IOSApplication createApplication() {
        IOSApplicationConfiguration config = new IOSApplicationConfiguration();
        return new IOSApplication(new TestKotlin(), config);
    }

    public static void main(String[] argv) {
        NSAutoreleasePool pool = new NSAutoreleasePool();
        UIApplication.main(argv, null, IOSLauncher.class);
        pool.close();
    }
}

Android里面也是一样的, 完美运行.

关于Kotlin语法

关于语法,请参考:链接1, 链接2

猜你喜欢

转载自blog.csdn.net/qq634416025/article/details/50024035