【问题标题】:XNA Framework "Vector2" issue. *First program...*XNA 框架“Vector2”问题。 *第一个程序... *
【发布时间】:2012-01-20 01:03:17
【问题描述】:

第一次在 C# 中尝试 XNA Framework..

按照本教程进行操作:http://www.uxmagic.com/blog/post/2010/08/17/e2809cHello-Worlde2809d-for-XNA-Game-Studio-40.aspx

一切正常,直到它说添加以下内容的那一行:

Vector2 playerPosition=vector2.zero;

在此行之前,我可以显示我的纹理,用 esc 关闭窗口。等等,整个事情看起来相当简单。

但是当我写完整个代码时,启动游戏给我这个错误......

The name 'vector2' does not exist in the current context    

我错过了导入还是什么?谢谢 !这是完整的代码,因为它显然不在实际的网站上。

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;

namespace WindowsGame2
{

    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D playerTexture;
        Vector2 playerPosition = vector2.zero;

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

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            playerTexture = Content.Load<Texture2D>("character1");
            //base.LoadContent();
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {

            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            if (Keyboard.GetState().IsKeyDown(Keys.Left))
                playerPosition.X--;

            if (Keyboard.GetState().IsKeyDown(Keys.Right))
                playerPosition.X++;

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            //spriteBatch.Draw(playerTexture, Vector2.Zero, Color.White);
            spriteBatch.Draw(playerTexture, playerPosition, Color.White);
            spriteBatch.End();
        }
    }
}

【问题讨论】:

    标签: c# visual-studio xna


    【解决方案1】:
    Vector2 playerPosition = vector2.zero;
    

    那条线。将vector2.zero 更改为Vector2.Zero

    C# 是区分大小写的语言,这就是“vector2.zero”不起作用的原因。

    几乎每个 C# 库(至少 Microsoft 的库)都使用这些大小写约定:

    http://msdn.microsoft.com/en-us/library/ms229043.aspx

    虽然,很多人(包括我自己)使用下划线(“_”)表示私有/受保护的实例变量。

    【讨论】:

      【解决方案2】:

      换行

      Vector2 playerPosition=Vector2.Zero;
      

      C# 区分大小写,因此 vector2 不是 Vector2

      【讨论】:

      • @MattDavey:我不知道你在说什么。 ;)
      【解决方案3】:
      Vector2 playerPosition=Vector2.Zero;
      

      Vector2 上大写 V,零上大写 Z。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多