I'm not sure if this is the best place to ask this question or not but I feel like I'm struggling to grasp something relatively simple and can't find any decent advice on it in relation to KorGE so if anyone can offer any assistance it would be much appreciated.
This is a a question that can relate to multiple things so I'll use a simple example to demonstrate.
Say I set up a solidRect and I want it to travel along the x coordinate by one on each update I might do something like this:
solidRect(16.0, 16.0, Colors.GREEN) {
x = 0.0;
y = 0.0;
addUpdater {
x += 1.0;
}
}
This will create a solid rectangle in the container of 16px by 16px at the position 0,0 and each update will then add 1 to the x position moving it across the screen. Now what I'd like to do is encapsulate this logic into a class in a separate file so I could do something along the lines of the following:
movingShape() {
x = 0.0,
y = 0.0,
}
My expectation being that this would create the same solid rect visible in the scene, position it a 0, 0 and then the updater would be contained within the class itself, so I can use this logic multiple times. However, whenever I do this either init the solidRect in the class or passing it through in the constructor the updater function fails to fire.
As a longer fully functioning example I'd want something along the lines of this:
suspend fun main() = Korge(width = 512.0, height=512.0, bgcolor = Colors["#2b2b2b"]) {
val sceneContainer = sceneContainer();
sceneContainer.changeTo({MyScene()});
}
class MyScene : Scene() {
override suspend fun SContainer.sceneMain() {
movingShape() {
x = 0.0;
y = 0.0;
}
}
}
How would I implement the movingShape class in this example so it works kind of how I mentioned above?
Once again, sorry, if this seems like a trivial question or could be easily answered through some Kotlin. I'm relatively new to the language mainly coming from a Java / JS / PHP background. Any help would be appreciated here? I've tried to look for a good example of this but so far have turned up empty.