我正在尝试构建一个非常简单的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);
}
}
}
答案 0 :(得分:2)
看起来你正在重新创建某个阵列中的所有球,每500毫秒增加一个(实际上是0.5秒)。你应该只是在一些全球球的列表中添加一个新球,而不是像现在一样重写所有现有的球。
所以我想你可以这样做:
if(spawnTime >= 500)
{
Components.Add(new Ball(this, ballTexture));
spawnTime = 0;
}
但是你的方法看起来并不好。您确定要将球直接添加到Components
吗?