2014-11-14 2 views
-1

그래서 저는 우주 침략자 게임을 만들고 있습니다. 유성을 different/random locations on the entire 0 coordinate of X에서 산란시키고 싶습니다. 나는 이것을 어떻게 무시해야합니까? 나는 ListsRandom()을 사용하는 사람들을 보았습니다. 그러나 meteorGenerator 클래스의 코드가 필요합니다. 그럼 Game1의 메소드라고 부릅니다.C# XNA - 무작위로 상단 X 축에 텍스처를 그립니다.

유성이 그려지면 유령이 화면 하단에 떨어지고 사라집니다. 그래서 여기에 대한 답변을보고 내 수업에 구현 :

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

+0

내가 필요한 것은 내 게임에 따라 수행하고 구현하는 접근법/가이드입니다. meteorGenerator 클래스에 코드가 아직 없습니다. 그래서 제가 묻는 것입니다. – PowerUser

+0

좋습니다. 하지만 당신은'List'와'Random'을 사용하는 코드를 보았다고 말합니다. 그것이 참으로 올바른 접근법입니다. 그것에 대해 혼란 스러웠던 점은 무엇입니까? * 그 질문은 당신이 여기에서 묻고 있어야하는 것입니다. – BradleyDotNET

답변

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 클래스의 여러 인스턴스를 가질

meteorGenerator.Loadcontent(Content) // to load your content 
meteorGenerator.Update(gameTime) // will generate the meteors 
meteorGenerator.Draw(spriteBatch) // will draw them. 

필요가 없습니다를 호출 할 수 있습니다.

솔직히 유성에 관한 정보를 저장하기 위해 별도의 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); 
     } 
    } 
} 

}

관련 문제