的返回值不能修改
本文关键字:修改 不能 返回值 | 更新日期: 2023-09-27 18:02:20
我是c#的新手,所以,如果这个问题有点傻,我提前抱歉,当我尝试运行程序时,我会得到一个错误。从visual Studio 2012我得到了这个错误信息"无法修改GameName1.Cartman的返回值。定位"因为它不是一个变量"2次,在第30行和31行。我希望有人能帮我解决这个问题。
下面是代码: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);
}
}
}
当类型为结构体时,属性的返回值不被视为变量。
:
(int)(Position.X = Texture.Width / 2 * Scale),
尝试为调用Position
属性的结果的X
属性赋一个新值,但Vector2
类型是struct
,而不是class
。
这基本上是在告诉你,你在试图搬起石头砸自己的脚,因为返回一个struct 返回一个副本。如果没有弹出此错误,您可以修改副本。
也就是说,上面的代码类似于:var temp = Position;
(int)(temp.X = Texture.Width / 2 * Scale),
但是,由于变量被认为是结构体的副本,因此在这种情况下,"temp"变量实际上并不存在,因此编译器会阻止您这样做。
所以你需要读出位置向量给一个变量,修改它,然后把整个变量赋值给属性。
有点像这样:
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)
);
您不允许这样修改Vector2
s(如错误所示)。下面是可行的方法:
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)
);
注意,我没有尝试修改Vector2
的组件,而是创建了一个新的组件,然后在矩形构造函数中使用它。Lasse V. Karlsen的回答很好地解释了为什么不允许这样做