2013-03-05 10 views
6

Od http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/creating_the_player, to jest instruowany, że kod ten należy stosować:C# 'get' accessor nie rozpoznał

public int Width() 
    { 
     get { return PlayerTexture.Width; } 
    } 

    public int Height() 
    { 
     get { return PlayerTexture.Height; } 
    } 

Jednak 'get' accessor nie wydaje się być rozpoznawane w ogóle. Pojawiają się następujące błędy:

  • Nazwa "get" nie istnieje w bieżącym kontekście.

  • Przyporządkowanie, wywołanie, inkrementacja, dekrementacja i nowe wyrażenia obiektowe mogą być używane tylko jako instrukcja.

Czy brakuje mi linii "za pomocą System. (Something)"? Zauważyłem, że udało się to z powodzeniem niezliczoną ilość razy podczas badania mojego problemu, ale nie mogę znaleźć nikogo, kto wpadłby na to samo.

Używam XNA Game Studio 4.0 z Microsoft Visual C# 2010 Express. To jest moje pełny kod dla klasy Player.cs:

using System; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 

namespace Shooter 
{ 
class Player 
{ 
    private Texture2D PlayerTexture; 
    public Vector2 Position; 
    public bool Active; 
    public int Health; 

    public int Width() 
    { 
     get { return PlayerTexture.Width; } 
    } 

    public int Height() 
    { 
     get { return PlayerTexture.Height; } 
    } 

    public void Initialise(Texture2D texture, Vector2 position) 
    { 
     PlayerTexture = texture; 
     Position = position; 
     Active = true; 
     Health = 100; 
    } 

    public void Update() 
    { 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(PlayerTexture, Position, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 0f); 
    } 
} 
} 

Odpowiedz

13

To nie jest ważna deklaracja nieruchomości:

public int Width() 
{ 
    get { return PlayerTexture.Width; } 
} 

() część jest błędna - który wygląda jak starasz się oświadczyć metoda, a nie własność. Powinieneś mieć:

public int Width 
{ 
    get { return PlayerTexture.Width; } 
} 

(. Nie sprawdzałem resztę, ale może również być wszystko, co jest źle)

+0

Jon, jestem pod wrażeniem tej prędkości odpowiedzi! – Sam

+0

Wow, działało idealnie. Dzięki za tonę, stary! Przyjmie odpowiedź tak szybko, jak tylko pozwoli. – user2134261

+0

Tak, szybciej niż mógłbym wpisać mój, LOL –

0

Trzeba będzie usunąć () The () wskazuje swój sposób, nie właściwość

Metoda:

public int Width() 
{ 
    return PlayerTexture.Width; 
} 

nieruchomości:

public int Width 
{ 
    get { return PlayerTexture.Width; } 
}