XNA Can';不创建多个实例?

XNA Can';不创建多个实例?,xna,instance,Xna,Instance,我正在尝试构建一个非常简单的XNA项目。 每5秒会产生一个新的球,它会反弹到墙壁上。 出于某种原因,它不会创建我的球的多个实例。我似乎不知道我做错了什么 希望这里有人能帮我发现我的错误 这是我的代码Game1.cs类 using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using

我正在尝试构建一个非常简单的XNA项目。 每5秒会产生一个新的球,它会反弹到墙壁上。 出于某种原因,它不会创建我的球的多个实例。我似乎不知道我做错了什么

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

这是我的代码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);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
名称空间弹跳
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
公共静态SpriteBatch SpriteBatch;
球;
球2;
球[]球;
int maxBalls=1;
纹理2d球状纹理;
纹理2d球纹理2;
矢量2位置;
向量2位置2;
整数时间=0;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
ballTexture=Content.Load(“位图1”);
ballTexture2=Content.Load(“位图2”);
随机r=新随机();
int randomNumberX=r.Next(0400);
int randomNumberY=r.Next(0400);
位置=新矢量2(随机数x,随机数);
位置2=新矢量2(随机数,随机数x);
球=新球(此、球纹理、位置);
ball2=新球(此,ballTexture2,位置2);
组件。添加(球);
//增加(2);
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
//TODO:在此处添加更新逻辑
spawnTime+=(int)gameTime.ElapsedGameTime.ms;
//Console.WriteLine(生成时间+“”+maxBalls);
如果(生成时间>=500)
{
最大球数+=1;
对于(int i=0;i
}

这是我的球

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);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Graphics;
名称空间弹跳
{
公共类球:Microsoft.Xna.Framework.DrawableGameComponent
{
纹理2D纹理;
矢量2速度=新矢量2(4,6);
矢量2位置;
颜色=颜色。蓝色;
公共球(游戏1,纹理2D,矢量2位置)
:基地(游戏)
{
这个纹理=纹理;
Console.WriteLine(“========================================BAL--------------------”;
这个位置=位置;
随机=新随机();
int red=random.Next(0255);
int绿色=随机。下一步(0255);
蓝色整数=随机。下一个(0255);
颜色=新颜色(红色、绿色、蓝色);
控制台写入线(位置);
}
公共覆盖无效更新(游戏时间游戏时间)
{
this.position=this.position+=速度;
if(this.position.Y>=(GraphicsDevice.Viewport.Height-this.texture.Height))
{
速度Y=速度Y*-1;
}
if(this.position.Y=(GraphicsDevice.Viewport.Width-this.texture.Width))
{
速度.X=速度.X*-1;
}

如果(this.position.X看起来您正在某个数组中重新创建所有球,每500毫秒再添加一个球(实际上是0.5秒)。您应该只将一个新球添加到一些全局球列表中,而不是像现在这样重写所有现有球

所以我想你可以这样做:

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

    spawnTime = 0;
}

不过,你的方法看起来不太好。你确定要直接向
组件添加球吗?

啊,谢谢你,你的代码完成了任务。为什么我不想这样做?这种方法有任何限制吗?嗯,一定有一些限制,比如不超过1000万个对象,我真的不知道。只是没有你可能想把所有的球都装进去