表达式表示“类型”,其中需要“变量”、“值”或“方法组”(CS0119) (MyFirstGame)
Expression denotes a `type', where a `variable', `value' or `method group' was expected (CS0119) (MyFirstGame)
我正在学习使用 monodevelop 和 monogame 在 C# 中编程。我对 C 和 Java 相当熟悉,但老实说,我仍在学习像面向 Object 的程序员一样思考。我想用下面的 class 来收集精灵的相关属性,以及移动、绘制、加速、检测碰撞等方法来整理游戏代码。此 class 出现在文件 SpriteObject.cs 中并且编译时没有错误。
namespace MyFirstGame
{
class SpriteObject
{
private Texture2D Texture;
private Vector2 Position;
private Vector2 Velocity;
public SpriteObject(Texture2D texture, Vector2 position)
{
this.Texture = texture;
this.Position = position;
this.Velocity = Vector2.Zero;
}
public SpriteObject(Texture2D texture, Vector2 position, Vector2 velocity)
{
this.Texture = texture;
this.Position = position;
this.Velocity = velocity;
}
public Rectangle BoundingBox
{
get
{
return new Rectangle(
(int)Position.X,
(int)Position.Y,
Texture.Width,
Texture.Height);
}
}
public void Move(SpriteObject sprite)
{
this.Position += this.Velocity;
}
public void BounceX(SpriteObject sprite)
{
this.Velocity.X *= -1;
}
public void BounceY(SpriteObject sprite)
{
this.Velocity.Y *= -1;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, Color.White);
}
}
}
我正在尝试在下面的代码中使用这个 class(这是不整洁的混蛋,但是 运行,我正在尝试清理的代码和新的代码class 我正在尝试这样做)。请特别注意标有 A> 和 B> 的行。
namespace MyFirstGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics; // All default to private
KeyboardState oldKeyState;
SpriteBatch spriteBatch;
Boolean gameOn=true;
A> SpriteObject ballSprite, starSprite; // A couple of sprites...
Texture2D texStar, texBall; // This is a texture we can render.
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 900-32; // Force window size or it won't work...
graphics.PreferredBackBufferWidth = 1600; // Force window size
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
}
/// <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
oldKeyState = Keyboard.GetState();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
// Set the coordinates to draw the sprite at.
Vector2 starPosition = Vector2.Zero;
Vector2 ballPosition = new Vector2(200, 0);
// Store some information about the sprite's motion.
Vector2 starSpeed = new Vector2(10.0f, 10.0f);
Vector2 ballSpeed = new Vector2(10.0f, 0.0f);
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texStar = Content.Load<Texture2D>("star");
texBall = Content.Load<Texture2D>("ball");
B> ballSprite = new SpriteObject(texBall, Vector2(200, 0), Vector2(10.0f, 0.0f)); // create two sprites
B> starSprite = new SpriteObject(texStar, Vector2.Zero, Vector2(10.0f, 10.0f));
}
/// <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)
{
// Move the sprite by speed.
KeyboardState newKeyState = Keyboard.GetState();
if (newKeyState.IsKeyDown(Keys.Escape))
Exit();
if (collisionDetected()) gameOn = false; // If there's a collision then no movement.
//Could do with putting this in a seperate method or class, if there isn't one already...
if (gameOn)
{
if (newKeyState.IsKeyDown(Keys.PageUp) && oldKeyState.IsKeyUp(Keys.PageUp))
{
starSpeed.X *= 1.1f;
starSpeed.Y *= 1.1f;
}
else if (newKeyState.IsKeyDown(Keys.PageDown) && oldKeyState.IsKeyUp(Keys.PageDown))
{
starSpeed.X *= 0.9f;
starSpeed.Y *= 0.9f;
}
else if (newKeyState.IsKeyDown(Keys.Up) && oldKeyState.IsKeyUp(Keys.Up))
{
starSpeed.Y += 1.0f;
}
else if (newKeyState.IsKeyDown(Keys.Down) && oldKeyState.IsKeyUp(Keys.Down))
{
starSpeed.Y -= 1.0f;
}
else if (newKeyState.IsKeyDown(Keys.Left) && oldKeyState.IsKeyUp(Keys.Left))
{
starSpeed.X -= 1.0f;
}
else if (newKeyState.IsKeyDown(Keys.Right) && oldKeyState.IsKeyUp(Keys.Right))
{
starSpeed.X += 1.0f;
}
oldKeyState = newKeyState;
starPosition += starSpeed;
ballPosition += ballSpeed;
int MaxX =
graphics.GraphicsDevice.Viewport.Width - texStar.Width;
int MinX = 0;
int MaxY =
graphics.GraphicsDevice.Viewport.Height - texStar.Height;
int MinY = 0;
// Check for ball bounce
if (ballPosition.X > MaxX)
{
ballPosition.X = MaxX;
ballSpeed.X *= -1;
}
if (ballPosition.X < MinX)
{
ballPosition.X = MinX;
ballSpeed.X *= -1;
}
if (ballPosition.Y > MaxY)
{
ballSpeed.Y *= -1;
ballPosition.Y = MaxY;
}
ballSpeed.Y += 1;
// Check for bounce.
if (starPosition.X > MaxX)
{
starSpeed.X *= -1;
starPosition.X = MaxX;
}
else if (starPosition.X < MinX)
{
starSpeed.X *= -1;
starPosition.X = MinX;
}
if (starPosition.Y > MaxY)
{
starSpeed.Y *= -1;
starPosition.Y = MaxY;
}
else if (starPosition.Y < MinY)
{
starSpeed.Y *= -1;
starPosition.Y = MinY;
}
}
else
{
starSpeed=Vector2.Zero;
ballSpeed=Vector2.Zero;
}
}
private Boolean collisionDetected()
{
if (((Math.Abs(starPosition.X - ballPosition.X) < texStar.Width) && (Math.Abs(starPosition.Y - ballPosition.Y) < texBall.Height)))
{
return true;
}
else
{
return false;
}
}
/// <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)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(texStar, starPosition, Color.White);
spriteBatch.Draw(texBall, ballPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
A> 标记新 SpriteObject ballSprite, starSprite;
声明没有任何问题。
B> 标记在我尝试使用 SpriteObject 构造函数创建并为我之前声明的 SpriteObjects 赋值时出现的错误 - 根据标题的错误post。第一行有两个,第二行有一个。
名称空间暗示了我的 C# 经验水平。如果您能帮助我理解我几乎可以肯定的基本错误,我将不胜感激。
您指出的那两行代码缺少 'new' operator,这是实例化 Vector2
(或任何)class.
所必需的
ballSprite = new SpriteObject(texBall, new Vector2(200, 0), new Vector2(10.0f, 0.0f));
starSprite = new SpriteObject(texStar, Vector2.Zero, new Vector2(10.0f, 10.0f));
我正在学习使用 monodevelop 和 monogame 在 C# 中编程。我对 C 和 Java 相当熟悉,但老实说,我仍在学习像面向 Object 的程序员一样思考。我想用下面的 class 来收集精灵的相关属性,以及移动、绘制、加速、检测碰撞等方法来整理游戏代码。此 class 出现在文件 SpriteObject.cs 中并且编译时没有错误。
namespace MyFirstGame
{
class SpriteObject
{
private Texture2D Texture;
private Vector2 Position;
private Vector2 Velocity;
public SpriteObject(Texture2D texture, Vector2 position)
{
this.Texture = texture;
this.Position = position;
this.Velocity = Vector2.Zero;
}
public SpriteObject(Texture2D texture, Vector2 position, Vector2 velocity)
{
this.Texture = texture;
this.Position = position;
this.Velocity = velocity;
}
public Rectangle BoundingBox
{
get
{
return new Rectangle(
(int)Position.X,
(int)Position.Y,
Texture.Width,
Texture.Height);
}
}
public void Move(SpriteObject sprite)
{
this.Position += this.Velocity;
}
public void BounceX(SpriteObject sprite)
{
this.Velocity.X *= -1;
}
public void BounceY(SpriteObject sprite)
{
this.Velocity.Y *= -1;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, Position, Color.White);
}
}
}
我正在尝试在下面的代码中使用这个 class(这是不整洁的混蛋,但是 运行,我正在尝试清理的代码和新的代码class 我正在尝试这样做)。请特别注意标有 A> 和 B> 的行。
namespace MyFirstGame
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics; // All default to private
KeyboardState oldKeyState;
SpriteBatch spriteBatch;
Boolean gameOn=true;
A> SpriteObject ballSprite, starSprite; // A couple of sprites...
Texture2D texStar, texBall; // This is a texture we can render.
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 900-32; // Force window size or it won't work...
graphics.PreferredBackBufferWidth = 1600; // Force window size
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
}
/// <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
oldKeyState = Keyboard.GetState();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
// Set the coordinates to draw the sprite at.
Vector2 starPosition = Vector2.Zero;
Vector2 ballPosition = new Vector2(200, 0);
// Store some information about the sprite's motion.
Vector2 starSpeed = new Vector2(10.0f, 10.0f);
Vector2 ballSpeed = new Vector2(10.0f, 0.0f);
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
texStar = Content.Load<Texture2D>("star");
texBall = Content.Load<Texture2D>("ball");
B> ballSprite = new SpriteObject(texBall, Vector2(200, 0), Vector2(10.0f, 0.0f)); // create two sprites
B> starSprite = new SpriteObject(texStar, Vector2.Zero, Vector2(10.0f, 10.0f));
}
/// <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)
{
// Move the sprite by speed.
KeyboardState newKeyState = Keyboard.GetState();
if (newKeyState.IsKeyDown(Keys.Escape))
Exit();
if (collisionDetected()) gameOn = false; // If there's a collision then no movement.
//Could do with putting this in a seperate method or class, if there isn't one already...
if (gameOn)
{
if (newKeyState.IsKeyDown(Keys.PageUp) && oldKeyState.IsKeyUp(Keys.PageUp))
{
starSpeed.X *= 1.1f;
starSpeed.Y *= 1.1f;
}
else if (newKeyState.IsKeyDown(Keys.PageDown) && oldKeyState.IsKeyUp(Keys.PageDown))
{
starSpeed.X *= 0.9f;
starSpeed.Y *= 0.9f;
}
else if (newKeyState.IsKeyDown(Keys.Up) && oldKeyState.IsKeyUp(Keys.Up))
{
starSpeed.Y += 1.0f;
}
else if (newKeyState.IsKeyDown(Keys.Down) && oldKeyState.IsKeyUp(Keys.Down))
{
starSpeed.Y -= 1.0f;
}
else if (newKeyState.IsKeyDown(Keys.Left) && oldKeyState.IsKeyUp(Keys.Left))
{
starSpeed.X -= 1.0f;
}
else if (newKeyState.IsKeyDown(Keys.Right) && oldKeyState.IsKeyUp(Keys.Right))
{
starSpeed.X += 1.0f;
}
oldKeyState = newKeyState;
starPosition += starSpeed;
ballPosition += ballSpeed;
int MaxX =
graphics.GraphicsDevice.Viewport.Width - texStar.Width;
int MinX = 0;
int MaxY =
graphics.GraphicsDevice.Viewport.Height - texStar.Height;
int MinY = 0;
// Check for ball bounce
if (ballPosition.X > MaxX)
{
ballPosition.X = MaxX;
ballSpeed.X *= -1;
}
if (ballPosition.X < MinX)
{
ballPosition.X = MinX;
ballSpeed.X *= -1;
}
if (ballPosition.Y > MaxY)
{
ballSpeed.Y *= -1;
ballPosition.Y = MaxY;
}
ballSpeed.Y += 1;
// Check for bounce.
if (starPosition.X > MaxX)
{
starSpeed.X *= -1;
starPosition.X = MaxX;
}
else if (starPosition.X < MinX)
{
starSpeed.X *= -1;
starPosition.X = MinX;
}
if (starPosition.Y > MaxY)
{
starSpeed.Y *= -1;
starPosition.Y = MaxY;
}
else if (starPosition.Y < MinY)
{
starSpeed.Y *= -1;
starPosition.Y = MinY;
}
}
else
{
starSpeed=Vector2.Zero;
ballSpeed=Vector2.Zero;
}
}
private Boolean collisionDetected()
{
if (((Math.Abs(starPosition.X - ballPosition.X) < texStar.Width) && (Math.Abs(starPosition.Y - ballPosition.Y) < texBall.Height)))
{
return true;
}
else
{
return false;
}
}
/// <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)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// Draw the sprite.
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(texStar, starPosition, Color.White);
spriteBatch.Draw(texBall, ballPosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
A> 标记新 SpriteObject ballSprite, starSprite;
声明没有任何问题。
B> 标记在我尝试使用 SpriteObject 构造函数创建并为我之前声明的 SpriteObjects 赋值时出现的错误 - 根据标题的错误post。第一行有两个,第二行有一个。
名称空间暗示了我的 C# 经验水平。如果您能帮助我理解我几乎可以肯定的基本错误,我将不胜感激。
您指出的那两行代码缺少 'new' operator,这是实例化 Vector2
(或任何)class.
ballSprite = new SpriteObject(texBall, new Vector2(200, 0), new Vector2(10.0f, 0.0f));
starSprite = new SpriteObject(texStar, Vector2.Zero, new Vector2(10.0f, 10.0f));