2011-05-18 105 views
0

我剛剛開始在C#/ XNA中編碼 我在XNA中製作了一個非常簡單的小程序,它是一個繪製的矩形,內有3個隨機生成的球, 球在自己的類中定義和我已經使用以下用鼠標點擊添加2D精靈

INT ballCount = 3本教程生成 http://www.bluerosegames.com/brg/xna101.aspx

球;

,什麼我想要做的就是讓這麼一個鼠標點擊將增加1整型,加入另一球到屏幕

我的代碼看起來是這樣,但我不知道這是否是正確的/可能

  mouseStateCurrent = Mouse.GetState(); 

     if (mouseStateCurrent.LeftButton == ButtonState.Pressed && 
      mouseStatePrevious.LeftButton == ButtonState.Released) 
     { 

      ballCount = ballCount+1; 

     } 

     mouseStatePrevious = mouseStateCurrent; 

任何幫助的建議將是有益的:)

我使用一個代碼來繪製球已經看起來像這樣

 spriteBatch.Begin(); 
     spriteBatch.Draw(debugColor, TextBox, Color.White); 
     spriteBatch.Draw(background, backgroundRectangle, Color.White); 
     foreach (BouncingBall ball in balls) 
     { 
      ball.Draw(spriteBatch); 
     } 
     spriteBatch.End(); 
     base.Draw(gameTime); 

是否可以編輯這個來獲得「點擊添加球」效果?

回答

1

在你繪製方法,你可以這樣做

protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 
     spriteBatch.Begin(); 
     for(var i=0;i<ballcount;i++) 
     { 
      spriteBatch.Draw() 
     } 
     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
3

如果balls在您MouseClick事件中,你可以使用balls.Add(new BouncingBall());定義爲List<BouncingBall>是到Game類訪問。因爲您正在使用循環,所以它會增加每個循環的球數,並且您的代碼已經滿足添加的所有新的balls