所以我正在制作太空入侵者游戏。我希望从different/random locations on the entire 0 coordinate of X
产生流星。我应该怎么做到这一点?我见过有人使用Lists
和Random()
,但我需要一个代码用于我的meteorGenerator
课程。然后我会在Game1中调用它的方法
当流星被拉出时,它们应该落在屏幕的底部并消失。 我在这里看到了一个答案,并将其实施到我的班级:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
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;
using Microsoft.Xna.Framework.Net;
namespace SpaceInvaders
{
class meteorGenerator
{
public static Vector2 m_Pos;
public Vector2 m_Position
{
get { return m_Pos; }
set { m_Pos = value; }
}
Texture2D m_Texture { get; set; }
Texture2D m_MeteorsTex;
public meteorGenerator(Texture2D m_Tex, Vector2 m_Pos)
{
m_Position = m_Pos;
m_Texture = m_Tex;
}
List<meteorGenerator> m_MeteorsList = new List<meteorGenerator>();
static readonly Random rnd = new Random();
public void LoadContent(ContentManager Content)
{
m_MeteorsTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\thePlan\\meteorSpawn");
}
public void Update(GameTime gameTime)
{
if (m_MeteorsList.Count() < 4)
{
m_MeteorsList.Add(new meteorGenerator(m_MeteorsTex, new Vector2(rnd.Next(30, 610), rnd.Next(30, 450))));
}
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (meteorGenerator m_Meteor in m_MeteorsList)
{
spriteBatch.Draw(m_Meteor.m_Texture, m_Meteor.m_Position, Color.White);
}
}
}
}
但是当我尝试在Game1中实例化该类的构造函数时,我得到一个错误:
meteorGenerator m_MeteorGenerator;
protected override void Initialize()
{
// TODO: Add your initialization logic here.
m_MeteorGenerator = new meteorGenerator(meteorGenerator.m_Tex, meteorGenerator.m_Pos);
}
Error 1 'SpaceInvaders.meteorGenerator' does not contain a definition for 'm_Tex'
答案 0 :(得分:0)
我认为这样可以解决问题。
我将构造函数的参数重命名为vTexture和vPos,但我同意这些旧样式编码的注释并且非常令人困惑。现在我们将使用
public Vector2 position
public Vector2 Position
{
get { return position }
set { position = value; }
}
public meteorGenerator(Texture2D texture, Vector2 position)
{
Position = texture;
Texture = position;
}
但是现在这就是分裂。
我所做的更改是m_MeteorsList,LoadContent,Update和Draw现在是静态的。
你可以打电话
meteorGenerator.Loadcontent(Content) // to load your content
meteorGenerator.Update(gameTime) // will generate the meteors
meteorGenerator.Draw(spriteBatch) // will draw them.
不需要meteorGenerator类的多个实例。
老实说,我没有想到这个好习惯,因为我会使用一个单独的Meteor类或结构来存储有关流星的信息。
namespace SpaceInvaders
{
class meteorGenerator
{
public Vector2 m_Pos;
public Vector2 m_Position
{
get { return m_Pos; }
set { m_Pos = value; }
}
Texture2D m_Texture { get; set; }
Texture2D m_MeteorsTex;
public meteorGenerator(Texture2D vTex, Vector2 vPos)
{
m_Position = vPos;
m_Texture = vTex;
}
static List<meteorGenerator> m_MeteorsList = new List<meteorGenerator>();
static readonly Random rnd = new Random();
public static void LoadContent(ContentManager Content)
{
m_MeteorsTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\thePlan\\meteorSpawn");
}
public static void Update(GameTime gameTime)
{
if (m_MeteorsList.Count() < 4)
{
m_MeteorsList.Add(new meteorGenerator(m_MeteorsTex, new Vector2(rnd.Next(30, 610), rnd.Next(30, 450))));
}
}
public static void Draw(SpriteBatch spriteBatch)
{
foreach (meteorGenerator m_Meteor in m_MeteorsList)
{
spriteBatch.Draw(m_Meteor.m_Texture, m_Meteor.m_Position, Color.White);
}
}
}
}