【问题标题】:Libgdx : Exported desktop jar file link to external path instead jar rootLibgdx:导出的桌面 jar 文件链接到外部路径而不是 jar 根
【发布时间】:2017-11-05 07:33:22
【问题描述】:

我正在尝试将我的 libgdx 应用程序部署到 jar 桌面文件中。我使用 Intellij,所以我构建了桌面模块,然后执行 ./gradlew desktop:dist 来生成 jar,一切顺利!我使用资产管理器来加载我的资产。现在当我执行 jar 时,资产管理器会尝试在 jar 目录中获取资产,而不是直接在 jar 文件中。

我的资产经理:

package com.com8.game.Utils;

import com.badlogic.gdx.assets.AssetDescriptor;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.SkinLoader;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;


public class Assets {

public AssetManager manager = new AssetManager();

public static final AssetDescriptor<Texture> logo =
        new AssetDescriptor<Texture>("logo.png", Texture.class);

public static final AssetDescriptor<Texture> tableBack =
        new AssetDescriptor<Texture>("back.png", Texture.class);

public static final AssetDescriptor<Texture> frontBackground =
        new AssetDescriptor<Texture>("frontBackground.png", Texture.class);

public static final AssetDescriptor<Texture> backBackground =
        new AssetDescriptor<Texture>("backBackground.png", Texture.class);

public static final AssetDescriptor<TextureAtlas> uiAtlas =
        new AssetDescriptor<TextureAtlas>("UI/uiskin.atlas", TextureAtlas.class);

public static final AssetDescriptor<Skin> uiSkin =
        new AssetDescriptor<Skin>("UI/uiskin.json", Skin.class,
                new SkinLoader.SkinParameter("UI/uiskin.atlas"));

public static final AssetDescriptor<TextureAtlas> gameAtlas =
        new AssetDescriptor<TextureAtlas>("Atlas/gameImages.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> flamesAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/flamesAnimation.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> hitAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/hitAnimation.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> shipExplosionAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/shipExplosionAnimation.atlas", TextureAtlas.class);

public static final AssetDescriptor<TextureAtlas> dustAnimation =
        new AssetDescriptor<TextureAtlas>("Atlas/dustAnimation.atlas", TextureAtlas.class);


public void load()
{
    manager.load(logo);
    manager.load(tableBack);
    manager.load(frontBackground);
    manager.load(backBackground);
    manager.load(uiAtlas);
    manager.load(uiSkin);
    manager.load(gameAtlas);
    manager.load(flamesAnimation);
    manager.load(hitAnimation);
    manager.load(shipExplosionAnimation);
    manager.load(dustAnimation);
}

public void dispose() {
    manager.dispose();
}
}

我的 desktopLauncher 的 build.gradle : 应用插件:“java”

sourceCompatibility = 1.6
sourceSets.main.java.srcDirs = [ "src/" ]

project.ext.mainClassName = "com.com8.game.desktop.DesktopLauncher"
project.ext.assetsDir = new File("../core/assets");

task run(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
}

task debug(dependsOn: classes, type: JavaExec) {
    main = project.mainClassName
    classpath = sourceSets.main.runtimeClasspath
    standardInput = System.in
    workingDir = project.assetsDir
    ignoreExitValue = true
    debug = true
}

task dist(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from {configurations.compile.collect {zipTree(it)}}
    from files(project.assetsDir);

    manifest {
        attributes 'Main-Class': project.mainClassName
    }
}

dist.dependsOn classes

eclipse {
    project {
        name = appName + "-desktop"
        linkedResource name: 'assets', type: '2', location: 'PARENT-1-PROJECT_LOC/core/assets'
    }
}

task afterEclipseImport(description: "Post processing after project 
generation", group: "IDE") {
  doLast {
    def classpath = new XmlParser().parse(file(".classpath"))
    new Node(classpath, "classpathentry", [ kind: 'src', path: 'assets' ]);
    def writer = new FileWriter(file(".classpath"))
    def printer = new XmlNodePrinter(new PrintWriter(writer))
    printer.setPreserveWhitespace(true)
    printer.print(classpath)
  }
}

有人可以帮助我吗? :) 谢谢

编辑: 我只是在每个路径名前面添加了一个“/”,它似乎更好! now i have this,文件logo.png在jar文件的根目录下。

这是我如何使用 Assets 类:

// Assets
private AssetManager manager;
private Stage stage;
private Skin skin;

// UI
private TextField userIDRegistered;
private TextField userPwd;
private TextField userIDGuest;
private Label authentificationResponse;
private Label socketConnectedMessage;

// Socket
private Socket socket;

public ConnexionScreen(Com8 game) {
    this.game = game;
    this.socket = game.getSocket();
    this.manager = game.assets.manager;

    skin = game.skin;

    stage = game.stage;
    stage.clear();

    configSocketEvents();
}

@Override
public void show() {
    //Create Table
    Table mainTable = new Table();

    //Set table position properties
    mainTable.align(Align.center|Align.center);
    mainTable.setFillParent(true);

    //Create image
    Texture logoTexture = manager.get("/logo.png", Texture.class);
    Image logoImage = new Image(logoTexture);

【问题讨论】:

    标签: java intellij-idea jar libgdx assets


    【解决方案1】:

    改变

    project.ext.assetsDir = new File("../core/assets");
    

    project.ext.assetsDir = new File("../android/assets");
    

    build.gradle桌面模块文件中

    然后运行./gradlew desktop:dist

    希望您的所有资源都在 android module 的 assets 文件夹中。

    编辑

    我测试过,它对我来说工作正常,找出你做错了什么。

    create() 方法内的代码

    AssetDescriptor<Texture> assetDescriptor=new AssetDescriptor<Texture>("brick.png",Texture.class);
    
    AssetManager assetManager=new AssetManager();
    assetManager.load(assetDescriptor);   // load by assetDescriptor
    assetManager.load("badlogic.jpg", Texture.class);   // manual loading
    
    assetManager.finishLoading();
    
    texture=assetManager.get("badlogic.jpg",Texture.class);  // fetch data manually 
    texture1=assetManager.get(assetDescriptor);              // fetch by descriptor
    

    我在render()方法中绘制这两个纹理

    资源位于assets 文件夹中的核心模块内。

    然后我在dist任务中使用的桌面模块的build.gradle文件中分配资产目录

    project.ext.assetsDir = new File("../core/assets");

    在终端上运行./gradlew dist 命令。

    我在桌面模块的 build 文件夹中找到了我的 desktop-1.0.jar。复制并保存在其他文件夹中并通过双击运行,它工作正常。

    这两个资源(badlogic.jpg & brick.png)都在我的.jar 的根目录下。

    【讨论】:

    • 您好,感谢您的帮助,但我没有 android 模块(我只在桌面上部署)并且我的资产位于“../core/assets”中。当我制作 jar 时,资产被正确复制到 jar 文件的根目录中,因此他找到了它们。问题是当我尝试在我的资产管理器中访问它们时,根路径在 jar 文件之外,所以如果我将所有资产放在 jar 的同一个文件中,它可以完美运行,但不是很方便
    • 好的,你能告诉我代码吗,你是如何使用Assets类的
    • 好的,谢谢!问题是我的 AssetDescriptors !他们处于静止状态!我不知道为什么,但现在它工作正常:)
    【解决方案2】:

    感谢@Abhishek Aryan,我找到了问题! 我的资产描述符是静态的,在处理资产管理器时,似乎没有什么是静态的。我像这样更改我的资产类:

    public class Assets {
    
    public AssetManager manager;
    
    public AssetDescriptor<Texture> logo;
    
    public AssetDescriptor<Texture> tableBack;
    public AssetDescriptor<Texture> frontBackground;
    
    public AssetDescriptor<Texture> backBackground;
    
    public AssetDescriptor<TextureAtlas> uiAtlas;
    
    public AssetDescriptor<Skin> uiSkin;
    
    public AssetDescriptor<TextureAtlas> gameAtlas;
    
    public AssetDescriptor<TextureAtlas> flamesAnimation;
    
    public AssetDescriptor<TextureAtlas> hitAnimation;
    
    public AssetDescriptor<TextureAtlas> shipExplosionAnimation;
    
    public AssetDescriptor<TextureAtlas> dustAnimation;
    
    public Assets(){
        manager = new AssetManager();
        logo = new AssetDescriptor<Texture>("logo.png", Texture.class);
        tableBack = new AssetDescriptor<Texture>("back.png", Texture.class);
        frontBackground = new AssetDescriptor<Texture>("frontBackground.png", Texture.class);
        backBackground = new AssetDescriptor<Texture>("backBackground.png", Texture.class);
        uiAtlas = new AssetDescriptor<TextureAtlas>("UI/uiskin.atlas", TextureAtlas.class);
        uiSkin = new AssetDescriptor<Skin>("UI/uiskin.json", Skin.class,
                new SkinLoader.SkinParameter("UI/uiskin.atlas"));
        gameAtlas = new AssetDescriptor<TextureAtlas>("Atlas/gameImages.atlas", TextureAtlas.class);
        flamesAnimation = new AssetDescriptor<TextureAtlas>("Atlas/flamesAnimation.atlas", TextureAtlas.class);
        hitAnimation = new AssetDescriptor<TextureAtlas>("Atlas/hitAnimation.atlas", TextureAtlas.class);
        shipExplosionAnimation = new AssetDescriptor<TextureAtlas>("Atlas/shipExplosionAnimation.atlas", TextureAtlas.class);
        dustAnimation = new AssetDescriptor<TextureAtlas>("Atlas/dustAnimation.atlas", TextureAtlas.class);
    }
    
    public void load()
    {
        manager.load(logo);
        manager.load(tableBack);
        manager.load(frontBackground);
        manager.load(backBackground);
        manager.load(uiAtlas);
        manager.load(uiSkin);
        manager.load(gameAtlas);
        manager.load(flamesAnimation);
        manager.load(hitAnimation);
        manager.load(shipExplosionAnimation);
        manager.load(dustAnimation);
    }
    
    public void dispose() {
        manager.dispose();
    }
    }
    

    现在一切正常:)

    【讨论】:

      猜你喜欢
      • 2013-11-15
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2011-08-22
      相关资源
      最近更新 更多