Another non working code

Joined
Apr 8, 2005
Messages
11,424
Reaction score
155
Location
٩(̾●̮̮̃̾•̃̾)۶ __̴ı̴̴̡&
I tried to create a program with a button that bounces when it hits a corner around in VB.net but so far it creates a few invisible walls and bouces within them :confused:

well heres the code it consists of 1 button called "woo" and four timers called timer1, timer2... and I enabled them and put them on 1 split second for each inteval. So heres the code, can any of you see the mistake :confused:

Code:
    Dim xe As Integer = 0
    Dim ye As Integer = 0
    Dim xecontroller As Integer = 0
    Dim yecontroller As Integer = 0
    Dim xcontroler As Integer = 0
    Dim ycontroler As Integer = 0

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If xe >= xcontroler Then
            xecontroller = 1
        ElseIf xe <= 0 Then
            xecontroller = 0
        End If

        If xecontroller = 0 Then
            xe = xe + 1
        ElseIf xecontroller = 1 Then
            xe = xe - 1
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If ye >= ycontroler Then
            yecontroller = 1
        ElseIf ye <= 0 Then
            yecontroller = 0
        End If

        If yecontroller = 0 Then
            ye = ye + 1
        ElseIf yecontroller = 1 Then
            ye = ye - 1
        End If
    End Sub



    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        woo.Location = (New Point(xe, ye))
    End Sub



    Private Sub woo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles woo.Click
        xcontroler = Me.Location.X
        ycontroler = Me.Location.Y
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        xcontroler = Me.Location.X
        ycontroler = Me.Location.Y
    End Sub
 
TIAL did it, so that has to be the first mistake :D Kidding, hope you get it fixed ;)
 
It's quite simple really.

The boundaries that you choose are defined by where your window itself opens. In other words, if Windows opens the window at 100,300, then the boundary is 100 x 300. You should be using Me.Width and Me.Height. Furthermore, you need to take into account the width and height of your button itself, when calculating x border and y border.

try

xcontroler = Me.Width - woo.Width
ycontroler = Me.Height - woo.Height

This works for me
 
Thanks graviti :)

Heres the working code now :D
Also I added code to make the ball go faster :)
Code:
   Dim xe As Integer = 0
    Dim ye As Integer = 0
    Dim xecontroller As Integer = 0
    Dim yecontroller As Integer = 0
    Dim speed As Integer = 1



    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If xe >= Me.Size.Width Then
            xecontroller = 1
        ElseIf xe <= 0 Then
            xecontroller = 0
        End If

        If xecontroller = 0 Then
            xe = xe + speed
        ElseIf xecontroller = 1 Then
            xe = xe - speed
        End If

        If xe >= Me.Size.Width - 24 Then
            xecontroller = 1
        End If

        If ye >= Me.Size.Height - 48 Then
            yecontroller = 1
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If ye >= Me.Size.Height Then
            yecontroller = 1
        ElseIf ye <= 0 Then
            yecontroller = 0
        End If

        If yecontroller = 0 Then
            ye = ye + speed
        ElseIf yecontroller = 1 Then
            ye = ye - speed
        End If
    End Sub
    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        woo.Location = (New Point(xe, ye))
    End Sub


    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        speed = speed + 1
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If speed = 0 = True Then
            MsgBox("Cannot slow down anymore", MsgBoxStyle.Information, "Cannot Reverse")
        ElseIf speed = 0 = False Then
            speed = speed - 1
        End If
    End Sub

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click
        Label1.Location = (New Point(-6000, -9000))
        Button1.Location = (New Point(-6000, -9000))
        Button2.Location = (New Point(-6000, -9000))
    End Sub


    Private Sub woo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles woo.Click
        If speed = 1 Then
            MsgBox("Weeeee Try  catch me at a higher speed :D")
            speed = speed + 1
        ElseIf speed = 0 Then
            MsgBox("Wow you caught me going at the speed of a stable stone")
            speed = speed + 1
        ElseIf speed = 2 Then
            MsgBox("You are getting better :)")
            speed = speed + 1
        ElseIf speed = 3 Then
            MsgBox("Good Good")
            speed = speed + 1
        ElseIf speed = 4 Then
            MsgBox("Heh, I gotta admit you are good but try a little faster")
            speed = speed + 1
        ElseIf speed = 5 Then
            MsgBox("Sheesh I'm suprised you managed to get this one :)")
            speed = speed + 1
        ElseIf speed = 6 Then
            MsgBox("Wow...")
            speed = speed + 1
        ElseIf speed = 7 Then
            MsgBox("You could catch a speedy goat :)")
            speed = speed + 1
        End If

    End Sub

/me thanks BG for laminating my butt :(
 
Hey guys, not like I can code much yet, except that HelloWorld thing, but im going to be doing this at university next year so I'm messing around with some VB.net for fun.

So how would you compile raw code like this? I thought just doing it in notepad, saving it as a .vb file and then using the vbc.exe compiler that comes with .net 2.0 framework would work, but it doesnt :(
Do you have to be using the whole huge Visual Studio.Net software for this or am i missing something? When I try compile it in the vbc command prompt way it give alot of errors with the code.

Thanks for helping a n00b. Or not helping me, which might happen, in that case.. well have this abortion :)
 
Vensters usually the editor comes with a compiler. In the case of VB you would probably need .NET but trust me you would be doing yourself a huge favour by learning c++ or java. Universities tend to focus on object-oriented programming and for this this reason they focus more on c++/java because if you can program with these languages then you can basically apply the same principles to any OO language - it's just a matter of different syntax from language to language.

If you can't get a hold of .NET (*cough* torrents *cough*) then there's a good free assembler/compiler called Dev c++ which I used during my campus days for c++.

RE: the errors - check the details of the errors. In most cases it's simple syntax errors (missing brackets, semi-colons etc.).
 
Okay, well the thing is I know im going to be doing VB.net, otherwise it would probably be C# in .NET that I would do..

I got hold of the free version of the Visual Studio last night, and I realized thats what I need, but thanks anyway. I'll get the stuff free when I start the course anyway, so I dont want to donwload an entire DVD for that now.
 
Top
Sign up to the MyBroadband newsletter
X