首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在monogame中克隆和绘制对象

在monogame中克隆和绘制对象
EN

Stack Overflow用户
提问于 2021-11-04 02:26:15
回答 1查看 67关注 0票数 0

如何克隆一个对象,然后选择一个随机位置,然后绘制它。

这是我为对象编写的代码:

代码语言:javascript
运行
复制
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中的代码:

代码语言:javascript
运行
复制
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’的定义,并且无法找到接受'object‘类型的第一个参数的可访问扩展方法'Position’(您是否缺少using指令或程序集引用?)“

EN

Stack Overflow用户

回答已采纳

发布于 2021-11-04 07:47:22

你的构造函数:

代码语言:javascript
运行
复制
public Trash(Texture2D texture)
{
    _texture = texture;
}

需要使用所需的可变参数进行扩展。在本例中,它需要添加PositionOrigin作为参数,然后将其作为值应用。

如下所示:

代码语言:javascript
运行
复制
public Trash(Texture2D texture, Vector2 position, Vector2 origin)
{
    _texture = texture;
    Position = position;
    Origin = origin;
}

并改变在game1.cs中调用它的方式,它们需要类似于texture的工作方式

代码语言:javascript
运行
复制
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);

作为提示:保持字段名称的一致性,在一个字段中混合使用下划线和小写,而在另一个字段中混合使用大写将使人难以理解。尤其是当参数也需要与字段不同的名称时。我更喜欢将它们全部保留为第一个字母大写。

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69833890

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档