1 Replies - 960 Views - Last Post: 10 April 2013 - 08:46 AM Rate Topic: -----

#1 moshiro18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 10-April 13

question, bouncing ball

Posted 10 April 2013 - 12:14 AM

Hi, I would like to ask this. I have a circle on my form its shape1. I have 4 command buttons
Up (Command1)
Down (Command4)
Right (Command3)
Left (Command2)

This is my code, I make the ball bounce to the right. But how can I bounce it back when I press Left (Command2), I have a problem on the if statement. I cannot get the logic how can I stop the ball when it reaches the end frame of the form on the left side. As same as when I click DOWN and UP.

Private Sub Command3_Click()
Timer1.Enabled = True
End Sub



    'Right
    Private Sub Timer1_Timer()
        If Shape1.Top <= Form1.ScaleHeight - Shape1.Height And Shape1.Left < Form1.ScaleWidth - Shape1.Width Then
            Shape1.Top = Shape1.Top + 100
            Shape1.Left = Shape1.Left + 20
        Else
            Timer1.Enabled = False
            Timer2.Enabled = True

        End If
    End Sub
    'Right
    Private Sub Timer2_Timer()
        If Shape1.Left > 0 And Shape1.Top > 0 Then
            Shape1.Top = Shape1.Top - 100
            Shape1.Left = Shape1.Left + 20
        Else
            Timer1.Enabled = True
            Timer2.Enabled = False

        End If
    End Sub




Is This A Good Question/Topic? 0
  • +

Replies To: question, bouncing ball

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5678
  • View blog
  • Posts: 12,219
  • Joined: 02-June 10

Re: question, bouncing ball

Posted 10 April 2013 - 08:46 AM

Hardcoding the movement values is the real problem.

COnsider that the ball location is based on x an y .

If moving right, the X is increased.
If moving left, the X is decreased.
If moving up, the Y is decreased.
If moving down the Y is increased.

So do your math and use variables instead of hard coding 100, 200 etc.

Here's the logic you said you were having troubling thinking up. Now all you have to do is code it.
if (moveLeft) horizontalMovement = -100;
if (moveRight) horizontalMovement = 100;
if (moveUP) verticalMovment = -100;
if (moveDown) verticalMovement = 100;

Shape.X += horizontal movment; // remember adding a negitive *is* subtraction.
Shapre.Y += verticalmovement;

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1