XNA Framework“Vector2”问题。 *第一个计划...... *

时间:2012-01-19 16:56:55

标签: c# visual-studio xna

第一次在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();
        }
    }
}

3 个答案:

答案 0 :(得分:5)

Vector2 playerPosition = vector2.zero;

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

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

几乎每个C#库(至少微软的库)都使用这些套管约定:

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

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

答案 1 :(得分:2)

将行更改为

Vector2 playerPosition=Vector2.Zero;

C#区分大小写,因此 v ector2不是 V ector2

答案 2 :(得分:2)

Vector2 playerPosition=Vector2.Zero;

Vector2上的资本V和零上的资本Z.