2016-11-24 5 views
0

私は現在、小さなアニメーションをコーディングしています。アニメーションは小さな円が画面を横切って開始され、ユーザーのクリックボタンやその他の小さな画像が画面を横切って移動します(画像の内容、プログラムの目的などは接線と無関係です)。複数の手順でタイマーティックを使用する方法はありますか?

私は現在、アニメーションをコーディングしています方法は、このようなものです:

Private Sub Timer_Tick(sender As Object, e As EventArgs) Handles Timer.Tick 
    Circle1.Left -= 10 
    If Counter = 16 Then 
     Timer.Enabled = False 
     Counter = 0 
    End If 
    Counter += 1 
End Sub 

しかし、問題は、私は複数の画像の動きをアニメーション化を助けるためにタイマーを使用する必要があるということです。複数のタイマーを作成する以外に、複数のサブルーチンでタイマーのティッキングを使用する方法はありますか?

+0

Circle1.Leftを更新する以上のものがあります。あなたがしていることは、あなた次第です。推測できません。 –

+0

あなたの 'timer_tick'に他の画像を追加するのを止めるのですか? –

答えて

0

リストを使用して、アニメートするすべてのコントロールを保持できます。タイマーイベントでリストを反復し、コントロールの位置を変更します。これを行う方法の例を次に示します。

Public Class Form1 

    ' this list holds all controls to animate 
    Private controlsToAnimate As New List(Of Control) 

    Private random As New Random 

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick 

     ' this list holds all controls to remove 
     Dim controlsToRemove As New List(Of Control) 

     For i = 0 To controlsToAnimate.Count - 1 
      Dim control = controlsToAnimate(i) 
      If control.Left < 0 Then 
       ' this control has reached the left edge, so put it in the list to remove 
       controlsToRemove.Add(control) 
      Else 
       ' move the control to left 
       control.Left -= 10 
      End If 
     Next 

     ' remove all controls that have reached the left edge 
     For Each control In controlsToRemove 
      controlsToAnimate.Remove(control) 
      control.Dispose() 
     Next 

     ' for debug only, display the number of controls to animate 
     Me.Text = controlsToAnimate.Count() 

    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 

     ' create a new picturebox 
     Dim newControl As New PictureBox 
     With newControl 
      ' load an image for the picturebox 
      .Image = Image.FromFile("D:\Pictures\abc.jpg") 

      ' size of the picturebox 
      .Size = New Size(100, 100) 

      ' picturebox appears at the right edge of the form, 
      ' the vertical position is randomize 
      .Location = New Point(Me.Width - newControl.Width, random.Next(Me.Height - newControl.Height)) 

      ' stretch the image to fit the picturebox 
      .SizeMode = PictureBoxSizeMode.StretchImage 
     End With 

     ' add the newly created control to list of controls to animate 
     controlsToAnimate.Add(newControl) 

     ' add the newly created control to the form 
     Me.Controls.Add(newControl) 

    End Sub 

End Class 
関連する問題