Results 1 to 11 of 11

Thread: I can't keep my balls from flickering

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Talking I can't keep my balls from flickering

    I have made this little bouncing ball app that basically moves a circle shape around the screen from inside a Timer_Tick event. The problem is that the ball "flickers" when it moves. What's the best way to stop that? If you want code, I can post it, just ask
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  2. #2
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286

    Re: I can't keep my balls from flickering

    Is the ball an image or PictureBox?

    If so, it might help to actually draw it on to the parent window instead, via PaintPicture or the BitBlt API (so in essence -- using a backbuffer).

  3. #3

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: I can't keep my balls from flickering

    it is a shape control loaded using

    VB Code:
    1. strShapeName = "NewShape" & CStr(i)
    2.         Me.Controls.Add "VB.Shape", strShapeName
    3.         Set bBall(i).Bouncer = Me.Controls(strShapeName)
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4
    Software Eng. Megatron's Avatar
    Join Date
    Mar 1999
    Location
    Canada
    Posts
    11,286

    Re: I can't keep my balls from flickering

    And you're just changing the Left and Top properties in a Timer?

    What's the Timer's interval?

    Have you tried the Ellipse method (of the Form) instead?

  5. #5

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: I can't keep my balls from flickering

    Quote Originally Posted by Megatron
    And you're just changing the Left and Top properties in a Timer?

    What's the Timer's interval?

    Have you tried the Ellipse method (of the Form) instead?
    the interval can be shorter than the time required by the code (<15ms) or bigger. I turn off the timer at the beginning of the Tick event the turn it off at the end.

    edit: check that, its not the time required by code, its the resolution of the timer

    I don't know what the Elliplse method is...can you briefly elaborate?
    kevin
    Last edited by kebo; May 30th, 2005 at 11:34 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: I can't keep my balls from flickering

    bump
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: I can't keep my balls from flickering

    Try setting the forms AutoRedraw property to true but I dont think it will help...

    The fact is the shape control isnt designed to be moved a lot because it is slow, your best bet if you want to stop the flickering is too have a look as mentioned above at BitBlt or DirectX for drawing.

    Bitblt Link in my sig and also try the Games and GFX forum for links to more guides!

    Hope it Helps

  8. #8
    Addicted Member PeteD's Avatar
    Join Date
    Jun 2003
    Location
    Sydney
    Posts
    158

    Re: I can't keep my balls from flickering

    VB Code:
    1. Imports System.Windows.Forms
    2. Imports System.Drawing
    3.  
    4.  
    5. Namespace Utilities
    6.  
    7.  
    8.  
    9.     Public Class Win32
    10.         Public Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC As Integer, ByVal x As Integer, _
    11.             ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hSrcDC As Integer, _
    12.               ByVal xSrc As Integer, ByVal ySrc As Integer, _
    13.               ByVal dwRop As Integer) As Integer
    14.         Public Declare Function GetWindowDC Lib "user32" Alias "GetWindowDC" (ByVal hwnd As Integer) As Integer
    15.         Public Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal hwnd As Integer, ByVal hdc As Integer) As Integer
    16.         Public Const SRCCOPY As Integer = &HCC0020
    17.  
    18.     End Class
    19.  
    20.    
    21.  
    22.     Public Class ControlFunctions
    23.  
    24.         Public Shared Function CreateBitmap(ByVal Control As Control) As Bitmap
    25.             If Control.Width > 0 AndAlso Control.Height > 0 Then
    26.                 Dim gDest As Graphics
    27.                 Dim hdcDest As IntPtr
    28.                 Dim hdcSrc As Integer
    29.                 Dim hWnd As Integer = Control.Handle.ToInt32
    30.                 CreateBitmap = New Bitmap(Control.Width, Control.Height)
    31.                 gDest = gDest.FromImage(CreateBitmap)
    32.                 hdcSrc = Win32.GetWindowDC(hWnd)
    33.                 hdcDest = gDest.GetHdc
    34.                 Win32.BitBlt(hdcDest.ToInt32, 0, 0, Control.Width, Control.Height, hdcSrc, 0, 0, Win32.SRCCOPY)
    35.                 gDest.ReleaseHdc(hdcDest)
    36.                 Win32.ReleaseDC(hWnd, hdcSrc)
    37.             End If
    38.         End Function
    39.  
    40.     End Class
    41.  
    42.  
    43.     Public Class UpdateFlashPreventer
    44.         Sub New()
    45.  
    46.         End Sub
    47.  
    48.         Sub New(ByVal controlToUpdate As Control)
    49.             _control = controlToUpdate
    50.         End Sub
    51.  
    52.         Private _control As Control
    53.         Private pic As PictureBox
    54.         Public Property Control() As Control
    55.             Get
    56.                 Return _control
    57.             End Get
    58.             Set(ByVal Value As Control)
    59.                 _control = Value
    60.             End Set
    61.         End Property
    62.  
    63.         Private _inUpdate As Boolean
    64.         Public ReadOnly Property InUpdate() As Boolean
    65.             Get
    66.                 Return _inUpdate
    67.             End Get
    68.         End Property
    69.  
    70.         Public Sub BeginUpdate()
    71.             If Not _control Is Nothing Then
    72.                 If Not _control.Parent Is Nothing Then
    73.                     _inUpdate = True
    74.                     pic = New PictureBox
    75.                     pic.Size = _control.Size
    76.                     pic.Image = ControlFunctions.CreateBitmap(_control)
    77.                     pic.Location = _control.Location
    78.                     pic.Anchor = _control.Anchor
    79.                     pic.Dock = _control.Dock
    80.                     _control.Parent.Controls.Add(pic)
    81.                     pic.BringToFront()
    82.                     pic.Refresh()
    83.                 End If
    84.             End If
    85.         End Sub
    86.  
    87.         Public Sub EndUpdate()
    88.             If pic Is Nothing AndAlso Not _control Is Nothing Then
    89.                 If TypeOf _control.Parent.Controls(0) Is PictureBox Then
    90.                     pic = CType(_control.Parent.Controls(0), PictureBox)
    91.                 End If
    92.             End If
    93.             If Not _control Is Nothing AndAlso Not _control.Parent Is Nothing AndAlso Not pic Is Nothing Then
    94.                 pic.SendToBack()
    95.                 _control.Parent.Controls.Remove(pic)
    96.                 pic.Dispose()
    97.                 pic = Nothing
    98.                 Me._inUpdate = False
    99.             End If
    100.         End Sub
    101.  
    102.     End Class

    to use, place a panel control (called BallPanel) on your form and set its dockstyle to fill, and add YourBallPictureBox into the
    VB Code:
    1. Private Sub TimerTick() handles .....
    2.         panel
    3.         Dim flickerPreventer As New UpdateFlashPreventer(Me.BallPanel)
    4.         flickerPreventer.BeginUpdate()
    5.         Me.YourBallPictureBox.left = whateverX
    6.         Me.YourBallPictureBox.left = whateverY
    7.         flickerPreventer.EndUpdate()
    8.  
    9.  
    10.     End Sub

  9. #9
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: I can't keep my balls from flickering

    Wrong language PeteD. This is the Classic VB section, not VB.NET.

  10. #10
    Frenzied Member Andrew G's Avatar
    Join Date
    Nov 2005
    Location
    Sydney
    Posts
    1,587

    Re: I can't keep my balls from flickering

    VB Code:
    1. Dim dirX As Long
    2. Dim dirY As Long
    3. Dim speed As Long
    4. Dim Xval As Long
    5. Dim Yval As Long
    6. Private Sub Form_Load()
    7. dirX = -1
    8. dirY = 1
    9. speed = 10
    10. Me.ScaleMode = vbPixels
    11. End Sub
    12.  
    13. Private Sub Timer1_Timer()
    14. Cls
    15. Xval = Xval + (speed) * dirX
    16. Yval = Yval + (speed) * dirY
    17.  
    18. If Xval < 0 Or Xval > Me.ScaleWidth Then dirX = dirX * -1
    19. If Yval < 0 Or Yval > Me.ScaleHeight Then dirY = dirY * -1
    20.  
    21. Me.Circle (Xval, Yval), 20
    22. End Sub

    This will draw a circle and make it move. Hope it helps

  11. #11
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: I can't keep my balls from flickering

    Andrew, this thread is an old thread. Plus you forgot AutoRedraw = True

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width