javascalaplayn

Error reading image from scala playn


I researched and looked into the PlayN game framework and I liked it a lot. I program in Scala and actually don't know Java but it's not usually a problem since they work together great.

I've set up a basic project in eclipse and imported all the libraries and dependencies. I even translated over the base maven project code. Here's the two files:

Zeitgeist.scala

package iris.zeit.core

import playn.core.PlayN._

import playn.core.Game
import playn.core.Image
import playn.core.ImageLayer

class Zeitgeist extends Game {

  override def init (){
    var bgImage: Image = assets().getImage("images/bg.png")
    var bgLayer: ImageLayer = graphics().createImageLayer(bgImage)
    graphics().rootLayer().add(bgLayer)
  }

  override def paint (alpha: Float){
    //painting stuffs
  }

  override def update(delta: Float){

  }

  override def updateRate(): Int = {
    25
  }

}

Main.scala

package iris.zeit.desktop

import playn.core.PlayN
import playn.java.JavaPlatform
import iris.zeit.core.Zeitgeist

object Main {
    def main(args: Array[String]){
        var platform: JavaPlatform = JavaPlatform.register()
        platform.assets().setPathPrefix("resources")
        PlayN.run(new Zeitgeist())
    }
}

The cool thing is it works! A window comes up perfectly. The only problem is I can't seem to load images. With the above line, "assets().getImage("images/bg.png")" it pops out

Could not load image: resources/images/bg.png [error=java.io.FileNotFoundException: resources/images/bg.png]

I've played around with the location of my resources file to no avail. I was even able to find bg.png myself with java.io.File. Am I doing something wrong? Is there something I'm forgetting?


Solution

  • Looking at the code of JavaAssetsManager, it looks like it is trying to load a resource and not a file. So you should check that your images are actually in the classpath and at the path you give ("resources/images/bp.png")

    Alternatively, you can use getRemoteImage and pass a File URL. As you succeeded in using a java.io.File, you can just get the URL with method toUri of File (toUrl is deprecated).