Question

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?

Was it helpful?

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).

OTHER TIPS

This almost certainly doesn't work because you're doing this:

platform.assets().setPathPrefix("resources")

That means you're saying your source folder looks like this:

src/main/resources/resources/images/bg.png
src/main/resources/resources/images/pea.png
src/main/resources/resources/images

I imagine it actually looks like one of these:

src/main/resources/assets/images/bg.png  <-- 'assets' the default prefix
src/main/resources/assets/images/pea.png
src/main/resources/assets/images

or:

src/main/resources/images/bg.png  <-- You have failed to put a subfolder prefix in
src/main/resources/images/pea.png
src/main/resources/images

You can either do this, if you have no prefix:

plat.assets().setPathPrefix("")

Or just put your files in the assets sub-folder inside the resources folder.

It's worth noting that the current implementation calls:

getClass().getClassLoader().getResource(...)

Not:

getClass().getResource(...)

The difference is covered elsewhere, but the tldr is that plat.assets.getImage("images/pea.png") will work, but plat.assets.getImage("/images/pea.png") will not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top