2014-05-08 141 views
1

我是C#的新手,所以,如果這個問題有點傻,我會提前抱歉,當我嘗試運行程序時出現錯誤。從visual Studio 2012我得到這個錯誤信息 「無法修改'GameName1.Cartman.Position'的返回值,因爲它不是一個變量」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); 
     } 
    } 
} 
+0

你應該張貼只是一對夫婦行代碼複製此問題,而不是數百無關行代碼與內某處埋藏的相關幾行代碼。除此之外,僅僅通過谷歌運行你的錯誤信息將導致大量的信息解釋到底發生了什麼。 – Servy

回答

1

你不允許修改Vector2就像是(作爲錯誤指示)。下面是什麼工作:

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的回答包含了一個很好的解釋爲什麼你不允許這樣做。

+0

感謝@BradleyDotNET – Justin

5

當類型是結構時,屬性的返回值不被視爲變量。

所以這個:

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

嘗試一個新的值賦給調用Position屬性,但the Vector2 type is a struct, not a class的結果X財產。

這基本上告訴你,你試圖在腳下自己拍攝,因爲返回結構返回副本。你會,如果這個錯誤沒有彈出,修改副本。

換句話說,上面的代碼是與此類似:

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

但是,由於變量被視爲一個結構在這種情況下複印,「溫度」的變量並沒有真正存在這樣,因此編譯器會阻止你這樣做。

所以你需要讀出位置向量到一個變量,修改它,然後將整個變量分配回屬性。

有點像這樣:

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) 
    ); 
+0

+ +1對於一個更好的解釋比我可以想出 – BradleyDotNET

+0

謝謝,現在它有點讓我心動:D – Justin