gpt4 book ai didi

c# - 表达式表示 `type' ,其中应为 `variable' 、 `value' 或 `method group' (CS0119) (MyFirstGame)

转载 作者:太空宇宙 更新时间:2023-11-03 21:23:44 25 4
gpt4 key购买 nike

我正在学习使用 modevelop 和 monogame 在 C# 中编程。我对 C 和 Java 相当熟悉,但老实说,我仍在学习像面向对象的程序员一样思考。我想使用下面的类来收集 Sprite 的相关属性,以及移动、绘制、加速、检测碰撞等方法来整理游戏代码。此类出现在文件 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);
}
}
}

我正在尝试在下面的代码中使用此类(这是我正在尝试清理的不整洁但正在运行的代码和我正在尝试使用的新类的混合体) .请特别注意标有 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 赋值时出现的错误 - 错误如帖子标题所示。第一行有两个,第二行有一个。

namespace 暗示了我的 C# 经验水平。如果您能帮助我理解我几乎可以肯定的基本错误,我将不胜感激。

最佳答案

您指出的那两行代码缺少 'new' operator ,这是实例化 Vector2(或任何)类所必需的。

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

关于c# - 表达式表示 `type' ,其中应为 `variable' 、 `value' 或 `method group' (CS0119) (MyFirstGame),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28709799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com