【问题标题】:C# XNA - randomly draw a texture on the top X axisC# XNA - 在顶部 X 轴上随机绘制纹理
【发布时间】:2014-11-14 16:44:41
【问题描述】:

所以我正在制作一款太空入侵者游戏。我想让流星从different/random locations on the entire 0 coordinate of X 生成。我应该如何实现这一点?我见过有人使用ListsRandom()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 类中还没有任何代码,这就是我要问的原因。
  • 好的;但你说你见过使用ListRandom 的代码。这确实是正确的做法。什么让你感到困惑? 那个问题是你应该在这里问的。
  • @BradleyDotNET 好的,我编辑了我的问题。我可以查看代码吗?有什么问题?
  • 你为什么要使用 m_ 前缀来命名参数,例如 public meteorGenerator(Texture2D m_Tex, Vector2 m_Pos) - 这是非常令人困惑和不好的做法

标签: c# random xna


【解决方案1】:

我认为这可以解决问题。

我将构造函数的参数重命名为 vTexture 和 vPos,但我同意 cmets 这是旧式编码并且非常混乱。现在我们将使用

 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.

不需要多个流星生成器类的实例。

老实说,我不认为这是一个好的做法,因为我会使用单独的 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);
        }
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多