' Silverlight Speed Tutorial VB source-code

' Written by John Bower, Deep Space Objects

' =========================================

 

Partial Public Class Page

    Inherits Canvas

 

    Dim imgSprite As New Image      ' Create new Image

 

    Dim txtFPS As New TextBlock   ' Create new TextBlock

 

    Dim cParent As Canvas        ' Create cParent

 

    Dim sbLoop As New Storyboard

 

    Dim intX, intY As Integer

    Dim movingLeft, movingUp As Boolean

 

    Dim FrameRate, LastTime As Integer

 

 

    ' Page_Loaded - Called when the canvas is loaded

    ' ================================================

 

    Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)

        ' Required to initialize variables

        InitializeComponent()

 

        ' set rootCanvas to the canvas that called this function

        cParent = FindName("parentCanvas")

 

        ' Create Image

        attachImage()

 

        ' Create FPS TextBlock

        attachFPSText()

 

        ' Create Storyboard

        attachStoryboard()

 

    End Sub

 

 

    ' Image attachment - Adds an Image object ot the canvas

    ' =====================================================

 

    Private Sub attachImage()

 

        ' Set up the new Image object

        imgSprite.Source = New Uri("images/car.jpg", UriKind.Relative)

 

        ' Add the Image to the Parent Canvas

        cParent.Children.Add(imgSprite)

 

    End Sub

 

 

    ' FPS attachment - Adds FPS textblock to the canvas

    ' =================================================

 

    Private Sub attachFPSText()

 

        ' add textblock to canvas

        cParent.Children.Add(txtFPS)

 

    End Sub

 

 

    ' Storyboard attachment - Adds the storyboard to the Canvas

    ' =========================================================

 

    Private Sub attachStoryboard()

 

        ' set up the new Storyboard

        sbLoop.SetValue(Storyboard.NameProperty, "loop")

 

        ' Add the Storyboard to the Canvas

        cParent.Resources.Add(sbLoop)

 

        AddHandler sbLoop.Completed, AddressOf loop_Completed

 

        ' Start playing the Storyboard loop

        sbLoop.Begin()

 

    End Sub

 

 

    ' Loop function - Handles storyboard.completed event

    ' ==================================================

 

    Private Sub loop_Completed(ByVal sender As Object, ByVal e As EventArgs)

 

        ' Update frame rate

        updateFPS()

 

        ' Y-Axis logic

        If movingUp Then

 

            intY -= 1 ' update Y

 

            If intY < 0 Then ' bounce

                movingUp = Not movingUp

            End If

 

        Else

 

            intY += 1 ' update Y

 

            If intY > (cParent.Height - imgSprite.Height) Then ' bounce

                movingUp = Not movingUp

            End If

 

        End If

 

        ' X-Axis logic

        If movingLeft Then

 

            intX -= 2 ' update X

 

            If intX < 0 Then ' bounce

                movingLeft = Not movingLeft

            End If

 

        Else

 

            intX += 2 ' update X

 

            If intX > (cParent.Width - imgSprite.Width) Then ' bounce

                movingLeft = Not movingLeft

            End If

 

        End If

 

        ' Update the Image object's position

        imgSprite.SetValue(Canvas.LeftProperty, intX)

        imgSprite.SetValue(Canvas.TopProperty, intY)

 

        ' Restart the Storyboard

        sbLoop.Begin()

 

    End Sub

 

 

    ' Frames per second - Updates FPS variables

    ' =========================================

 

    Private Sub updateFPS()

 

        If LastTime > Date.Now.Millisecond Then

 

            txtFPS.Text = "FPS: " & FrameRate

 

            FrameRate = 0

 

            LastTime = Date.Now.Millisecond

 

        Else

 

            LastTime = Date.Now.Millisecond

 

            FrameRate += 1

 

        End If

 

    End Sub

 

 

End Class