2013-03-13 90 views
0

我正在尝试构建一个非常简单的XNA项目。 每隔5秒钟就会产生一个新球,这将会弹出墙壁。 由于某些原因,它不会创建我的球的多个实例。我似乎无法弄清楚我做错了什么。XNA无法创建多个实例?

希望这里有人能帮我发现我的错误。

这里是我的代码Game1.cs类

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 

namespace BallBounce 
{ 
/// <summary> 
/// This is the main type for your game 
/// </summary> 
public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    public static SpriteBatch spriteBatch; 

    Ball ball; 
    Ball ball2; 

    Ball[] balls; 
    int maxBalls = 1; 

    Texture2D ballTexture; 
    Texture2D ballTexture2; 

    Vector2 position; 
    Vector2 position2; 

    int spawnTime = 0; 

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

    /// <summary> 
    /// Allows the game to perform any initialization it needs to before starting to run. 
    /// This is where it can query for any required services and load any non-graphic 
    /// related content. Calling base.Initialize will enumerate through any components 
    /// and initialize them as well. 
    /// </summary> 
    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 
     ballTexture = Content.Load<Texture2D>("Bitmap1"); 
     ballTexture2 = Content.Load<Texture2D>("Bitmap2"); 

     Random r = new Random(); 
     int randomNumberX = r.Next(0, 400); 
     int randomNumberY = r.Next(0, 400); 

     position = new Vector2(randomNumberX, randomNumberY); 
     position2 = new Vector2(randomNumberY, randomNumberX); 

     ball = new Ball(this, ballTexture, position); 
     ball2 = new Ball(this, ballTexture2, position2); 

     Components.Add(ball); 
     // Components.Add(ball2); 

     base.Initialize(); 
    } 

    /// <summary> 
    /// LoadContent will be called once per game and is the place to load 
    /// all of your content. 
    /// </summary> 
    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     // TODO: use this.Content to load your game content here 
    } 

    /// <summary> 
    /// UnloadContent will be called once per game and is the place to unload 
    /// all content. 
    /// </summary> 
    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     // TODO: Add your update logic here 
     spawnTime += (int)gameTime.ElapsedGameTime.Milliseconds; 
     // Console.WriteLine(spawnTime + " " + maxBalls); 
     if(spawnTime >= 500) 
     { 
      maxBalls += 1; 

      for(int i = 0; i < maxBalls; i++) 
      { 
       //balls[i] = new Ball(this,ballTexture); 
       //Components.Add(balls[i]); 
      } 

      spawnTime = 0; 
     } 

     base.Update(gameTime); 
    } 

    /// <summary> 
    /// This is called when the game should draw itself. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     // TODO: Add your drawing code here 

     base.Draw(gameTime); 
    } 
} 

}

这里是我的Ball.cs:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace BallBounce 
{ 
public class Ball : Microsoft.Xna.Framework.DrawableGameComponent 
{ 
    Texture2D texture; 
    Vector2 speed = new Vector2(4,6); 
    Vector2 position; 
    Color color = Color.Blue; 

    public Ball(Game1 game, Texture2D texture, Vector2 position) 
     : base(game) 
    { 
     this.texture = texture; 
     Console.WriteLine("=========================BAL---------------"); 

     this.position = position; 

     Random random = new Random(); 

     int red = random.Next(0, 255); 
     int green = random.Next(0,255); 
     int blue = random.Next(0,255); 

     color = new Color(red,green,blue); 

     Console.WriteLine(position); 

    } 

    public override void Update(GameTime gameTime) 
    { 
     this.position = this.position += speed; 

     if (this.position.Y >= (GraphicsDevice.Viewport.Height - this.texture.Height)) 
     { 
      speed.Y = speed.Y * -1; 
     } 
     if (this.position.Y <= 0) 
     { 
      speed.Y = speed.Y * -1; 
     } 
     if (this.position.X >= (GraphicsDevice.Viewport.Width - this.texture.Width)) 
     { 
      speed.X = speed.X * -1; 
     } 
     if (this.position.X <= 0) 
     { 
      speed.X = speed.X * -1; 
     } 

    } 

    public override void Draw(GameTime gameTime) 
    { 
     Game1.spriteBatch.Begin(); 
     Game1.spriteBatch.Draw(texture, position, color); 
     Game1.spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

}

回答

2

它看起来像你重新创建一些数组中的所有球,每500毫秒增加一个(真的是0.5秒)。您应该只添加一个新球到一些全局球的列表中,而不是像现在这样重写所有现有的球。

所以我猜你可以这样做:

if(spawnTime >= 500) 
{ 
    Components.Add(new Ball(this, ballTexture));    

    spawnTime = 0; 
} 

你的做法并不好看,但。您确定要直接添加球到Components吗?

+0

嗯谢谢你,你的一段代码完成了这项工作。 为什么我不想那么做?这种方法有什么限制吗? – Weszzz7 2013-03-13 11:41:36

+0

好吧,一定有一些限制,比如不超过1000万个对象,我真的不知道。只是你可能想把所有球实例放在一个你可以直接调用和修改的列表中,但是如果你打算做除了创建球之外的任何事情。如果没有,那么我猜你没事。 – user1306322 2013-03-13 13:03:27

+0

好的,谢谢你的信息:) – Weszzz7 2013-03-13 13:17:15