簡體   English   中英

在單游戲中克隆和繪制對象

[英]Cloning and drawing Objects in monogame

如何克隆一個對象,然后選擇一個隨機位置,然后繪制它。

這是我擁有的對象代碼:

public class Trash : ICloneable
    {
        private Texture2D _texture;

        private float _rotation;

        public Vector2 Position;

        
        public Vector2 Origin;

        
        public float RotationVelocity = 3f;

       
        public float LinearVelocity = 4f;

        public Trash(Texture2D texture)
        {
            _texture = texture;
        }

        public void Update()
        {
         // Do epic stuff here            

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_texture, Position, null, Color.White, _rotation, Origin, 1, SpriteEffects.None, 0f);
        }
        public object Clone()
        {
            return this.MemberwiseClone();
        }

這是到目前為止我在 Game1.cs 中的代碼:

public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private SeaJam.Objects.Trash Trash;
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            var texture = Content.Load<Texture2D>("Prototype");

            Trash = new Objects.Trash(texture)
            {
                Position = new Vector2(100, 100),
                Origin = new Vector2(texture.Width / 2, texture.Height - 25),
            };
        }
        protected override void Update(GameTime gameTime)
        {
            Trash.Update();

            base.Update(gameTime);
        }
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            Trash.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }
        private void AddTrash()
        {
            var rnd = new System.Random();
            var NewTrash = Trash.Clone();

            
        }

問題是每當我嘗試在 AddTrash() 方法中為克隆提供隨機位置時,我只會得到錯誤,例如“'object'不包含'Position'的定義並且沒有可訪問的擴展方法可以找到接受類型為“對象”的第一個參數的“位置”(您是否缺少 using 指令或程序集引用?)”

你的構造函數:

public Trash(Texture2D texture)
{
    _texture = texture;
}

需要使用所需的可變參數進行擴展。 在您的情況下,它需要添加PositionOrigin作為參數,然后將其作為值應用。

像這樣:

public Trash(Texture2D texture, Vector2 position, Vector2 origin)
{
    _texture = texture;
    Position = position;
    Origin = origin;
}

並更改您在 game1.cs 中調用它的方式,它們需要像texture一樣工作:

var texture = Content.Load<Texture2D>("Prototype");
var position = new Vector2(100, 100),
var origin = new Vector2(texture.Width / 2, texture.Height - 25),

Trash = new Objects.Trash(texture, position, origin);

作為提示:保持字段名稱的一致性,在一個字段中混合使用下划線和小寫字母,在另一個字段中混合使用大寫字母會讓人難以理解。 特別是當參數也需要與字段不同的名稱時。 我更喜歡將它們全部保留為第一個字母大寫。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM