我一直在查看我使用的应用程序的一些示例源代码,我遇到了这一行:
for (;;)
{
// The rest of the application's code
}
Run Code Online (Sandbox Code Playgroud)
看起来这是创建一个无限循环,但我不熟悉";;" 不幸的是谷歌很难.
创建 2D Pong Clone 时,我试图让球从 UI 的顶部和底部“墙”反弹。这是我的 Game.cs
public void CheckBallPosition()
{
if (ball.Position.Y == 0 || ball.Position.Y >= graphics.PreferredBackBufferHeight)
ball.Move(true);
else
ball.Move(false);
if (ball.Position.X < 0 || ball.Position.X >= graphics.PreferredBackBufferWidth)
ball.Reset();
}
Run Code Online (Sandbox Code Playgroud)
目前我在 Ball.cs 中使用它
public void Move(bool IsCollidingWithWall)
{
if (IsCollidingWithWall)
{
Vector2 normal = new Vector2(0, 1);
Direction = Vector2.Reflect(Direction,normal);
this.Position += Direction;
Console.WriteLine("WALL COLLISION");
}
else
this.Position += Direction;
}
Run Code Online (Sandbox Code Playgroud)
它有效,但我使用的是手动输入的法线,我想知道如何计算屏幕顶部和底部的法线?
我想象的简单问题,但这些代码行之间有什么区别:
代码1
public int Temp { get; set; }
Run Code Online (Sandbox Code Playgroud)
和
代码2
private int temp;
public int Temp { get { return temp; } }
Run Code Online (Sandbox Code Playgroud)
我的理解是,根据代码1的自动属性将执行与代码2完全相同的功能?
我正在阅读Head First C#,我发现很难理解为什么它使用两种不同的方式做同样的事情?
我有两个课程,人类和怪物.
两者都有一个名为MoveBehavior的属性
Human有HumanMoveBehavior,而Monster有MonsterMoveBehavior
我希望HumanMoveBehavior从怪兽身上移动,并让MonsterMoveBehavior移动TOWARD Humans.
我遇到的问题是我应该在哪里移动代码?
在人类/怪物类中?
使用这种方法,我有一个Move()方法,它接受游戏中所有实体的List,使用名为GetListOfOpponents(List allsprites)的方法决定它是Monster还是Human,然后运行GetNearestOpponent(List opponents);
但这看起来非常混乱.
我应该有一个SpriteController来决定Sprite移动的位置吗?我不确定我需要把这段代码放在哪里:(
谢谢!