我已经为我正在工作的一些软件开发了一个动画,它可以动态地将两个对象倾斜到它们各自的边界,然后一旦被绊倒就会再次返回。我遇到的问题是应该跳闸的脚本没有被执行,我无法弄清楚为什么。
请注意:_fg 和 _bg 值在类的顶部定义。
public static void Grass(Canvas canvas, int boundry)
{
Random random = new Random();
foreach (FrameworkElement element in canvas.Children)
{
var elementName = Regex.Split(element.Name, "_");
if (elementName[0] == "grass")
{
switch (elementName[1])
{
case "foreground":
_fgGrassDrn = (_fgGrassPos == 0 && _fgGrassHbd == 0) ? random.Next(0, 2) : _fgGrassDrn;
_fgGrassPos = (_fgGrassDrn == 0) ? _fgGrassPos + 1 : _fgGrassPos - 1;
_fgGrassBnd = (_fgGrassDrn == 0) ? boundry : -boundry;
if (_fgGrassBnd == boundry) // this is the tripwire code.
{
_fgGrassHbd = 1;
_fgGrassDrn = (_fgGrassDrn == 0) ? 1 : 0;
}
element.RenderTransform = new SkewTransform(_fgGrassPos, 0);
break;
case "background":
_bgGrassDrn = (_bgGrassPos == 0 && _bgGrassHbd == 0) ? random.Next(0, 2) : _bgGrassDrn;
_bgGrassPos = (_bgGrassDrn == 0) ? _bgGrassPos + 1 : _bgGrassPos - 1;
_bgGrassBnd = (_bgGrassDrn == 0) ? boundry : -boundry;
if (_bgGrassBnd == boundry) // this is the tripwire code.
{
_bgGrassHbd = 1;
_bgGrassDrn = (_bgGrassDrn == 0) ? 1 : 0;
}
element.RenderTransform = new SkewTransform(_bgGrassPos, 0);
break;
}
}
}
}
我有一种感觉,我错过了一些非常明显的东西,但我只用 c# 编程了大约 5 天,所以请原谅 :)