2014-04-04 20 views
1

我是XNA的新手,發現了一個在線教程,用於製作像使用它的pong遊戲。我正在閱讀教程,但即使它是確切的同樣code.Here是我Game1.cs *代碼:我在一些基本的XNA 4.0代碼中遇到了一個錯誤

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); 
} 
} 
} 

這段代碼到目前爲止僅僅是創建一個槳,用戶可以move.I也有槳的圖像正確加載。這是它產生的錯誤:

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

這是產生這種錯誤的部分:將非常感激

Paddle paddle; 

我已經確切地做了什麼教程說做,它的工作對任何人制造的tutorial.Any的幫助,如果您需要更多信息只是問。謝謝。

回答

1

您需要Paddle類名稱空間的引用,但是如果Paddle類在同一個項目中,只需將名稱空間重命名爲WindowsGame1。

0

賈森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); 
} 
} 
} 
2

槳類是「BreakingOut」命名空間,這是不是您的Game1類引用裏面。

所以你可以在Game1類之前添加「使用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 
{ 
相關問題