天天看點

在libgdx中隻用Kotlin(1)-配置環境在libgdx中隻用Kotlin(1)-配置環境

在libgdx中隻用Kotlin(1)-配置環境

本篇文章主要介紹Kotlin在libgdx中的使用,用它寫代替java的原因在于:它是JAVA中的Swift,将會帶來全新的體驗哦:

  • 建立一種相容Java的語言
  • 編譯速度至少同Java一樣快
  • 比Java更安全
  • 比Java更簡潔
  • 比最成熟的競争者Scala還簡單

下面開始講解怎麼在libgdx中配置Kotlin

  1. 安裝idea插件
    在libgdx中隻用Kotlin(1)-配置環境在libgdx中隻用Kotlin(1)-配置環境
  2. 導入libgdx項目

    用最新的setup.jar建立libgdx項目, 然後導入到idea(as)中去,如下圖

    在libgdx中隻用Kotlin(1)-配置環境在libgdx中隻用Kotlin(1)-配置環境
  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檔案
    在libgdx中隻用Kotlin(1)-配置環境在libgdx中隻用Kotlin(1)-配置環境
    這裡我建立的是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(f, f, f, f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch?.begin();
        img?.draw(batch, f);
        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