locked
bouncing ball around screen RRS feed

  • Question

  •  I had a question on this and cant find it again, but if i remember it didnt solve the problem.

    I want to have a  bouncing ball around the screen so when it hits the side of the silverlight form it rebounds using vb.net. In silverlight how do I change direction with movement? Like a simple screen saver program and the ball never stops.

    i cant seem to see how i can change direction easy with this code? 

          Storyboard.SetTargetProperty(xa, "(Canvas.Left)") ';   // set Animation TargetProperty
            Storyboard.SetTargetProperty(ya, "(Canvas.Top)")
            xa.To = 600 '   // shrink to 0      
            ya.To = 600 '   // shrink to 0      
            xa.From = 0
            ya.From = 0

     

    Friday, June 6, 2008 8:49 PM

Answers

  • Hello, try something like this:

    <UserControl.Resources>

    <Storyboard x:Name="sb" Completed="sb_Completed">

    <DoubleAnimation x:Name="dax" Storyboard.TargetName="ball" Storyboard.TargetProperty="(Canvas.Left)"/>

    <DoubleAnimation x:Name="day" Storyboard.TargetName="ball" Storyboard.TargetProperty="(Canvas.Top)"/>

    </Storyboard>

    </UserControl.Resources>

     

    <Canvas x:Name="LayoutRoot" Background="Red" Width="400" Height="300">

    <Ellipse x:Name="ball" Width="10" Height="10" Fill="Blue"/>

    </Canvas>

     

    Private ran As New Random

    'Which side the ball will hit.

    Private side As Integer = 1Public Sub New()

    InitializeComponent()

    End Sub

    Private Sub UserControl_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    dax.To = 390.0

    day.To = ran.NextDouble() * 290

    sb.Begin()

    End Sub

    Private Sub sb_Completed(ByVal sender As System.Object, ByVal e As System.EventArgs)Dim i As Integer = 0

    i = 2

    Select Case side

    Case 1

    side = 2

    dax.To = 390

    day.To = ran.NextDouble() * 290

    Case 2

    side = 3

    dax.To = ran.NextDouble() * 390

    day.To = 290

    Case 3

    side = 4

    dax.To = 0

    day.To = ran.NextDouble() * 290

    Case 4

    side = 1

    dax.To = ran.NextDouble() * 390

    day.To = 0

    End Select

    sb.Begin()

    End Sub

     

    Monday, June 9, 2008 10:10 AM

All replies

  •  what i wanted was a .NETsolution of a ball in constant movement bouncing off the sides of the screen, like the screen saver. NOT a ball being dropped and bouncing a few times until it stops like a basket ball.

    Friday, June 6, 2008 9:05 PM
  •  you mean like this one?

    http://metalinkltd.com/?p=93

    Friday, June 6, 2008 10:10 PM
  •  I cant run the source code as their is errors and i havent got all day to try to fix it.

    OK it is bouncing so fast i cant see if this is what i want. I can see some code and i cant see any doubleanimation variables and i cant see how it draws the ball to the screen at new location. 

     

    I am going to need a little more help. 

    Friday, June 6, 2008 11:55 PM
  •  I don't know vb.net, but I can give you a rundown of what you will need to do.

    First you need a ball. I would make a struct and assign properties like x, y, movex, and movey. It would also be easier to have a width and height property. X and Y will be the current coords and movex and movey will be what to increment by.

    you will need a function that is called every 100 milliseconds or so. within that function have if() statements checking whether the top, bottom, left, or right are hitting the sides of the screen. For example, lets say the ball is 10px across:

     
    if(ball.x + ball.width >= *screen_width_here*)

           ball.movex *= -1;

     When the ball his the wall, it will reverse the movex, cause it to go into the other direction, but still looking fairly realistic at the same time.

    Sorry I don't know vb.net, or I may be able to help you more.

    Good luck


     

    Friday, June 6, 2008 11:59 PM
  • I understand what you mean.

     my silverlight skills are not great so this is what I can do so far.

     

    I have used a rectangle to simple draw on every timer click and i didnt use a storyboard because I could see how to use it

    If I find the end of screen i simply draw the other way no problem

    q1) do i need a storyboard as many examples still find a use for one?


     

      Private Sub dispatcherTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            t += 10
            MainGameLoop(t)

        End Sub

       Private Sub MainGameLoop(ByVal t As Integer)

            Dim brush As New SolidColorBrush
            brush.Color = Colors.Black

            Dim bb As New Rectangle
            bb.Width = 100
            bb.Height = 100
            bb.Fill = brush


            Me.canvas1.Children.Clear()


           
            Canvas.SetTop(bb, 100 + t)
            Canvas.SetLeft(bb, 100 + t)
            canvas1.Children.Add(bb)

    Saturday, June 7, 2008 12:48 AM
  • I do not know vb.net so most of that means nothing to me. If you understand C# I could right up a quick app. 

    Saturday, June 7, 2008 1:31 AM
  • Hi,

    c# is ok as i can usually understand that. I know the logic and i can do this in vb.net winforms.

    I did have questions on it which i stated in last post. 

    I did another post on the topic so enough said here.  

    this is what i have and it works. 

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

            If (t > 400) Then
                tt *= -1
            ElseIf (t < 0) Then
                tt *= -1
            End If
            t = t + 7 * tt
            MainGameLoop(t)

        End Sub

     

    q)the animation doesnt seem that smooth with drawing every 50ms  drawing only 7 pixels from last point

    Saturday, June 7, 2008 3:33 AM
  • check out bubblemark.com

    Saturday, June 7, 2008 4:27 AM
  •  

    there is a error with the link provided.

     

     q)the animation doesnt seem that smooth with drawing every 50ms  drawing only 7 pixels from last point?

    Saturday, June 7, 2008 5:14 AM
  • Hello, try something like this:

    <UserControl.Resources>

    <Storyboard x:Name="sb" Completed="sb_Completed">

    <DoubleAnimation x:Name="dax" Storyboard.TargetName="ball" Storyboard.TargetProperty="(Canvas.Left)"/>

    <DoubleAnimation x:Name="day" Storyboard.TargetName="ball" Storyboard.TargetProperty="(Canvas.Top)"/>

    </Storyboard>

    </UserControl.Resources>

     

    <Canvas x:Name="LayoutRoot" Background="Red" Width="400" Height="300">

    <Ellipse x:Name="ball" Width="10" Height="10" Fill="Blue"/>

    </Canvas>

     

    Private ran As New Random

    'Which side the ball will hit.

    Private side As Integer = 1Public Sub New()

    InitializeComponent()

    End Sub

    Private Sub UserControl_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    dax.To = 390.0

    day.To = ran.NextDouble() * 290

    sb.Begin()

    End Sub

    Private Sub sb_Completed(ByVal sender As System.Object, ByVal e As System.EventArgs)Dim i As Integer = 0

    i = 2

    Select Case side

    Case 1

    side = 2

    dax.To = 390

    day.To = ran.NextDouble() * 290

    Case 2

    side = 3

    dax.To = ran.NextDouble() * 390

    day.To = 290

    Case 3

    side = 4

    dax.To = 0

    day.To = ran.NextDouble() * 290

    Case 4

    side = 1

    dax.To = ran.NextDouble() * 390

    day.To = 0

    End Select

    sb.Begin()

    End Sub

     

    Monday, June 9, 2008 10:10 AM