2013-10-17 54 views
0

我正在做一個簡單的點擊遊戲,只是爲了讓我的手在鼠標輸入類。因此,我有一個按鈕類(這只是一個貓的^^隨機圖片)和一個計數器類(它統計了我點擊了多少次),遊戲本身將是一個「Cookie點擊者」類型的遊戲; d鼠標點擊值不會更新

但是,當我用它從另一個類,即使它是公共的變量將不會更新,這裏是我的課:

class catButton 
{ 
    Texture2D buttonTexture; 
    Vector2 buttonPosition; 
    public Rectangle buttonRectangle; 

    public MouseState currentMouseState; 
    public MouseState previousMouseState; 

    public void Initialize() 
    { 
     buttonPosition = new Vector2(150, 300); 
    } 

    public void LoadContent(ContentManager Content) 
    { 
     buttonTexture = Content.Load<Texture2D>("cat"); 
     buttonRectangle = new Rectangle((int)buttonPosition.X, (int)buttonPosition.Y, buttonTexture.Width, buttonTexture.Height);  
    } 

    public void Update(GameTime gameTime) 
    { 
     previousMouseState = currentMouseState; 
     currentMouseState = Mouse.GetState(); 
    } 

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime) 
    { 
      spriteBatch.Draw(buttonTexture, buttonRectangle, Color.White); 
    }  
} 

和我的櫃檯:

class catCounter 
{ 
    SpriteFont catfont; 
    Vector2 counterPosition; 

    catButton button = new catButton(); 


    public MouseState currentMouseState; 
    public MouseState previousMouseState; 

    KeyboardState keyState = Keyboard.GetState(); 

    public float amountOfCats = 0; 

    public catCounter() 
    { 

    } 

    public void LoadContent(ContentManager Content) 
    { 
     counterPosition = new Vector2(205, 155); 
     catfont = Content.Load<SpriteFont>("counter"); 
    } 

    public void Update(GameTime gameTime) 
    { 
     MouseState currentMouseState = Mouse.GetState(); 

     if (currentMouseState.LeftButton == ButtonState.Pressed && previousMouseState.LeftButton == ButtonState.Released && button.buttonRectangle.Contains(new Point(currentMouseState.X, currentMouseState.Y))) 
     { 
      amountOfCats++; 
     } 

     previousMouseState = currentMouseState; 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.DrawString(catfont, "Cats: " + amountOfCats, counterPosition, Color.White); 
    } 
} 

我應該怎麼做鼠標點擊Game1.cs而不是?

我很短時間寫這個抱歉,如果你必須知道什麼告訴我!

在此先感謝!

回答

1

爲什麼變量在其他類中使用時不會更新? 您必須接近如何正確收集此變量。

它是哪個變量?什麼樣的電話應該更新它,但是沒有按照預期執行?

我假設你更新,並通過第一初始化catCounter類到您的Game1.cs文件中的對象,並且裏面Game1.cs相關的方法在該對象上調用Update()Draw()爲好,而只是提繪製組件如果這是缺少的。

值得注意的是,Update方法catButton只是一種浪費。只要刪除它內的兩條線。它沒有任何作用,因爲你不會在任何地方重新使用這些鼠標狀態。同樣在您的catCounter更新中,您將currentMouseState重新定義爲該範圍內的局部變量,從而使public MouseState currentMouseState變得毫無價值且未被使用。

無論哪種方式,都需要注意一些關於XNA組件的事情。由於您的類目前按照給定的XNA結構,你應該考慮繼承DrawableGameComponent爲你的類:

class catCounter: DrawableGameComponent 

讓你做:

Components.Add(new catCounter()); 

這將使這樣你不必打電話給我們手動使用Update()Draw()方法,但您需要覆蓋特定方法(例如,Initialize,LoadContent,Update,Draw)。

對不起,當你不告訴我們問題存在的地方時,我無法理解你的問題在哪裏。

+0

+1 for DrawableGameComponent – pinckerman

0

好的,我認爲更好的方法是創建一個名爲clicked的變量並將其設置爲False

然後:

public void Update(GameTime gameTime) 
    { 
     MouseState currentMouseState = Mouse.GetState(); 

     if (currentMouseState.LeftButton == ButtonState.Pressed) 
     { 
      if (clicked == False && button.buttonRectangle.Contains(new Point(currentMouseState.X, currentMouseState.Y))) 
      { 
       amountOfCats++; 
       clicked = True; 
      } 
     } 
     else 
     { 
      clicked = False 
     } 

     // Not needed - previousMouseState = currentMouseState; 
    } 

NB:不要忘記將點擊一個bool變量。

這樣,您可以防止用戶將鼠標按下,並且不依賴於第二個鼠標狀態變量。

至於你的代碼不工作的原因,是因爲Mouse.GetState()自動更新。因此,previousMouseState將始終是當前的鼠標狀態。

我希望這有助於。

Mona