I have an assignment for school where I have an animated character that runs. I need to be able make him jump and return to the spot he started the jump after a button press. I have a timer that switches images to animate the character. There are also buttons to speed up, slow down, start, and stop the character. I'm having trouble because I think I have to integrate the timer into the button press and I'm not too sure how to to that. There is a space at the bottom of the code where the jump button is. I'm able to make him go up but can't figure out how to make him come back down.
Here is my code so far:
Private Sub tmrSpeed_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSpeed.Tick
Static Dim intCounter As Integer
picRunner.Location = New Point(picRunner.Location.X + 15, picRunner.Location.Y)
If picRunner.Location.X >= 523 Then
picRunner.Location = New Point(-25, picRunner.Location.Y)
End If
Select Case intCounter
Case 0
Me.picRunner.Image = My.Resources.r0
Case 1
Me.picRunner.Image = My.Resources.r1
Case 2
Me.picRunner.Image = My.Resources.r2
Case 3
Me.picRunner.Image = My.Resources.r3
Case 4
Me.picRunner.Image = My.Resources.r4
Case 5
Me.picRunner.Image = My.Resources.r5
Case 6
Me.picRunner.Image = My.Resources.r6
Case 7
Me.picRunner.Image = My.Resources.r7
Case 8
Me.picRunner.Image = My.Resources.r8
Case 9
Me.picRunner.Image = My.Resources.r9
End Select
intCounter += 1
If intCounter >= 10 Then
intCounter = 0
End If
End If
End Sub
Private Sub btnFaster_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFaster.Click
Try
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval - 10
Catch x As Exception
MessageBox.Show("He can't run any faster!")
End Try
End Sub
Private Sub btnSlower_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSlower.Click
Me.tmrSpeed.Interval = Me.tmrSpeed.Interval + 10
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.tmrSpeed.Stop()
End Sub
Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
Me.tmrSpeed.Start()
End Sub
Private Sub btnJump_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnJump.Click
End Sub
End Class
How would I go about implementing the jump? My teacher says I have to use a loop, obviously to check when the picturebox reaches a certain hot but I can't figure out how to use this with the timer. Any help would be appreciated.
set a loop that calculates force.
also make a variable for the rate of decay for that force.
have the loop apply the force by subtracting it from the current y position
and after it apply force subtract the rate of decay from it to decrease the force so the next time the loop happens the force is lesser and it begins slowing down.
also have a global variable called gravity to create a constant pull on the object downwards so when the upward force decays it will go back down.
The rest you must figure out, if this is an assignment then it is very important for you to develop the logic to do what i described without someone giving you the answer. It will give you better understanding of the code.
I had the exact same assignment, and I came to this following code as my solution. I set the initial interval of the timer to 999999 so that the form wouldn't load with the animation jumping.
Private Sub btnJump_Click(sender As Object, e As EventArgs) Handles btnJump.Click
Me.tmrJump.Interval = 100
Me.tmrJump.Start()
End Sub
Private Sub tmrJump_Tick(sender As Object, e As EventArgs) Handles tmrJump.Tick
Static intOldTop As Integer
Static intCounter As Integer
Dim intStep As Integer = 5
If intCounter = 0 Then intOldTop = picSpaceRunner.Top
intCounter += 1
Select Case intCounter
Case 1 To 15
picSpaceRunner.Top -= intStep
Case 16 To 30
picSpaceRunner.Top += intStep
Case Else
picSpaceRunner.Top = intOldTop
intCounter = 0
tmrJump.Stop()
End Select
End Sub
End Class
Related
I'm having a slight conflict with a button which I've been working with in Visual Basic NET.
My first code sample is for my Button_Height_Tick, which controls changing the button's height:
Dim ChangeHeight As Boolean = False
Private Sub Button_Height_Tick(sender As Object, e As EventArgs) Handles Button_Height.Tick
If Not ChangeHeight Then
Do Until FlatButton1.Height = 63
FlatButton1.Height += 1
System.Threading.Thread.Sleep(1)
Loop
ChangeHeight = True
Else
End If
End Sub
And for my FlatButton1_MouseHover.
Private Sub FlatButton1_MouseHover(sender As Object, e As EventArgs) Handles FlatButton1.MouseHover
Button_Height.Enabled = True
Button_Height.Start()
End Sub
Now, as you can see in the Button_Height_Tick sub, the code changes the height of the button to 63, however, when this code is ran, the buttons total height is changed.
Here are some photos in-case I haven't explained it well.
What my original button looks like
What I want it to do
What it's doing (going up in size vertically going down, when I want it to go up)
Please comment below if you don't understand this question.
You need to change the 'Top' position and also I notice you have a timer then just go in a do a loop. In your example there's no need for a timer.
I'll give an example using a timer and hopefully you'll understand it and can use it for what you want. I've changed 'hover' to 'enter' and 'leave'.
If it's too slow just change the increment amount.
Dim ChangeHeight As Boolean = False
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If ChangeHeight Then
FlatButton1.Height += 2
FlatButton1.Top -= 2
If FlatButton1.Height < 63 Then Exit Sub
FlatButton1.Height = 63
Timer1.Enabled = False
Else
FlatButton1.Height -= 2
FlatButton1.Top += 2
If FlatButton1.Height > 31 Then Exit Sub
FlatButton1.Height = 31
Timer1.Enabled = False
End If
End Sub
Private Sub FlatButton1_MouseEnter(sender As Object, e As EventArgs) Handles FlatButton1.MouseEnter
ChangeHeight = True
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Private Sub FlatButton1_MouseLeave(sender As Object, e As EventArgs) Handles FlatButton1.MouseLeave
ChangeHeight = False
If Timer1.Enabled Then Exit Sub
Timer1.Enabled = True
Timer1.Start()
End Sub
Hello and welcome to StackOverflow. I did a little example of how to achieve what you are looking for.
Code:
Public Class Form1
Dim buttonXCoordinate As Integer
Dim buttonYCoordinate As Integer
Dim buttonOriginalHeight As Integer
Dim buttonOriginalLocation As Point
Private Sub GetButtonCoordinate()
buttonXCoordinate = testBtn.Left
buttonYCoordinate = testBtn.Top
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
buttonOriginalHeight = testBtn.Height
buttonOriginalLocation = testBtn.Location
GetButtonCoordinate()
End Sub
Private Sub testBtn_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseEnter
Dim buttonLocation As Point = Nothing
GetButtonCoordinate()
buttonLocation.X += buttonXCoordinate
buttonLocation.Y += buttonYCoordinate - buttonOriginalHeight
testBtn.Height += buttonOriginalHeight
testBtn.Location = buttonLocation
End Sub
Private Sub testBtn_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles testBtn.MouseLeave
testBtn.Height = buttonOriginalHeight
testBtn.Location = buttonOriginalLocation
End Sub
End Class
I did it really fast but it's enough to give you an idea to how to achive your goal.
In my example there is a button called testBtn, when you go over it with the mouse it the button's height is increased and it returns back to normal when you move your mouse out of it
I would like to do a For Loop inside a timer. Since I want to check an array with element row continuously and I cant continue since the loop seems to not move from 0 on tick, is there a way around this?
I first tried this:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
For row = 0 To 9
msgbox(row)
Next
And then I tried another approach, as suggested in an answer
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
row+=1
msgbox(row)
if row = 10 then
timer1.stop()
end if
Next
MsgBox will now output 0++ on tick but does not stop at 10.
Output Picture
Dim row As Integer = -1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
RefreshData()
Timer1.Enabled = True
initialize()
End sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
increment()
MsgBox(row)
End sub
Private Sub increment()
If row > 8 Then
row = 0
Else
row = row + 1
End If
'my if else statement for checking array variable(row)
End Sub
With this code msgbox was able to output 0 to 9 and repeat the process since i need it to continuously monitor the array. For some reason though when msgbox is situated at the first line of increment method or on timer tick before increment is called the output stays -1 the whole time dunno why. Anyways thanks for all the input as im still new to visual basic 2015
Instead of using for loop use a global variable and increment it inside the tick function
Dim ctr as integer = 0
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ctr=ctr+1
row(ctr)
msgbox(row)
if ctr == 10 then
ctr = 0
timer1.stop
end if
I would like to read line by line from richtextbox and show each line every a second in label.
I have this code blocks.
and I think I need a timer but I couldnt make it.
can you help me ?
Remarks :
If I use this code , I can only see the last line in label.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim RotateCount As String()
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text = RichTextBox1.Lines(i)
Next
End Sub
I mean, assume that we have lines in richtextbox like..
a1
b2
c3
d4
e5
and I would like to show label1 in for each second like..
a1
(after 1 sec.)
b2
(after 1 sec.)
c3
(after 1 sec.)
like this...
You seems to expect that, because you set the Text property, the label repaints itself immediately with the new text. This doesn't happen until you exit from the event handler and the system could repaint the label. Of course, with this code, only the last text is shown.
To reach your goal, you could use a Timer set to 1 second interval and a counter that keeps track of the current line dispayed:
Dim tm As System.Windows.Forms.Timer = new System.Windows.Forms.Timer()
Dim counter As Integer = 0
At this point your button click just start the timer and exits
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tm.Interval = 1000
AddHandler tm.Tick, AddressOf onTick
tm.Start()
' Don't allow to click again this button until
' the timer is stopped
Button1.Enabled = False
Button2.Enabled = True
End Sub
When the Tick event is raised you change the label text to the line indexed by the counter, increment it and check if you have reached the last line restarting from the first one if this is the case. Note that the button is disabled before exiting. This is required to avoid a second/third/fourth/etc click on the same button while the timer runs..... More on Button2 later....
Sub onTick(sender as Object, e as EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
if counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Of course, now you need another button to stop the Timer run and reenable the first button
' This button stops the timer and reenable the first button disabling
' itself - It should start as disabled from the form-designer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
tm.Stop
RemoveHandler tm.Tick, AddressOf onTick
Button1.Enabled = True
Button2.Enabled = False
End Sub
You're almost there. Your problem is that you keep setting the text, not adding to it. Label1.Text = ... sets the text, if you want to keep adding to it you'd use Label1.Text &= ...
Also note that you need to include something like Environment.NewLine in order to include line breaks.
For i As Integer = 0 To RichTextBox1.Lines.Length - 1
Label1.Text &= RichTextBox1.Lines(i) & If(i < RichTextBox1.Lines.Length - 1, Environment.NewLine, "")
Next
thank you for your help !!!
I solved with this code ;
Public Class Form1
Dim tm = New System.Windows.Forms.Timer()
Dim counter As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Sub onTick(sender As Object, e As EventArgs)
Label1.Text = RichTextBox1.Lines(counter)
counter += 1
If counter >= RichTextBox1.Lines.Count Then
counter = 0
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For i = 0 To RichTextBox2.Lines.Count - 1
TextBox1.Text = RichTextBox2.Lines(i)
wait(2000)
Next
End Sub
Private Sub wait(ByVal interval As Integer)
Dim sw As New Stopwatch
sw.Start()
Do While sw.ElapsedMilliseconds < interval
' Allows UI to remain responsive
Application.DoEvents()
Loop
sw.Stop()
End Sub
End Class
It is very simple.
Declare one more string variable and load all string to this variable .
Improved code is given below.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As system.EventArgs) Handles Button1.Click
Dim a1 as int32
a1=0
'get number of lines of the rich text box content
a1=RichTextBox1.Lines.Count()
Dim str As String
For i As Int32 = 0 To a1-1
str = str + RichTextBox1.Lines(i)
Label1.Text= str
Next
End Sub
I need a way to finding out when the time from 0 ends up as 4 secs.
My code is as follows:
I have a global variable called weightdelaycount, which is incremented every 1000 intervals.
Private Sub Timer_weightcheck_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer_weightcheck.Tick
weightdelaycount = weightdelaycount + 1
End Sub
Now I have the do while loop that runs infinitely and only stops depending on two conditions. Either the weightchange = True or if the clock = 4 secs.
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
weightdelaycount = 0
Timer_weightcheck.Enabled = True
Do While 1
If (weightchange() = True ) Then
Timer_weightcheck.Enabled = False
Exit Do
End If
If (weightdelaycount = 4) Then
Timer_weightcheck.Enabled = False
Exit Do
End If
Loop
MessageBox.Show(weightdelaycount)
End Sub
From the routine above you see that I'm using exit Do to exit the loop if the two conditions are met. The problem is that if the weightchange() is not True and 4 seconds passed the systems doesn't stop. I can put a delay in there and then it works, but I need to be accurate with the values that I get for the weight from a scale. If I put a delay, then the values will not be accurate. Is there a way to solve this?
Thank you in advance
Instead of using a timer, you could use a stopwatch. This code will loop until 4000ms after the stopwatch has started. You'll never get it to stop exactly on 4000ms because of the time it takes to run through the the Do..Loop(minimal really), but I presume a couple of ms after is close enough.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim weightdelaycount = 0
Dim timer As New Stopwatch
timer.Start()
Do
If (weightchange() = True) Then
timer.Stop()
Exit Do
End If
Loop Until timer.ElapsedMilliseconds > 4000
timer.Stop()
MessageBox.Show(timer.ElapsedMilliseconds)
End Sub
I have this code:
If RotateCount = RichTextBox1.Lines.Count Then
RotateCount = 0
End If
TextBox2.Text = RichTextBox1.Lines(RotateCount)
RotateCount += 1
The Question is... How do i get it to STOP after it reads the last line and not keep repeating?
Also, is there a way to automate it so I dont have to keep hitting the button to go to next one?
(for those who need to know this is for an automated Twitter poster for a Cryptocurrency Tipping Bot)
It wont stop because you re-assign RotateCount = 0 when RotateCount = RichTextBox1.Lines.Count it can be avoided by using
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If RotateCount = RichTextBox1.Lines.Count Then
Exit Sub
End If
TextBox1.Text = RichTextBox1.Lines(RotateCount)
RotateCount += 1
End Sub
If you want to do it automatically means you need to call a time delay between read using a timer. you can achieve this by placing a timer control from the tool box and then set its interval to 5000(5sec) and enable it on button click. then the code will be
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
If RotateCount = RichTextBox1.Lines.Count Then
Exit Sub
End If
TextBox1.Text = RichTextBox1.Lines(RotateCount)
RotateCount += 1
End Sub