5 Replies - 12623 Views - Last Post: 04 December 2008 - 01:03 PM Rate Topic: -----

#1 Steeeve  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 03-December 08

Moving image across the screen

Posted 03 December 2008 - 10:43 AM

I'm having trouble with my final project for my VB class. We have to use a splash screen, where the picture starts at the bottom left and moves up to the top right, and disable the timer when it gets to the top right. Figures the only class I missed all semester was this one!

Here's what I've managed to piece together using my textbook. The problem is that I have to move it diagonally across, but the code I have right now moves it across the bottom, then moves it up when it reaches the right side.

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Dim X As Decimal
		Dim Y As Decimal

		For X = 0 To 544 Step 0.0001
			Me.picSanta.SetBounds(X, 0, 0, 0, BoundsSpecified.X)
			Me.picSanta.SetBounds(0, Y, 0, 0, BoundsSpecified.Y)
		Next X

		For Y = 316 To 0 Step -0.0001
			Me.picSanta.SetBounds(X, 0, 0, 0, BoundsSpecified.X)
			Me.picSanta.SetBounds(0, Y, 0, 0, BoundsSpecified.Y)
		Next Y

		If X >= 500 Then
			Timer1.Enabled = False
			Timer2.Enabled = True
		End If

	End Sub

This post has been edited by Steeeve: 03 December 2008 - 10:45 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Moving image across the screen

#2 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Moving image across the screen

Posted 03 December 2008 - 03:05 PM

Everytime the timer ticks you want to move the picture, in your code you are trying to do the entire animation in the tick. I thought it would be fun to try so wrote this code to give you an idea.

Public Class FormSplash
    Private AnimationCurrentFrame As Integer

    Private Sub TimerAnimate_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerAnimate.Tick
        Dim AnimationFrames As Integer = Math.Max(Me.ClientSize.Width, Me.ClientSize.Height) 'Find the greater of width and height to determine the number of frames required
        Dim AnimationTime As Integer = 5  'Time in seconds to complete the animation
        Dim AnimationStep As Integer = AnimationFrames / (AnimationTime * (1000 / TimerAnimate.Interval)) 'Calculate how many frames we have to increment by to make the animation complete within the AnimationTime
        Dim AnimationY As Integer = Me.ClientSize.Height - (AnimationCurrentFrame * (Me.ClientSize.Height / AnimationFrames)) 'Calculate the Y position starting at bottom
        Dim AnimationX As Integer = AnimationCurrentFrame * (AnimationFrames / Me.ClientSize.Width) 'Calculate the X position starting at left

        PictureBoxAnimation.Location = New Point(AnimationX, AnimationY) 'Move the picturebox
        If AnimationCurrentFrame > AnimationFrames Then TimerAnimate.Stop() 'Stop the timer once we have completed the animation
        AnimationCurrentFrame += AnimationStep 'Increment the current frame
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

#3 Steeeve  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 03-December 08

Re: Moving image across the screen

Posted 04 December 2008 - 08:32 AM

Thanks for the help, but I should have mentioned that it needs to be in the For... Next format, and I think I have to use the SetBounds statement as well. I just need to fix what I have rather than do something completely new. I apologize for not being more clear.

I think I understand the timer part of it, but I'm still questionable with the For...Next and the SetBounds parts. Another problem is that the form is a rectangle and not a square (700 x 500), so I need a way to move it further to the right for every time it moves up.

What I have now moves it from the top left to the bottom right instead of the bottom left to the top right. Is there some way to set the Y starting value to 0 in the same statement?

For intX = 0 To 544 Step 0.0001
			Me.picSanta.SetBounds(intX, 0.1, 0, 0, BoundsSpecified.X)
			Me.picSanta.SetBounds(0.1, intX, 0, 0, BoundsSpecified.Y)
		Next intX

This post has been edited by Steeeve: 04 December 2008 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Moving image across the screen

Posted 04 December 2008 - 08:54 AM

Hmm then why even use a timer? Something doesn't add up. If you don't use the timer then the animation is going to suck up 100% cpu and run extremly fast and unless you add application.doevents in the loop you won't even see the animation.

If you think you have to do it in a loop then you can extract most of my logic from above, however it is a waste of resources to do it this way as you have to slow the loop down with the step to see the animation. For the record my original code is by far the better solution.

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

        Dim maxWidthOrHeight As Integer = Math.Max(Me.ClientSize.Width, Me.ClientSize.Height)
        Dim PositionY As Integer
        Dim PositionX As Integer

        For intLoop = 0 To Me.ClientSize.Width Step 0.0001
            PositionY = Me.ClientSize.Height - (intLoop * (Me.ClientSize.Height / maxWidthOrHeight))
            PositionX = intLoop * (maxWidthOrHeight / Me.ClientSize.Width)
            Me.picsanta.SetBounds(PositionX, PositionY, 0, 0, BoundsSpecified.X Or BoundsSpecified.Y)
            Application.DoEvents()
        Next intLoop

    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 Steeeve  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 58
  • Joined: 03-December 08

Re: Moving image across the screen

Posted 04 December 2008 - 09:29 AM

Sorry, the reason it has to be so specific is that it's a project for a class, and we have an instruction sheet to follow. The first part of it is:

1. Create a form that is 700 by 500.
2. Place provided image in the lower left corner.
3. Move the object via a timer control so that it "smoothly" moves to the upper right corner of the screen.
4. As soon as the object reaches the upper right corner, display the welcome message. Keep the message visible for 3 seconds before moving to Form 2.
Was This Post Helpful? 0
  • +
  • -

#6 magicmonkey  Icon User is offline

  • D.I.C Regular

Reputation: 106
  • View blog
  • Posts: 484
  • Joined: 12-September 08

Re: Moving image across the screen

Posted 04 December 2008 - 01:03 PM

I see move the object via a timer, not via a for next loop, I still think my original code is the correct method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1