任何人都可以解释我以下代码吗? [英] Can anyone explain me the following code?

查看:75
本文介绍了任何人都可以解释我以下代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是vb的初学者,我是一个自学者,所以基本上互联网和书籍是我学习vb的唯一方式。我正在浏览youtube中的这个教程,它基本上展示了如何准备一只鸟飞的动画。不幸的是,这个人根本没有解释代码,所以我想了解代码,然后去做。所以,请任何人都可以通过声明向我解释代码声明。



另外,两个语句之间有什么区别(Dim x as integer = 5 and static x as integer = 5)。提前致谢

 静态 x 正如 整数 =  5  
静态 y 作为 整数 = 0
静态 flip 作为 整数 = 错误

如果 y Mod 2 = 0 然后
.PictureBox1.Image = My.Re。 sources.bird_flight_3_big
否则
.PictureBox1.Image = My.Resources。 bird_flight_4_big
结束 如果

如果 .PictureBox1.Left> = 500 然后
x = -5
flip = True
ElseIf .PictureBox1.Left< = 0 然后
x = 5
flip = False
结束 如果

如果 flip = True 那么
Me .PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
End 如果

.PictureBox1.Left = .PictureBox1.Left + x
y = y + 1

解决方案

Mod为你提供除数时的余数:所以17 Mod 10是17/10的余数,即7。你得到的值总是在零和(除数减去1)之间:X Mod 10将永远在0到9之间。

所以... y Mod 2总是0或1,具体取决于y是奇数还是偶数。

如果y是奇数,它会显示bird_flight_4_big,如果它甚至显示为bird_flight_3_big - 并且因为每次执行代码时都添加一个y,它将在它们之间切换。可能这两张照片分别显示了上下的翅膀,因此每次执行代码时,翅膀都会改变位置。



所有剩下的代码都在做正在检查图片框是否已经到达它的左边缘或右边缘,如果是,则将其设置为移动另一个方向。最后一条线实际上将图片框向适当的方向移动。


对于OriginalGriff的回答,



静态 Dim 不同,因为它会在下次执行代码时保存对该var的更改。使用Dim,每次Code运行时x都为'5'。



看看 [ ^ ]

I am a beginner in vb and i am a self learner, so basically internet and books are my only way of learning vb. I was looking through this tutorial in youtube which basically showed how to prepare a animation of a bird flying. Unfortunately the person did not explain the code at all, so i wanted to understand the code and then do it. So, please can anyone explain me the code statement by statement.

Also, what is the difference between the two statement ("Dim x as integer = 5 and static x as integer = 5). Thanks in advance

Static x As Integer = 5
Static y As Integer = 0
Static flip As Integer = False

If y Mod 2 = 0 Then
  Me.PictureBox1.Image = My.Resources.bird_flight_3_big
Else
  Me.PictureBox1.Image = My.Resources.bird_flight_4_big
End If

If Me.PictureBox1.Left >= 500 Then
  x = -5
  flip = True
ElseIf Me.PictureBox1.Left <= 0 Then
  x = 5
  flip = False
End If

If flip = True Then
  Me.PictureBox1.Image.RotateFlip(RotateFlipType.RotateNoneFlipX)
End If

Me.PictureBox1.Left = Me.PictureBox1.Left + x
y = y + 1

解决方案

Mod gives you the remainder when you divide: so 17 Mod 10 is the remainder of 17 / 10, which is 7. The value you get will always be between zero and (the divisor minus 1): X Mod 10 will always be between zero and nine.
So... y Mod 2 will always be 0 or 1, depending on whether y is odd or even.
If y is odd, it displays bird_flight_4_big, if it's even is displays bird_flight_3_big - and because you add one to y each time the code is executed, it will toggle between them. Probably, the two pictures show the wings up and down respectively, so each time you execute the code, the wings change position.

All the rest of the code is doing is checking if the picture box has reached it's left or right edges, and if so setting it up to move the other way. The final-but-one line actually moves the picturebox in the appropriate direction.


In adition to OriginalGriff's answer,

Static differs from Dim as it saves changes to that var for the next time that Code executes. Using Dim, x would be '5' everytime that Code runs.

have a look[^]


这篇关于任何人都可以解释我以下代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆