accmltr / scala-games

A game engine for writing games in Scala.
MIT License
4 stars 0 forks source link

Fix Color Addition #64

Open accmltr opened 9 months ago

accmltr commented 9 months ago

Code:

//... some other code ...
var sprite: Sprite = Sprite(
    "res/sample_image.png"
  )
  sprite.color = Color.GREEN
  sprite.position = Vector2(50, 50)
  sprite.width = 100
  sprite.height = 100
//... some other code ...
onUpdate += { (delta: Float) =>

    //... some other code ...

    if (input.justPressed(KeyCode.s)) {
      sprite.width += 10
      sprite.height += 10
      sprite.rotation += 0.1f
      sprite.color += Color(0.1f, 0.1f, 0.1f)
    }
    if (input.justPressed(KeyCode.d)) {
      sprite.width -= 10
      sprite.height -= 10
      sprite.rotation -= 0.1f
      sprite.color -= Color(0.1f, 0.1f, 0.1f)
    }

    if (input.justPressed(KeyCode.v)) {
      window.vsync = !window.vsync
    }

    if (input.justReleased(KeyCode.escape)) {
      quit()
    }
  }

Error when pressing 'S':

Node(name: MyFirstNode, position: (0.000, 0.000), children: 4, components: 0)
Running Scala Games on LWJGL 3.3.3+5!
Exception in thread "main" java.lang.ExceptionInInitializerError
        at MyGame.main(Main.scala)
Caused by: java.lang.IllegalArgumentException: 'g' out of bounds: [0,1]
        at engine.render.Color.<init>(Color.scala:16)
        at engine.render.Color$.apply(Color.scala:6)
        at engine.render.Color.$plus(Color.scala:52)
        at MyGame$.$init$$$anonfun$2(Main.scala:100)
        at scala.runtime.java8.JFunction1$mcVF$sp.apply(JFunction1$mcVF$sp.scala:18)
        at engine.scene.Signal.emit$$anonfun$1(Signal.scala:13)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:15)
        at scala.runtime.function.JProcedure1.apply(JProcedure1.java:10)
        at scala.collection.immutable.List.foreach(List.scala:333)
        at engine.scene.Signal.emit(Signal.scala:13)
        at engine.Game.updateCallback(Game.scala:79)
        at engine.Game.$init$$$anonfun$2(Game.scala:41)
        at engine.render.window.Window._runLoop(Window.scala:236)
        at engine.render.window.Window.run(Window.scala:85)
        at engine.Game.run(Game.scala:53)
        at MyGame$.<clinit>(Main.scala:118)
        ... 1 more

No error when pressing 'D', but sprite completely disappears.