0

我开始使用带有 XNA 框架的 C# 编程。我创建了新的解决方案,制作了程序,保存了它。程序正在运行,并且没有错误。在我打开解决方案的第二天,我在一个文件中有很多错误。代码看起来不错,但注释中有错误:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using The_Destiny_of_Azureus.Komponenty;

namespace The_Destiny_of_Azureus
{

public class Hra : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    public SpriteBatch spriteBatch;

    public int wwidth = 1366;
    public int wheight = 768;

    Texture2D cursor;

    MouseState mouseState;

    public Hra()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    protected override void Initialize()
    {
        graphics.PreferredBackBufferWidth = wwidth;
        graphics.PreferredBackBufferHeight = wheight;
        graphics.IsFullScreen = false;
        graphics.ApplyChanges();

        MMenu mmenu = new MMenu(this);
        Components.Add(mmenu);           //1 error: } expected


        public Button ngbutton = new Button(this, new Vector2(64, 258), "NEW GAME");     // Keyword 'this' is not available in the current context
        Components.Add(ngbutton);       //3 errors:
                                                     //Invalid token '(' in class, struct, or interface member declaration, 
                                                     //Invalid token ')' in class, struct, or interface member declaration
                                                     //Microsoft.XNA.Framework.Game.Components is a property but is used like a type

        base.Initialize();                       //1 error: Method must have a return type
    }


    protected override void LoadContent()     //1 error: Expected class, delegate, enum, interface, or struct
    {

        spriteBatch = new SpriteBatch(GraphicsDevice);      //1 error: Expected class, delegate, enum, interface, or struct

        cursor = Content.Load<Texture2D>(@"Textury\cursor");
    }


    protected override void UnloadContent()            //1 error: Expected class, delegate, enum, interface, or struct
    {

    }


    protected override void Update(GameTime gameTime)        //1 error: Expected class, delegate, enum, interface, or struct
    {

        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();


        mouseState = Mouse.GetState();

        base.Update(gameTime);
    }


    protected override void Draw(GameTime gameTime)           //1 error: Expected class, delegate, enum, interface, or struct
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(cursor, new Vector2(mouseState.X, mouseState.Y), Color.White);      //1 chyba: Expected class, delegate, enum, interface, or struct
        spriteBatch.End(); 

        base.Draw(gameTime);
    }              //1 error: Type or namespace definition, or end-of-file expected
}
}

错误:

Error   } expected  44  35
Error   Invalid token '(' in class, struct, or interface member declaration 47  27
Error   Invalid token ')' in class, struct, or interface member declaration 47  36
Error   Method must have a return type  49  18
Error   Expected class, delegate, enum, interface, or struct    53  28
Error   Expected class, delegate, enum, interface, or struct    56  31
Error   Expected class, delegate, enum, interface, or struct    62  28
Error   Expected class, delegate, enum, interface, or struct    68  28
Error   Expected class, delegate, enum, interface, or struct    81  28
Error   Expected class, delegate, enum, interface, or struct    86  42
Error   Type or namespace definition, or end-of-file expected   90  9
Error   Keyword 'this' is not available in the current context  46  49
Error   'Microsoft.Xna.Framework.Game.Components' is a 'property' but is used like a 'type' 47  13
Error   'The_Destiny_of_Azureus.Hra.ngbutton' is a 'field' but is used like a 'type'    47  28

我尝试将整个解决方案发送给我的朋友,他打开了解决方案并且他在这个文件中没有错误。

4

2 回答 2

8

你的问题在这里:

Components.Add(mmenu);           //1 error: } expected


public Button ngbutton = new Button(this, new Vector2(64, 258), "NEW GAME");     // Keyword 'this' is not available in the current context
Components.Add(ngbutton);       //3 errors:

public关键字对局部变量无效。您要么从类的成员定义中复制并粘贴它,要么只是public错误地添加了。

错误链源于编译器认为您错过了}结束Initialize并且其余支撑被丢弃的事实。

您的同事不可能拥有确切的代码并构建它。

于 2013-03-11T14:30:26.900 回答
3

错误是完全正确的。

public有一个局部变量是没有意义的。

因此,编译器假定您试图在类上创建一个字段,但忘记了}.
这会导致更多错误,因为它在错误假设您需要额外的}.

于 2013-03-11T14:29:55.597 回答