2012-04-07 25 views
0

我試圖讓它使用XNA一個SteamRE機器人,現在,我剛剛創建了一個新的XNA項目,並拍了一下SteamRE示例代碼就可以了,如果我運行它,它工作正常,但事情是,它並沒有真正開始了XNA的窗口,所以它可以不看鍵盤處理等雖然(true)循環使我的程序無法啓動

如果我刪除了,而(真)循環它的工作原理,但它不會連接。

這裏是我的代碼,如果你們可以看看這個,也許幫助我,這將是巨大的。

namespace Steam 
{ 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    string userName = "username"; 
    string passWord = "password"; 

    SteamClient steamClient = new SteamClient(); // initialize our client 
    SteamUser steamUser; 
    SteamFriends steamFriends; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
    } 

    protected override void Initialize() 
    { 
     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     steamUser = steamClient.GetHandler<SteamUser>(); // we'll use this later to logon 
     steamFriends = steamClient.GetHandler<SteamFriends>(); 

    } 

    protected override void UnloadContent() 
    { 
    } 

    KeyboardState oldState = Keyboard.GetState(); 

    protected override void Update(GameTime gameTime) 
    { 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     ConnectSteam(); 

     base.Update(gameTime); 
    } 

    public void ConnectSteam() 
    { 
     steamClient.Connect(); // connect to the steam network 

     while (true) 
     { 
      CallbackMsg msg = steamClient.WaitForCallback(true); // block and wait until a callback is posted 

      msg.Handle<SteamClient.ConnectCallback>(callback => 
      { 
       // the Handle function will call this lambda method for this callback 

       if (callback.Result != EResult.OK) 
       { 
        Console.WriteLine("Failed. 1"); 
       } 
       //break; // the connect result wasn't OK, so something failed 

       // we've successfully connected to steam3, so lets logon with our details 
       Console.WriteLine("Connected to Steam3."); 
       steamUser.LogOn(new SteamUser.LogOnDetails 
       { 
        Username = userName, 
        Password = passWord, 
       }); 
       Console.WriteLine("Logged on."); 
      }); 
      msg.Handle<SteamUser.LogOnCallback>(callback => 
      { 
       if (callback.Result != EResult.OK) 
       { 
        Console.WriteLine("Failed. 2"); 
       } 

       // we've now logged onto Steam3 
      }); 
     } 
    } 

    public void ConnectToFPP() 
    { 
     KeyboardState newState = Keyboard.GetState(); // get the newest state 

     // handle the input 
     if (oldState.IsKeyUp(Keys.Space) && newState.IsKeyDown(Keys.Space)) 
     { 
      steamFriends.JoinChat(110338190871147670); 
     } 

     oldState = newState; // set the new state as the old state for next time 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     base.Draw(gameTime); 
    } 
    } 
    } 

謝謝!

+4

您使用'while(true)'一個無限循環,不僅會阻止窗口,而且會阻止程序本身。 – Tigran 2012-04-07 21:41:24

+0

Steam客戶端可能會超時,或者您阻止線程並設置回調的方式可能存在其他一些問題。是否有可能創建一個顯式的超時? – Aaronontheweb 2012-04-07 21:45:28

回答

0

的問題,據我看到的是,除非你需要不斷的連接,你需要擺脫你的ConnectSteam代碼,只要你有實際連接。

Console.WriteLine("Logged on."); 
break; 

或者,你可以有一個isConnected變量(很僞代碼-ISH):

bool isConnected = false 
while(!isConnected) 
{ 
    isConnected = LogonCode; 

} 
0

我不是XNA的專家,但我知道你正在使用while(true)循環內Update()有處理非現場窗口的方法,但是窗口控制更新(實際上即使場景更新什麼都不會改變從這個角度來看)。

您遇到無窮遠環路試圖連接到一些服務。你可以在窗口創建窗口的同一個線程上運行它。 我建議你使用多線程,所以運行你想要在另一個線程中連接的代碼。使用ThreadPool.QueueUserWorkItem或其他一些運行線程的方式,並向用戶提供程序正在連接的信息。

0

正如其他人所指出的,問題是在while(true)片段。即使你連接成功,你也永遠不會脫離循環。即使在連接之後連接break;也會解決問題,但仍然會阻止整個過程的運行,直到連接建立。

最好的解決方法是刪除了,同時,一旦在每一幀(或第二,或兩次,你的異步調用使用的等待時間),如果沒有建立連接,嘗試只有一次再次連接。由於您的連接代碼是異步的,您的遊戲將繼續循環,您可以繼續繪製屏幕並通知用戶事態進展情況。

protected override void Update (GameTime gameTime) 
{ 
    // ... 
    if (!connected && !tryingToConnect) 
    { 
     ConnectSteam(); 

     // Remember to set to false once connection is established 
     showConnectingDialog = true; 
    } else 
    { 

    } 
    // ... 
} 

protected override void Draw (GameTime gameTime) 
{ 
    // ... 
    if (showConnectingDialog) 
    { 
     SpriteBatch.DrawString(SpriteFont, "Connecting to Steam servers...", Vector2.Zero, Color.White); 
    } else 
    { 
     // ... 
    } 
}