Nic*_*ner 1 c# foreach xna loops
我试图通过将其包装在循环中来压缩它:
if (pos.X + pixelRadius < 0) {
pos.X = bounds.Width - pixelRadius;
} else if (pos.X + pixelRadius > bounds.Width) {
pos.X = pixelRadius;
}
if (pos.Y + pixelRadius < 0) {
pos.Y = bounds.Heigh - pixelRadius;
} else if (pos.Y + pixelRadius > bounds.Height) {
pos.Y = pixelRadius;
}
Run Code Online (Sandbox Code Playgroud)
我的第一直觉是做这样的事情:
foreach (float coord in new float[] { pos.X, pos.Y }) {
float upperBound = (coord == pos.X ? bounds.Width : bounds.Height);
if (coord + pixelRadius < 0) {
coord = upperBound - pixelRadius;
} else if (coord + pixelRadius > upperBound) {
coord = pixelRadius;
}
}
Run Code Online (Sandbox Code Playgroud)
但当然,我收到错误消息:
Cannot assign to 'coord' because it is a 'foreach iteration variable'
Run Code Online (Sandbox Code Playgroud)
有什么办法可以将这段代码包装在循环中吗?或者也许这不值得付出努力,而且留下第一种形式更具可读性.
对于那些好奇的人:是的,这是实现环绕.
归档时间: |
|
查看次数: |
2078 次 |
最近记录: |