所以我在C#.NET中使用XNA进行了项目。我的代码:
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 Fireflies
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D backgroundImage;
Texture2D swietlik;
float vSwietlikX, vSwietlikY;
Rectangle recSwietlik;
int n = 20; // objects population
int d = 50; // dimension
int io = 10; // max light intensity
double alfa = 0.5; // alfa value
double betao = 0.2; // beta value
double gamma = 1.0; // gamma value
Random rnd = new Random();
List<Swietlik> populacja = new List<Swietlik>();
List<Swietlik> sasiad = new List<Swietlik>();
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()
{
// Generate new population
for (int i = 0; i < n; i++)
{
populacja.Add(new Swietlik(rnd.Next(0, 800), rnd.Next(0, 600), rnd.Next(0, 10)));
}
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);
backgroundImage = Content.Load<Texture2D>("BG");
swietlik = Content.Load<Texture2D>("swietlik");
recSwietlik = new Rectangle(rnd.Next(10), rnd.Next(10), (swietlik.Width / 10) * 4, (swietlik.Height / 10) * 4);
vSwietlikX = vSwietlikY = 1;
}
/// <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();
recSwietlik.X += (int)(gameTime.ElapsedGameTime.TotalMilliseconds * vSwietlikX);
recSwietlik.Y += (int)(gameTime.ElapsedGameTime.TotalMilliseconds * vSwietlikY);
if (recSwietlik.X < 0 || recSwietlik.X + recSwietlik.Width > GraphicsDevice.Viewport.Width)
{
vSwietlikX = -(vSwietlikX);
}
if (recSwietlik.Y < 0 || recSwietlik.Y + recSwietlik.Height > GraphicsDevice.Viewport.Height)
{
vSwietlikY = -(vSwietlikY);
}
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.AliceBlue);
// Drawing objects
spriteBatch.Begin();
spriteBatch.Draw(backgroundImage, GraphicsDevice.Viewport.TitleSafeArea, Color.White);
foreach(swietlik in populacja)
{
spriteBatch.Draw(swietlik, recSwietlik, Color.White);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
说明:在Initialize()部分,我想从一个类生成对象(我随机生成位置x的值,位置y和每个对象的光强度)并将它们放入List<Swietlik> populacja
。然后在我想绘制的Draw()方法中。我使用了foreach
循环,但我不知道应该将哪些内容放入spriteBatch.Draw
行,为什么我有“是一个字段但是像类型一样使用”错误。
答案 0 :(得分:0)
我认为你的问题就在这一行:
foreach(swietlik in populacja)
尝试将其更改为:
foreach(Swietlik s in populacja)
{
spriteBatch.Draw(swietlik, recSwietlik, Color.White);
}
swietlik
是Texture2D
,而不是Swietlik
,这是填充populacja
列表的内容。您无法使用Texture2D
实例遍历列表。 (在foreach循环中需要type
和identifier
。如果您的Swietlik
类具有矩形属性,并且您希望在其中绘制每个Swietlik
矩形,然后你可以这样做:
foreach(Swietlik s in populacja)
{
spriteBatch.Draw(swietlik, s.rectangle, Color.White);
}
答案 1 :(得分:0)
感谢您的帮助,但实际上我自己找到了解决方案。我的循环看起来像:
for (int b = 0; b < swietliki.Count; b++)
{
swietliki[b].Draw(spriteBatch);
}
spriteBatch.End();
无论如何,谢谢你;)