2017-06-04 5 views
1

libgdxアプリケーションをjarデスクトップファイルにデプロイしようとしています。私はIntellijを使ってデスクトップモジュールをビルドし、次に./gradlewデスクトップを実行しました:distを生成して、すべてうまくいった!私はアセットマネージャーを使って資産を読み込みます。今すぐjarファイルを実行すると、アセットマネージャはjarファイルのディレクトリではなくjarディレクトリのアセットを取得しようとします。Libgdx:エクスポートされたデスクトップjarファイルが、外部パスへのリンクではなく、jarルート

マイアセット・マネージャー:desktopLauncherの

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(); 
} 
} 

マイ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 
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); 

答えて

0

変更

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

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

にデスクトップモジュール

build.gradleファイルで、次にを実行します

あなたのすべてのリソースはandroid moduleの資産フォルダにあることを願っています。

EDIT

私がテストし、それは私のために正常に動作しています、あなたは間違ってやっているものを見つけます。

コード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 

内部の私は

リソースassetsフォルダ内のコアモジュール内にあるrender()方法でこれらの2つのテクスチャを描画します。

enter image description here

その後、私はまた、端末上のdistタスク

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

実行./gradlew distコマンドで使用しているデスクトップモジュールのbuild.gradleファイルにアセットディレクトリを割り当てます。

desktop-1.0.jarはデスクトップモジュールのビルドフォルダ内にあります。コピーして他のフォルダに入れてダブルクリックして実行しても問題ありません。

両方のリソース(badlogic.jpg & brick.png)は、トップルートに私の.jarにあります。

+0

こんにちは、助けに感謝しますが、私はアンドロイドモジュール(私はデスクトップ上に展開)と私の資産を持っていません"../core/assets"にあります。 jarファイルを作成すると、jarファイルのルートにアセットが正しくコピーされるため、アセットを見つけることができます。問題は、私がアセットマネージャーにアクセスしようとすると、ルートパスがjarファイルの外側にあるため、jarの同じファイルにすべてのアセットを置くと、そのファイルは完全に動作しますが、非常に便利ではないということです。 –

+0

大丈夫、あなたは 'Assets'クラスをどのように使用しているのかコードを表示できますか – Aryan

+0

私は投稿を更新しました:) –

0

@Abhishek Aryanのおかげで問題が見つかりました。 My Assets Descriptorsは静的で、資産マネージャを扱うときは何も静的ではないようです。私はこのように私の資産クラスを変更:

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(); 
} 
} 

すべては今うまく機能している:)