2013-12-23 128 views
1

假設我正在製作遊戲(或類似的東西),解決方案中的第一個項目是1級,第二個項目是2級,等等......想象一下,遊戲開始於等級1,並且玩家可以在等級2和等級3之間作出選擇以便繼續。並根據他的選擇,運行項目2或3。我該怎麼做(如果可以的話)?我使用的Visual Studio 2012專業版,如果它很重要...如何從同一解決方案訪問另一個項目? (Visual Studio 2012)

+1

我會說,你有一個設計問題,遊戲的水平應該是裝載和同解/可執行文件內顯示的資源(圖片,聲音,動畫)的集合。此外,「訪問另一個項目」的含義並不那麼明確 –

+0

項目只是開發人員組織源代碼的一種方式,不要這樣看。不要將玩家的視角與玩家分開。但是,如果你真的想做得那麼好,只需引用項目並按照你在同一個項目中使用類的方式來使用該類。 –

+0

遊戲是一種解決方案,但不同的級別是一種解決方案中的不同項目。解決方案是一系列項目。 –

回答

1

如果你需要的是從 「MainProject.exe」 呼叫 「PROJECT1.EXE」 這可以使用System.Diagnostics程序幫助您

;

class Program 
{ 
    static void Main() 
    { 
     int level ; 

     //ask or determine which level wants to be played 
     LaunchCommandLineApp(level); 
    } 

    /// <summary> 
    /// Launch the legacy application with some options set. 
    /// </summary> 
    static void LaunchCommandLineApp(int level) 
    { 
    // For the example 
    const string ex1 = "C:\\"; 
    const string ex2 = "C:\\Dir"; 

    // Use ProcessStartInfo class 
    ProcessStartInfo startInfo = new ProcessStartInfo(); 
    startInfo.CreateNoWindow = false; 
    startInfo.UseShellExecute = false; 
    startInfo.FileName = "Project" + level + ".exe"; 

    //I guess you dont need this 
    //startInfo.WindowStyle = ProcessWindowStyle.Hidden; 

    //If your project needs som parameters use this, if not removeit 
    startInfo.Arguments = "-f j -o \"" + ex1 + "\" -z 1.0 -s y " + ex2; 

    try 
    { 
     // Start the process with the info we specified. 
     // Call WaitForExit and then the using statement will close. 
     using (Process exeProcess = Process.Start(startInfo)) 
     { 
     exeProcess.WaitForExit(); 
     } 
    } 
    catch 
    { 
     // Log error. 
    } 
    } 
} 
0

您可以添加參考使用VS SolutionExplorer到項目2項目1 - >項目1 - >右鍵點擊References並選擇Add Reference,然後指定的DLL。

+0

您也可以直接引用該項目。 – Bit

+0

好的,我添加了參考,但現在呢?我在「參考」文件夾中看到它,但我該如何啓動項目本身? –

+1

你可以調用你的公共對象,例如'Project2.myPublicClass ....' – Milen

0

我不能完全肯定我理解您的方案,但是......

有兩種選擇:

  1. 「主」項目引用所有的「水平」項目。「主」項目是一個'外殼',它包含基本的遊戲規則邏輯並引用關卡項目。在這裏,「你想玩哪個級別?」會發生。
  2. 依賴注入: 1級項目查看配置文件並動態加載下一級。
相關問題