无法修改的返回值 [英] Cannot modify the return value of

查看:139
本文介绍了无法修改的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,如果这个问题有点愚蠢,请您提前抱歉,尝试运行该程序时出现错误.从visual Studio 2012我收到此错误消息 无法修改'GameName1.Cartman.Position'的返回值,因为它不是变量",在第30行和第31行中两次.我希望有人可以对此提供帮助.

I'm new at C# so, sorry in advance if this question is a bit silly, I get a error when i try to run the program. From visual Studio 2012 I got this error message "Cannot modify the return value of 'GameName1.Cartman.Position' because it is not a variable" 2 times, on line 30 and 31. I hope some one can help me with this.

下面是代码:

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GameName1
{
    class Cartman
    {
        public Vector2 Position { get; set; }
        public float Scale { get; set; }
        public int Direction { get; set; }
        public float Rotation { get; set; }
        public Texture2D Texture { get; set; }

    public Vector2 Origin {
        get
        {
            return new Vector2(Texture.Width / 2, Texture.Height / 2);
        } 
    }

    public Rectangle BoundingBox
    { 
        get 
        {
            return new Rectangle(
                (int)(Position.X = Texture.Width / 2 * Scale), // line 30
                (int)(Position.Y = Texture.Width / 2 * Scale), // line 31
                (int)(Texture.Width * Scale),
                (int)(Texture.Height * Scale)
                );
        }
    }
    public Cartman()
    {
        Direction = 1; //standaard naar rechts
    }
    }
}

这是我的另一堂课:

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
#endregion

namespace GameName1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private Cartman _cartman;

        public Game1()
            : base()
        {
            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

            _cartman = new Cartman();
            _cartman.Position = new Vector2(100, 100);
            _cartman.Scale = 0.3f;

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            _cartman.Texture = Content.Load<Texture2D>("cartman");

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            _cartman.Position = new Vector2(_cartman.Position.X + 1 *_cartman.Direction, _cartman.Position.Y);

            _cartman.Rotation += 0.3f * _cartman.Direction;

            if(!GraphicsDevice.Viewport.Bounds.Contains(_cartman.BoundingBox))
            {
                _cartman.Direction *= -1;
            }
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Orange);

            // TODO: Add your drawing code here
            spriteBatch.Begin();

            spriteBatch.Draw(   _cartman.Texture,
                                _cartman.Position,
                                null,
                                Color.White,
                                _cartman.Rotation,
                                new Vector2 (0,0),
                                _cartman.Scale,
                                SpriteEffects.None,
                                0);

            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}

推荐答案

当类型为struct时,属性的返回值不视为变量.

The return value from a property is not considered a variable when the type is a struct.

所以这个:

(int)(Position.X = Texture.Width / 2 * Scale),

尝试将新值分配给调用Position属性的结果的X属性,但是

Attempts to assign a new value to the X property of the result of calling the Position property, but the Vector2 type is a struct, not a class.

这基本上是在告诉您您要用脚射击自己,因为返回结构会返回副本.如果未弹出此错误,则可以修改副本.

This is basically telling you that you're attempting to shoot yourself in the foot, because returning a struct returns a copy. You would, if this error didn't pop up, modify the copy.

换句话说,上面的代码与此类似:

In other words, the above code is similar to this:

var temp = Position;
(int)(temp.X = Texture.Width / 2 * Scale),

但是,由于该变量被视为结构的副本,因此在这种情况下,"temp"变量实际上并不存在,因此编译器会阻止您这样做.

But, since the variable is considered a copy of a struct, the "temp" variable in this case does not really exist as such, and thus the compiler prevents you from doing it this way.

因此,您需要读取位置向量到一个变量,对其进行修改,然后将整个变量分配回该属性.

So you need to read out the position vector to a variable, modify that, and then assign the whole variable back to the property.

有点像:

Position = new Vector2(Texture.Width / 2 * Scale, Texture.Width / 2 * Scale);
return new Rectangle(
    (int)Position.X,
    (int)Position.Y,
    (int)(Texture.Width * Scale),
    (int)(Texture.Height * Scale)
    );

这篇关于无法修改的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆