2013-05-16 25 views
0

我是SFML的新手,我現在只是在演示一個示例。我嘗試繪製一個精靈,最後在窗口中點擊鼠標。但某些原因事件發生,並沒有繪製精靈。我無法弄清楚爲什麼。當釋放鼠標按鈕時,窗口不會繪製我的Sprite

我一直在瀏覽文檔,但我似乎沒有做錯任何事情。請幫助我漂亮嗎? :3

該事件被稱爲OnMouseLeftClick,我試圖繪製mouseLeftClickSprite對象。我刪除了一些無關緊要的代碼,如OnMouseMoved事件和OnClose事件。他們與此無關。

using System; 
using SFML.Audio; 
using SFML.Graphics; 
using SFML.Window; 

namespace SFMLExample 
{ 
    class Program 
    { 
     static Text xMouseCoord; 
     static Text yMouseCoord; 
     static Text statusMsg; 
     static float mouseX, mouseY; 
     static Texture mouseLeftClickTexture; 
     static Sprite mouseLeftClickSprite; 

     static void OnMouseLeftClick(object sender, EventArgs e) 
     { 
      if (Mouse.IsButtonPressed(Mouse.Button.Left)) 
      { 
       statusMsg.DisplayedString = "Console: OnMouseLeftClick() Fired"; 
       RenderWindow window = (RenderWindow)sender; 
       mouseLeftClickSprite.Position = new Vector2f(mouseX, mouseY); 
       window.Draw(mouseLeftClickSprite); 
      } 
     } 

     static void Main(string[] args) 
     { 
      mouseX = 0.0f; 
      mouseY = 0.0f; 
      // Create the main window 
      RenderWindow window = new RenderWindow(new VideoMode(800, 600), "SFML Window"); 
      window.Closed += new EventHandler(OnClose); 
      window.MouseMoved += new EventHandler<MouseMoveEventArgs>(OnMouseMoved); 
      window.MouseButtonPressed += new EventHandler<MouseButtonEventArgs>(OnMouseLeftClick); 

      // Load a sprite to display 
      Texture texture = new Texture("cute_image.jpg"); 
      Sprite sprite = new Sprite(texture); 

      mouseLeftClickTexture = new Texture("GM106.png"); 
      mouseLeftClickSprite = new Sprite(mouseLeftClickTexture); 

      // Create a graphical string to display 
      Font font = new Font("arial.ttf"); 
      Text text = new Text("Hello SFML.Net", font); 

      xMouseCoord = new Text("Mouse X: ", font); 
      yMouseCoord = new Text("Mouse Y: ", font); 
      statusMsg = new Text("Console: ", font); 

      // Play some Music 
      //Music music = new Music("nice_music.mp3"); 
      //music.Play(); 

      // Start the game loop 
      while (window.IsOpen()) 
      { 
       // Process events 
       window.DispatchEvents(); 

       // Clear screen 
       window.Clear(); 

       sprite.Scale = new Vector2f(1.0f, 1.0f); 
       sprite.Position = new Vector2f(window.Size.X/2, window.Size.Y/2); 
       // Draw the sprite 
       window.Draw(sprite); 

       // Draw the strings 
       xMouseCoord.Position = new Vector2f(text.Position.X, text.Position.Y + 30); 
       yMouseCoord.Position = new Vector2f(xMouseCoord.Position.X, xMouseCoord.Position.Y + 30); 
       statusMsg.Position = new Vector2f(yMouseCoord.Position.X, yMouseCoord.Position.Y + 30); 
       window.Draw(text); 
       window.Draw(xMouseCoord); 
       window.Draw(yMouseCoord); 
       window.Draw(statusMsg); 

       // Update the window 
       window.Display(); 
      } 
     } 
    } 
} 

添加了此位下方的一些澄清

static void OnMouseMoved(object sender, EventArgs e) 
    { 
     RenderWindow window = (RenderWindow)sender; 
     Vector2i vector = window.InternalGetMousePosition(); 
     mouseX = vector.X; 
     mouseY = vector.Y; 
     xMouseCoord.DisplayedString = "Mouse X: " + mouseX; 
     yMouseCoord.DisplayedString = "Mouse Y: " + mouseY; 
     statusMsg.DisplayedString = "Console: "; 
    } 

回答

1

你只畫你的精靈一次。大約每秒60幀,您可能會看到它閃爍或不閃爍。如果你需要它永久保存,你需要記住主程序中的點,然後在每次繪製其他東西時在那個點繪製一個精靈。

你可能想看看遊戲編程模式。通常情況下,您有三個大塊不斷重複:您獲得用戶輸入,根據用戶輸入進行操作,然後繪製結果。

在僞代碼:

while(isRunning) 
{ 
    HandleUserInput(); 

    ChangeWorld(): 

    Render(); 
} 

這樣的話,你可以檢查自己,如果你做的是正確的。例如,在你的場景中,你會在處理用戶輸入時繪製一些東西。不好。

+0

雖然我不確定這是多麼真實。關於僅針對一幀而不是停留的渲染。因爲如果你看看我的鼠標移動代碼,你可以看到我沒有畫出每一幀的鼠標座標文本,但他們仍然存在。即使我不移動鼠標,它仍然存在。 – OmniOwl

+0

由於某種原因,似乎現在工作。猜猜它已經修復了。 – OmniOwl

+0

從你的代碼中,我猜你會這樣做。最後看看你的主循環。 .Draw()調用每幀都執行一次。任何不在那裏的電話將只執行一次。 – nvoigt

0

在所謂的window.DispatchEvents()內調度並評估事件。當你點擊你的窗口時,事件將被觸發並且你的精靈將被繪製。

事情是,你在它後面打電話window.Clear(),所以你看不到結果。正如nvoigt所說,你必須在每一幀都呈現一切,也就是說在你的循環的每一個回合。這就是爲什麼使用Window中的Clear()方法來清除前一幀的結果。

你可以做的是記住事件被觸發時鼠標的位置或直接根據它設置精靈的位置。然後畫出你的精靈和你的文字等。如果你不希望它在任何點擊鼠標之前顯示,只需將它的座標放在窗口外的一個點上,這樣它就不會被繪製。

相關問題