我是xna的新手,我正在制作一个RPG。当我尝试按项目构建时,我得到错误:
1)当前上下文中不存在名称“component”:
if (component is DrawableGameComponent)
((DrawableGameComponent)component).Visible = true;
2)当前上下文中不存在名称“content”:
LoadContent(content);
在 GameScreen 类中找到了“组件”错误,并在 StartScreen <中找到“内容”错误/ strong> class。
为什么会发生这些错误,这是什么意思?感谢。
Game1.cs
namespace myRPG
{
/// <summary>
/// Default Project Template
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
SpriteFont font;
//hold current state of the keyboard
KeyboardState keyboardState;
//hold previous state of the keyboard
KeyboardState oldKeyboardState;
//active screen will be set to either startScreen or actionScreen
GameScreen activeScreen;
StartScreen startScreen;
ActionScreen actionScreen;
CharScreen charScreen;
ClassScreen classScreen;
GenderScreen genderScreen;
StatBar statBar;
Vector2 charPosition = new Vector2(0, 0);
Texture2D charSprite;
int charHorizSpeed = 1;
int charVertSpeed = 1;
Texture2D logoTexture;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = false;
oldKeyboardState = Keyboard.GetState();
}
/// <summary>
/// Overridden from the base Game.Initialize. Once the GraphicsDevice is setup,
/// we'll use the viewport to initialize some values.
/// </summary>
protected override void Initialize()
{
base.Initialize();
}
/// <summary>
/// Load your graphics content.
/// </summary>
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
charSprite = this.Content.Load<Texture2D>("charSprite");
statBar = new StatBar(Content);
//create new instance of the startScreen
startScreen = new StartScreen(
this,
spriteBatch,
//loads the font to the screen
font = Content.Load<SpriteFont>("menufont"),
//loads the image to the screen
Content.Load<Texture2D>("RPGLogo"));
//adds the screen to components
Components.Add(startScreen);
//startScreen.Hide();
//creates new instance the actionScreen
actionScreen = new ActionScreen(
this,
spriteBatch,
charSprite = Content.Load<Texture2D>("charSprite"));
//adds the screen to components
Components.Add(actionScreen);
//actionScreen.Hide();
activeScreen.Hide();
activeScreen = startScreen;
activeScreen.Show();
charScreen = new CharScreen(
this,
spriteBatch,
font = Content.Load<SpriteFont>("menufont"),
charSprite = Content.Load<Texture2D>("charSprite"));
Components.Add(charScreen);
//charScreen.Hide ();
activeScreen.Hide();
activeScreen = charScreen;
activeScreen.Show();
classScreen = new ClassScreen(
this,
spriteBatch,
font = Content.Load<SpriteFont>("menufont"),
charSprite = Content.Load<Texture2D>("charSprite"));
Components.Add(classScreen);
activeScreen.Hide();
activeScreen = classScreen;
activeScreen.Show();
}
/// <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)
{
//get hte current state of the keyboard
keyboardState = Keyboard.GetState();
UpdateSprite(gameTime);
statBar.Update();
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
//checks if instances are the same
if (activeScreen == startScreen)
{
//checks if enter key was pressed
if (CheckKey(Keys.Enter))
{
//if the selected index is on the first item (start game), the current active screen will hide adn it will be switched to the action screen
if (startScreen.SelectedIndex == 0)
{
activeScreen.Hide();
activeScreen = actionScreen;
activeScreen.Show();
}
//if the selected index is on the second item (exit) the game will exit
if (startScreen.SelectedIndex == 1)
{
this.Exit();
}
}
}
if (activeScreen == charScreen)
{
if (CheckKey(Keys.Enter))
{
if (charScreen.SelectedIndex == 0)
{
activeScreen.Hide();
activeScreen = classScreen;
activeScreen.Show();
//create a drop down menu for character class options/pop up?
}
}
if (CheckKey(Keys.Enter))
{
if (charScreen.SelectedIndex == 1)
{
activeScreen.Hide();
activeScreen = genderScreen;
activeScreen.Show();
}
}
}
if (activeScreen == classScreen)
{
if (CheckKey(Keys.Enter))
{
if (classScreen.SelectedIndex == 0)
{
//call warior class
}
if (classScreen.SelectedIndex == 1)
{
//call mage class
}
if (classScreen.SelectedIndex == 2)
{
//call ranger class
}
}
}
if (activeScreen == genderScreen)
{
if (CheckKey(Keys.Enter))
{
if (genderScreen.SelectedIndex == 0)
{
//call gender class (male)
}
if (genderScreen.SelectedIndex == 1)
{
//call gender class (female)
}
}
}
base.Update(gameTime);
oldKeyboardState = keyboardState;
}
private bool CheckKey(Keys theKey)
{
//returns if the key was pressed in the last frame
return keyboardState.IsKeyUp(theKey) &&
oldKeyboardState.IsKeyDown(theKey);
}
private void DrawStartScreen()
{
spriteBatch.DrawString(font, "Vengence In Albion", new Vector2(20, 45), Color.White);
}
private void DrawCharScreen()
{
spriteBatch.DrawString(font, "Character Selection", new Vector2(20, 45), Color.White);
spriteBatch.Draw(charSprite, charPosition, Color.White);
}
private void DrawClassScreen()
{
spriteBatch.DrawString(font, "Choose your Class", new Vector2(20, 45), Color.White);
}
private void DrawGenderScreen()
{
spriteBatch.DrawString(font, "Choose a gender", new Vector2(20, 45), Color.White);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
private void UpdateSprite(GameTime gameTime)
{
//move the sprite by speed
KeyboardState newState = Keyboard.GetState();
int MaxX = Window.ClientBounds.Width - charSprite.Width;
int MaxY = Window.ClientBounds.Height - charSprite.Height;
int MinX = 0;
int MinY = 0;
if (newState.IsKeyDown(Keys.Left))
{
// Move left
charHorizSpeed = -1;
}
if (newState.IsKeyDown(Keys.Right))
{
// Move left
charHorizSpeed = 1;
}
if (newState.IsKeyDown(Keys.Up))
{
// Move left
charVertSpeed = -1;
}
if (newState.IsKeyDown(Keys.Down))
{
// Move left
charVertSpeed = 1;
}
if (charPosition.X > MaxX)
{
charHorizSpeed *= -1;
charPosition.X = MaxX;
}
else if (charPosition.X < MinX)
{
charHorizSpeed *= -1;
charPosition.X = MinX;
}
if (charPosition.Y > MaxY)
{
charVertSpeed *= -1;
charPosition.Y = MaxY;
}
else if (charPosition.Y < MinY)
{
charVertSpeed *= -1;
charPosition.Y = MinY;
}
oldKeyboardState = keyboardState;
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.DarkSlateBlue);
spriteBatch.Begin();
switch (activeScreen)
{
case startScreen:
DrawStartScreen();
startScreen.Draw(spriteBatch);
//StartScreen ();
break;
case charScreen:
DrawCharScreen();
charScreen.Draw(spriteBatch);
//CharScreen ();
break;
case actionScreen:
//draw map
statBar.Draw(spriteBatch);
break;
case classScreen:
DrawClassScreen();
classScreen.Draw();
//ClassScreen ();
statBar.Draw(spriteBatch);
break;
case genderScreen:
DrawGenderScreen();
genderScreen.Draw(spriteBatch);
//GenderScreen ();
break;
}
base.Draw(gameTime);
spriteBatch.End();
}
}
GameScreen类
foreach (GameComponent component in components)
component.Enabled = true;
if (component is DrawableGameComponent)
((DrawableGameComponent)component).Visible = false;
StartScreen类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
namespace AnyRPG
{
class StartScreen : GameScreen
{
MenuComponent menuComponent;
//background image
Texture2D RPGLogo;
//used to make the image fill the game window
Rectangle rectangleScreen;
//gets the users menu choice
public int SelectedIndex
{
//gets what the user chooses which is set to SelectedIndex
get
{
return menuComponent.SelectedIndex;
}
set
{
menuComponent.SelectedIndex = value;
}
}
public StartScreen(Game game, SpriteBatch spriteBatch, SpriteFont spriteFont, Texture2D image)
: base(game, spriteBatch)
{
LoadContent(content);
//constructor creates menu with start and exit options
string[] menuOptions = { "Start Game", "Leave Game" };
//the menu is added to the list of components
menuComponent = new MenuComponent(game,
spriteBatch,
spriteFont,
menuOptions);
Components.Add(menuComponent);
//sets the image to the image that is sent in
this.RPGLogo = RPGLogo;
// a rectangle is created which fill the game screen
rectangleScreen = new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height / 2);
}
private void LoadContent(ContentManager content)
{
RPGLogo = content.Load<Texture2D>("RPGLogo");
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
spriteBatch.Draw(RPGLogo, rectangleScreen, Color.AntiqueWhite);
base.Draw(gameTime);
}
}
}
答案 0 :(得分:5)
添加花括号:
foreach (GameComponent component in components)
{ component.Enabled = true;
if (component is DrawableGameComponent)
((DrawableGameComponent)component).Visible = false;
}
如果不这样做,只有第一行(component.Enabled = true;
)会在你的foreach中。下一行不会对component
变量有任何线索。
答案 1 :(得分:0)
在你正在做的StartScreen
构造函数中
LoadContent(content);
但编译器不知道content
是什么。也许你想将它作为参数添加到构造函数中。
或者更好,做
LoadContent(Game.Content);
因为StartScreen
应该从DrawableGameContent
继承。