我有一个学校的作业,我有一个动画角色。我需要能够让他跳起来并回到他按下按钮后开始跳跃的位置。我有一个计时器,可以切换图像来为角色设置动画。还有一些按钮可以加速,减慢,启动和停止角色。我遇到了麻烦,因为我认为我必须将定时器集成到按钮按钮中,我不太清楚如何做到这一点。代码底部有一个跳转按钮所在的空间。我能够让他上升,但无法弄清楚如何让他回来。
到目前为止,这是我的代码:
Private Sub tmrSpeed_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSpeed.Tick
Static Dim intCounter As Integer
picRunner.Location = New Point(picRunner.Location.X + 15, picRunner.Location.Y)
If picRunner.Location.X >= 523 Then
picRunner.Location = New Point(-25, picRunner.Location.Y)
End If
Select Case intCounter
Case 0
Me.picRunner.Image = My.Resources.r0
Case 1
Me.picRunner.Image = My.Resources.r1
Case 2
Me.picRunner.Image = My.Resources.r2
Case 3
Me.picRunner.Image = My.Resources.r3
Case 4
Me.picRunner.Image = My.Resources.r4
Case 5
Me.picRunner.Image = My.Resources.r5
Case 6
Me.picRunner.Image = My.Resources.r6
Case 7
Me.picRunner.Image = My.Resources.r7
Case 8
Me.picRunner.Image = My.Resources.r8
Case 9
Me.picRunner.Image = My.Resources.r9
End Select
intCounter += 1
If intCounter >= 10 Then
intCounter = 0
End If
End If
End Sub
Private Sub btnFaster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFaster.Click
Try
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval - 10
Catch x As Exception
MessageBox.Show("He can't run any faster!")
End Try
End Sub
Private Sub btnSlower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSlower.Click
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval + 10
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.tmrSpeed.Stop()
End Sub
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
Me.tmrSpeed.Start()
End Sub
Private Sub btnJump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJump.Click
End Sub
End Class
我如何实施跳跃?我的老师说我必须使用一个循环,显然要检查图片框何时达到某个热点,但我无法弄清楚如何在计时器中使用它。任何帮助,将不胜感激。
答案 0 :(得分:0)
设置一个计算力的循环。
也为该力量的衰变率做出变量。
让循环通过从当前y位置减去它来施加力 并且在它施加力之后减去它的衰减速率以减小力,所以下次循环发生时力越小,它开始减速。
还有一个称为“重力”的全局变量,可以向下创建对象的持续拉力,因此当向上力衰减时,它会再次向下移动。
其余你必须弄清楚,如果这是一项任务,那么你开发逻辑来做我所描述的事情是非常重要的,而不会有人给你答案。它将使您更好地理解代码。
答案 1 :(得分:0)
我的任务完全相同,因此我以下面的代码作为解决方案。我将计时器的初始间隔设置为999999,以使表单不会随着动画的跳转而加载。
Private Sub btnJump_Click(sender As Object, e As EventArgs) Handles btnJump.Click
Me.tmrJump.Interval = 100
Me.tmrJump.Start()
End Sub
Private Sub tmrJump_Tick(sender As Object, e As EventArgs) Handles tmrJump.Tick
Static intOldTop As Integer
Static intCounter As Integer
Dim intStep As Integer = 5
If intCounter = 0 Then intOldTop = picSpaceRunner.Top
intCounter += 1
Select Case intCounter
Case 1 To 15
picSpaceRunner.Top -= intStep
Case 16 To 30
picSpaceRunner.Top += intStep
Case Else
picSpaceRunner.Top = intOldTop
intCounter = 0
tmrJump.Stop()
End Select
End Sub
结束班级