2011-05-18 103 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