【发布时间】:2014-11-14 16:44:41
【问题描述】:
所以我正在制作一款太空入侵者游戏。我想让流星从different/random locations on the entire 0 coordinate of X 生成。我应该如何实现这一点?我见过有人使用Lists 和Random()s,但我需要我的meteorGenerator 课程的代码。然后我会在 Game1 中调用它的方法
当流星被绘制时,它们应该落在屏幕底部并消失。 我在 SO 上看到了一个答案并将其实施到我的班级:
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'
【问题讨论】:
-
这对 SO 来说太宽泛了。如果您对技术或您拥有的某些代码有具体的问题;请缩小这个问题。
-
我需要的是一种方法/指南来遵循和实施到我的游戏中。我的 meteorGenerator 类中还没有任何代码,这就是我要问的原因。
-
好的;但你说你见过使用
List和Random的代码。这确实是正确的做法。什么让你感到困惑? 那个问题是你应该在这里问的。 -
@BradleyDotNET 好的,我编辑了我的问题。我可以查看代码吗?有什么问题?
-
你为什么要使用
m_前缀来命名参数,例如public meteorGenerator(Texture2D m_Tex, Vector2 m_Pos)- 这是非常令人困惑和不好的做法