Hello everyone! Anyone have experience doing kotli...
# gamedev
m
Hello everyone! Anyone have experience doing kotlin js games using Phaser? Currently struggling setting up a working project. There seems to exist two kotlin wrappers for Phaser. First: https://github.com/hiperbou/kotlin-phaser seems to be good but i cannot get it to work with a multiplatform project(doing web game with jvm backend). @hiperbou Then there is: https://gitlab.com/nanodeath/phaserkt but that seems to be not support the phaser api enough to do a basic game. @nanodeath A third option would be to use Dukat and generate kotlin wrapper from the phaser typscript bindings. But there seems to be a lot of issues with Dukat and this path might be alot of work. I just want to get something up and running. Have anyone looked at this and could give me some pointer on how to do this? A link to some project i can look at would be great! 🙂
h
Hello Max, it's been a long time since I worked in kotlin-phaser, at the time I started on it, multiplatform projects probably weren't mature enough (or didn't exist at all?). I will try to use gradle to set up the library and the template projects and deprecate the plain IntellJ projects... but don't expect this to happen anytime soon :S Meanwhile, if you look closer to the kotlin-phaser/phaserKt/src folder, you could see that all those files are just the Phaser 2.6.2 definitions. Maybe that's the only thing that you need. I made the definitions using the old ts2kt tool, with the help of an automated script to fix things and some manual work too. I think that a good starting point would be just make a multiplatform project, and then copy and paste the definition files or the compiled version that you can find at kotlin-phaser/tree/master/template/lib/phaserKt.jar Working with definitions as huge as those ones slowed down the compilation times, that's why I compiled them on a library. But the easier way to start. certainly would be just grabbing those and including them on your own project.
m
Thanks for the reply Daniel! The project setup im using is very similar to https://github.com/maxonline/KotlinJsJvmGame. What i have tried doing is including the compiled jar as a file dependency with gradle(I had to manually remove the kotlin.js file to not conflict with the one the project already provides). It seems to work fine as i get autocomplete in intellij etc. The current problem is that i get
Copy code
Uncaught ReferenceError: PIXI is not defined

  at /Users/max/dev/home/hexone/build/js/node_modules/Phaser/build/phaser.js:23282:1 <- /Users/max/dev/home/hexone/build/js/packages/hexone-test/adapter-browser.js:23373:11
I think this has to do with how the js lib is packaged. In a kotlin js project the kotlinOptions.moduleKind should be set to something else to support the current default multiplatform js project (Im not a JS dev so im not 100% certain what the problem is). Maybe the idea of including the files in the project is simpler. Then it should probably work with my current multiplatform setup After reading your reply Dukat seems to be simpler than i thought. Maybe this would be a possible option as well. The advantage of this is that i would get a more up to date Phaser version.
h
I was using the minified version https://github.com/hiperbou/kotlin-phaser/blob/master/template/assets/phaser.min.js wich probably includes everything. If you are using npm to get phaser (I guess that is the case) you need to set the moduleKind to 'commonjs' Also, If you want to use the compiled definitions with the npm library, that will not work, because those were expecting moduleKind as 'plain'. So you may have to edit the files manually, changing this lines https://github.com/hiperbou/kotlin-phaser/blob/master/phaserKt/src/Phaser/phaser.kt#L1 to something like this
Copy code
@file:Suppress("INTERFACE_WITH_SUPERCLASS", "OVERRIDING_FINAL_MEMBER", "RETURN_TYPE_MISMATCH_ON_OVERRIDE", "CONFLICTING_OVERLOADS", "EXTERNAL_DELEGATION")
@file:JsModule("phaser")
About using Dukat, you might have to give some love to the results, like changing variable types to make Phaser more Kotlin friendly. When using ts2kt, most (if not all) numeric types were translated to Number, and I had to manually change those to Double or Int, to be able to do some operations on them, but leaving the function parameter types as Number to be able to use every numeric type without needing to do conversions. Also function and constructor overloads used to give me some trouble too. So, expect to invest some time tweaking the definitions from Dukat.
on an extra note, last "official" Phaser 2 version didn't get to npm, so if you are importing it from npm, you're getting Phaser 3 that may be quite different.
But you could use
phaser-ce@2.7.0
that is almost the same as I was using. (you will have to change "phaser" to "phaser-ce" on the lines I mentioned before on the definition file
m
Thanks for the very detailed reply! Ok using Dukat to generate a new wrapper seems to be a lot of work. As i just want to get something working and dont really know why i would need a newer version of Phaser, i will probably copy your wrapper code into my project or do the gradle version of your project(and set it to a moduleKind that works 😏) you mentioned before.
👌 1