1

我正在用 JRuby 编写一个使用 Slick2D 库的游戏。我不知道如何让 Rawr gem 正确地捆绑我的应用程序而不会出错。

  1. 我跑rake rawr:jar。包生成正确,没有错误。
  2. 然后java -jar package/jar/mygame.jar运行应用程序。
  3. 我运行它会rake rawr:bundle:app生成一个 MacOSX 应用程序,没有错误。
  4. 打开package/osx/mygame.app失败并出现错误:

    线程“主”org.jruby.exceptions.RaiseException 中的异常:(NameError)缺少类或大写包名(`org.newdawn.slick.AppGameContainer')

    如果我将package目录移动到其他地方并尝试该java -jar package/jar/mygame.jar命令,也会发生同样的情况。

为什么“org.newdawn.slick.AppGameContainer”会丢失,而所有其他声明似乎都可以正常工作?


这是应用程序源代码:

require 'java'
require 'lib/java/lwjgl.jar'
require 'lib/java/slick.jar'

java_import org.newdawn.slick.BasicGame
java_import org.newdawn.slick.GameContainer
java_import org.newdawn.slick.Graphics
java_import org.newdawn.slick.Image
java_import org.newdawn.slick.Input
java_import org.newdawn.slick.SlickException
java_import org.newdawn.slick.AppGameContainer

class PongGame < BasicGame  
  def render(container, graphics)
    @bg.draw(0, 0)
    @ball.draw(@ball_x, @ball_y)
    @paddle.draw(@paddle_x, 400)
    graphics.draw_string('RubyPong (ESC to exit)', 8, container.height - 30)
  end

  def init(container)
    @bg = Image.new('bg.png')
    @ball = Image.new('ball.png')
    @paddle = Image.new('paddle.png')
    @paddle_x = 200
    @ball_x = 200
    @ball_y = 200
    @ball_angle = 45
  end

  def update(container, delta)
    input = container.get_input
    container.exit if input.is_key_down(Input::KEY_ESCAPE)

    if input.is_key_down(Input::KEY_LEFT) && @paddle_x > 0
      @paddle_x -= 0.3 * delta 
    end

    if input.is_key_down(Input::KEY_RIGHT) && @paddle_x < container.width - @paddle.width
      @paddle_x += 0.3 * delta 
    end 

    @ball_x += 0.3 * delta * Math.cos(@ball_angle * Math::PI / 180)   
    @ball_y -= 0.3 * delta * Math.sin(@ball_angle * Math::PI / 180) 

    if (@ball_x > container.width - @ball.width) || (@ball_y < 0) || (@ball_x < 0)
      @ball_angle = (@ball_angle + 90) % 360
    end

    if @ball_y > container.height
      @paddle_x = 200
      @ball_x = 200
      @ball_y = 200
      @ball_angle = 45
    end

    if @ball_x >= @paddle_x && @ball_x <= (@paddle_x + @paddle.width) && @ball_y.round >= (400 - @ball.height)
      @ball_angle = (@ball_angle + 90) % 360
    end
  end
end

app = AppGameContainer.new(PongGame.new('RubyPong'))
app.set_display_mode(640, 480, false)
app.start

Rawr build_configuration.rb 文件:

configuration do |c|
  # The name for your resulting application file (e.g., if the project_name is 'foo' then you'll get foo.jar, foo.exe, etc.)
  # default value: "mygame"
  #
  #c.project_name = "mygame"

  # Undocumented option 'output_dir'
  # default value: "package"
  #
  #c.output_dir = "package"

  # The type of executable to create (console or gui)
  # default value: "gui"
  #
  #c.executable_type = "gui"

  # The main ruby file to invoke, minus the .rb extension
  # default value: "main"
  #
  c.main_ruby_file = "pong"

  # The fully-qualified name of the main Java file used to initiate the application.
  # default value: "org.monkeybars.rawr.Main"
  #
  #c.main_java_file = "org.monkeybars.rawr.Main"

  # A list of directories where source files reside
  # default value: ["src"]
  #
  #c.source_dirs = ["src"]

  # A list of regexps of files to exclude
  # default value: []
  #
  #c.source_exclude_filter = []

  # The base directory that holds Mirah files, or subdirectories with Mirah files.
  # default value: "src"
  #
  #c.mirah_source_root = "src"

  # Whether Ruby source files should be compiled into .class files. Setting this to true currently breaks packaging
  # default value: false
  #
  #c.compile_ruby_files = false

  # A list of individual Java library files to include.
  # default value: []
  #
  #c.java_lib_files = []

  # A list of directories for rawr to include . All files in the given directories get bundled up.
  # default value: ["lib/java"]
  #
  #c.java_lib_dirs = ["lib/java"]

  # A list of files that will be copied into the `<output_dir>/jar` folder.  Note that the files maintain their directory path when copied. 
  # default value: []
  #
  #c.files_to_copy = []

  # Undocumented option 'source_jvm_version'
  # default value: 1.7
  #
  #c.source_jvm_version = 1.7

  # Undocumented option 'target_jvm_version'
  # default value: 1.7
  #
  #c.target_jvm_version = 1.7

  # Undocumented option 'jvm_arguments'
  # default value: ""
  #
  #c.jvm_arguments = ""

  # Undocumented option 'java_library_path'
  # default value: ""
  #
  #c.java_library_path = ""

  # Undocumented option 'extra_user_jars'
  # default value: {}
  #
  #c.extra_user_jars[:data] = { :directory => 'data/images/png',
  #                             :location_in_jar => 'images',
  #                             :exclude => /*.bak$/ }

  # Undocumented option 'verbose'
  # default value: false
  #
  #c.verbose = false

  # Undocumented option 'mac_do_not_generate_plist'
  # default value: false
  #
  #c.mac_do_not_generate_plist = false

  # working directory specified in plist file
  # default value: "$APP_PACKAGE"
  #
  #c.mac_plist_working_directory = "$APP_PACKAGE"

  # Undocumented option 'mac_icon_path'
  # default value: nil
  #
  #c.mac_icon_path = nil

  # Undocumented option 'windows_icon_path'
  # default value: nil
  #
  #c.windows_icon_path = nil

end

项目树状:

- mygame/
  |
  |- lib/
  |  |- java/
  |     |- lwjgl.jar
  |     |- slick.jar
  |
  |- src/
  |  |- pong.rb
  |  |- org/
  |     |- monkeybars/
  |        |- rawr/
  |           |- Main.java
  |
  |- ball.png
  |- bg.png
  |- build_configuration.rb
  |- libjinput-osx.jnilib
  |- liblwjgl.jnilib
  |- openal.dylib
  |- paddle.png
  |- Rakefile

PS:我从此处提供的存档中获取了代码,并将其更改为符合 Rawr 默认值。

4

1 回答 1

1

基本上问题是 LWJGL 原生需要与mygame.jar.

这样,当您运行rake rawr:bundle:app它们时,它们会被捆绑在应用程序中,并且将包目录移动到其他地方也可以。

错误的非冗长性有点误导。

将此添加到build_configuration.rb遗嘱中:

# A list of files that will be copied into the `<output_dir>/jar` folder.
c.files_to_copy = [
  "libjinput-osx.jnilib",
  "liblwjgl.jnilib",
  "openal.dylib"
]
于 2013-04-11T16:18:44.580 回答