0

我正在尝试构建一个非常简单的 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);
    }
}

}

这是我的 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);
    }
}

}

4

1 回答 1

2

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

所以我想你可以这样做:

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

    spawnTime = 0;
}

不过,您的方法看起来不太好。您确定要直接添加球Components吗?

于 2013-03-13T11:02:37.597 回答