【问题标题】:C# 'get' accessor not recognisedC# 'get' 访问器无法识别
【发布时间】:2013-03-05 04:36:16
【问题描述】:

来自http://xbox.create.msdn.com/en-US/education/tutorial/2dgame/creating_the_player,指示使用此代码:

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

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

但是,“get”访问器似乎根本无法识别。我收到以下错误:

  • 名称“get”在当前上下文中不存在。

  • 只有赋值、调用、递增、递减和新对象表达式可以用作语句。

我是否缺少“使用 System.(Something)”行?在调查我的问题时,我已经无数次看到此方法成功使用,但我找不到遇到相同问题的任何人。

我正在使用带有 Microsoft Visual C# 2010 Express 的 XNA Game Studio 4.0。这是我的 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);
    }
}
}

【问题讨论】:

    标签: c# xna get accessor


    【解决方案1】:

    这不是一个有效的属性声明:

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

    () 部分不正确 - 看起来您正在尝试声明方法而不是属性。你应该有:

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

    (我还没有检查其余的,但这很可能是错的。)

    【讨论】:

    • 乔恩,我对这种回复速度感到敬畏!
    • 哇,完美的工作。非常感谢,伙计!将尽快接受答案。
    • 是的,比我打字还快,哈哈
    猜你喜欢
    • 2020-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    相关资源
    最近更新 更多