2014-11-14 29 views
-1

所以我正在制作太空入侵者游戏。我想要从different/random locations on the entire 0 coordinate of X产生流星。我应该怎么办?我见过使用ListsRandom() s的人,但我需要我的meteorGenerator类的代码。然后我将其称为Game1中的方法C#XNA - 随机在顶部X轴上绘制纹理

当流星被绘制时,它们应该落在屏幕的底部并消失。 我在这里看到了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'

+0

这是SO过于宽泛。如果您有关于该技术的*特定*问题或者您拥有的某些代码;请缩小这个问题的范围。 – BradleyDotNET 2014-11-14 18:42:51

+0

我需要的是一种方法/指南,以遵循并贯彻到我的游戏中。我的meteorGenerator类中没有任何代码,这就是为什么我问。 – PowerUser 2014-11-14 18:49:13

+0

好的;但你说你已经看过使用'List'和'Random'的代码。这确实是正确的做法。什么让你感到困惑? *这个问题是你应该在这里问的。 – BradleyDotNET 2014-11-14 18:50:49

回答

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); 
     } 
    } 
} 

}