【问题标题】:I have an error in some basic XNA 4.0 code我在一些基本的 XNA 4.0 代码中有错误
【发布时间】:2014-04-04 21:37:15
【问题描述】:

我是 XNA 的新手,并在网上找到了一个使用它制作类似乒乓球游戏的教程。我正在阅读教程,但即使代码完全相同,我也会收到错误消息。这是我的 Game1.cs*代码:

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 WindowsGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Paddle paddle;
        Rectangle screenBounds;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            screenBounds = new Rectangle(
            0,
            0,
            graphics.PreferredBackBufferWidth,
            graphics.PreferredBackBufferHeight);
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Texture2D tempTexture = Content.Load<Texture2D>("paddle");
            paddle = new Paddle(tempTexture, screenBounds);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            paddle.Update();
            base.Update(gameTime);
        }


        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            paddle.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }

    }
} 

我也有这个课

using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace BreakingOut
{
 class Paddle
 {
 Vector2 position;
 Vector2 motion;
 float paddleSpeed = 8f;
 KeyboardState keyboardState;
 GamePadState gamePadState;
 Texture2D texture;
     Rectangle screenBounds;
 public Paddle(Texture2D texture, Rectangle screenBounds)
 {
 this.texture = texture;
 this.screenBounds = screenBounds;
 SetInStartPosition();
 }
 public void Update()
 {
 motion = Vector2.Zero;
 keyboardState = Keyboard.GetState();
 gamePadState = GamePad.GetState(PlayerIndex.One);
 if (keyboardState.IsKeyDown(Keys.Left))
 motion.X = -1;
 if (keyboardState.IsKeyDown(Keys.Right))
 motion.X = 1;
 if (gamePadState.ThumbSticks.Left.X < -.5f)
 motion.X = -1;
 if (gamePadState.ThumbSticks.Left.X > .5f)
 motion.X = 1;
 motion.X *= paddleSpeed;
 position += motion;
 LockPaddle();
 }
 private void LockPaddle()
 {
 if (position.X < 0)
 position.X = 0;
 if (position.X + texture.Width > screenBounds.Width)
 position.X = screenBounds.Width - texture.Width;
 }
 public void SetInStartPosition()
 {
 position.X = (screenBounds.Width - texture.Width) / 2;
 position.Y = screenBounds.Height - texture.Height - 5;
 }
 public void Draw(SpriteBatch spriteBatch)
 {
 spriteBatch.Draw(texture, position, Color.White);
 }
 public Rectangle GetBounds()
 {
 return new Rectangle(
 (int)position.X, 
 (int)position.Y, 
 texture.Width,
 texture.Height);
 }
 }
}

到目前为止,此代码仅用于创建用户可以移动的桨。我还正确加载了桨的图像。这是它产生的错误:

The type or namespace name 'Paddle' could not be found (are you missing a using directive or an assembly reference?)

这是产生此错误的部分:

Paddle paddle;

我已经完全按照教程所说的做了,它对制作教程的人都有效。非常感谢任何帮助,如果您需要更多信息,请询问。谢谢。

【问题讨论】:

    标签: c# xna xna-4.0 pong


    【解决方案1】:

    Paddle 类位于“BreakingOut”命名空间内,您的 Game1 类没有引用该命名空间。

    所以您可以在 Game1 类之前添加“using BreakingOut”,或者更改 Paddle 的命名空间。

    选项 1 如下所示:

    using Microsoft.Xna.Framework.Media;
    
    using BreakingOut;     // Add the namespace of the object!!!!!
    
    namespace WindowsGame1
    

    选项 2 是:

    using Microsoft.Xna.Framework.Input;
    namespace WindowsGame1    // Change the namespace of your object!!!!!
    {
     class Paddle
     {
    

    【讨论】:

      【解决方案2】:

      您需要对 Paddle 类命名空间的引用,但如果 Paddle 类在同一个项目中,只需将命名空间重命名为 WindowsGame1。

      【讨论】:

        【解决方案3】:

        正如 Jason strikeholf 所说,要更改 paddle 类的命名空间 使用此代码

        using System.Text;
           using Microsoft.Xna.Framework;
           using Microsoft.Xna.Framework.Graphics;
        using Microsoft.Xna.Framework.Input;
        namespace  WindowsGame1
        {
         class Paddle
         {
         Vector2 position;
         Vector2 motion;
         float paddleSpeed = 8f;
         KeyboardState keyboardState;
         GamePadState gamePadState;
         Texture2D texture;
             Rectangle screenBounds;
         public Paddle(Texture2D texture, Rectangle screenBounds)
         {
         this.texture = texture;
         this.screenBounds = screenBounds;
         SetInStartPosition();
         }
         public void Update()
         {
         motion = Vector2.Zero;
         keyboardState = Keyboard.GetState();
         gamePadState = GamePad.GetState(PlayerIndex.One);
         if (keyboardState.IsKeyDown(Keys.Left))
         motion.X = -1;
         if (keyboardState.IsKeyDown(Keys.Right))
         motion.X = 1;
         if (gamePadState.ThumbSticks.Left.X < -.5f)
         motion.X = -1;
         if (gamePadState.ThumbSticks.Left.X > .5f)
         motion.X = 1;
         motion.X *= paddleSpeed;
         position += motion;
         LockPaddle();
         }
         private void LockPaddle()
         {
         if (position.X < 0)
         position.X = 0;
         if (position.X + texture.Width > screenBounds.Width)
         position.X = screenBounds.Width - texture.Width;
         }
         public void SetInStartPosition()
         {
         position.X = (screenBounds.Width - texture.Width) / 2;
         position.Y = screenBounds.Height - texture.Height - 5;
         }
         public void Draw(SpriteBatch spriteBatch)
         {
         spriteBatch.Draw(texture, position, Color.White);
         }
         public Rectangle GetBounds()
         {
         return new Rectangle(
         (int)position.X, 
         (int)position.Y, 
         texture.Width,
         texture.Height);
         }
         }
        }
        

        【讨论】:

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