2014-11-14 53 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); 
     } 
    } 
} 

}