我必须让 20 个球在屏幕上移动。我想知道如何使用循环,这样我就不必长时间输入代码。目前,我拥有的代码是
for (int i = 0; i < ballSpeedXAxis.Length; i++)
{
ballSpeedXAxis[i] = 1;
}
for (int i = 0; i < ballSpeedYAxis.Length; i++)
{
ballSpeedYAxis[i] = 1;
}
private void OnUpdate(object sender, object e)
{
Canvas.SetLeft(this.ball1, this.ballSpeedXAxis[1] + Canvas.GetLeft(this.ball1));
Canvas.SetTop(this.ball1, this.ballSpeedYAxis[1] + Canvas.GetTop(this.ball1));
Canvas.SetLeft(this.ball2, this.ballSpeedXAxis[2] + Canvas.GetLeft(this.ball2));
Canvas.SetTop(this.ball2, this.ballSpeedXAxis[2] + Canvas.GetTop(this.ball2));
...
Canvas.SetLeft(this.ball20, this.ballSpeedXAxis[20] + Canvas.GetLeft(this.ball20));
Canvas.SetTop(this.ball20, this.ballSpeedXAxis[20] + Canvas.GetTop(this.ball20));
}
ball1, ball2 ... ball3 是图像名称。