我有项目,里面有 background.cs 类和 game1.cs 类, 多数民众赞成我的 background.cs 类代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
namespace Rocket
{
class Backgrounds
{
public Texture2D texture;
public Rectangle rectangle;
public void Draw(SpriteBatch spriteBatch){
spriteBatch.Draw(texture, rectangle, Color.White);
}
class Scrolling : Backgrounds{
public Scrolling(Texture2D newTexture, Rectangle newRectangle){
texture = newTexture;
rectangle = newRectangle;
}
public void Update(){
rectangle.X -= 3;
}
}
}
}
和那个 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 Rocket
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Scrolling scrolling1;
Scrolling scrolling2;
” 所以,滚动滚动1;下划线(作为第二个),它表示无法找到类滚动,但它存在!我是XNA的菜鸟,我无法找到它为什么不起作用。任何帮助都会好的!
答案 0 :(得分:0)
为什么你的Scrolling类嵌套在background.cs的Backgrounds类中? C#中嵌套类的默认可见性是私有的,因此它对Game1类不可见。
你应该把这个
class Scrolling : Backgrounds{
public Scrolling(Texture2D newTexture, Rectangle newRectangle){
texture = newTexture;
rectangle = newRectangle;
}
public void Update(){
rectangle.X -= 3;
}
}
进入一个文件scrolling.cs。这为Scrolling和Background提供了类(内部)的默认可见性,这应该适用于您的示例。
您应该明确指定类的可见性。看看:https://msdn.microsoft.com/en-us/library/ms173121.aspx