我是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;
我已经完成了教程所说的内容,并且它适用于任何制作教程的人。非常感谢任何帮助,如果您需要更多信息,请问。谢谢。
答案 0 :(得分:2)
Paddle课程位于&#34; BreakingOut&#34;命名空间,在您的Game1类中未引用。
所以你可以使用BreakingOut&#34;添加&#34;在Game1类之前,或者更改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
{
答案 1 :(得分:1)
您需要对Paddle类命名空间的引用,但如果Paddle类在同一项目中,则只需将命名空间重命名为WindowsGame1。
答案 2 :(得分:0)
正如Jason struckholf所说,改变桨类的命名空间 使用此代码
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);
}
}
}