Follow the mouse
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
1 post
Page 1 of 1
Hello, this is something I've been working on for EGM. I was thinking maybe for enemy's to chase the player for example.
Let's get started then
Create a new project, add a Picturebox and a timer. I named my picturebox pctrEnemy, set the interval for the timer to 10 and enabled True.
We need two events, one is the MouseMove and the other is the Timer's tick event. We also need two declarations.
Now it's the form's mouse move event.
Now it's the tick event
All code:
Let's get started then
Create a new project, add a Picturebox and a timer. I named my picturebox pctrEnemy, set the interval for the timer to 10 and enabled True.
We need two events, one is the MouseMove and the other is the Timer's tick event. We also need two declarations.
Code: Select all
Put those declarations below the class but not inside any subroutine. Public Mousex As Integer
Public MouseY As Integer
Now it's the form's mouse move event.
Code: Select all
In the MouseMove event we are setting the Mousex and Mousey declarations we made to the coordinates of the mouse, every time we move the mouse. Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
Mousex = e.X
MouseY = e.Y
End Sub
Now it's the tick event
Code: Select all
Here are first creating two integers and setting them to the enemys x and y location. Basically we want the enemy to get closer to the user each time the timer ticks so we need to check if the enemy's x and y location is higher or lower, if its higher we take off 2, otherwise we add 2, same with the y coordinates. We want it to be closer to the mouse. Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim pctrx As Integer = pctrEnemy.Location.X
Dim pctry As Integer = pctrEnemy.Location.Y
If pctrx > Mousex Then
pctrx -= 2
pctrEnemy.Location = New Point(pctrx, pctry)
Else
pctrx += 2
pctrEnemy.Location = New Point(pctrx, pctry)
End If
If pctry > MouseY Then
pctry -= 2
pctrEnemy.Location = New Point(pctrx, pctry)
Else
pctry += 2
pctrEnemy.Location = New Point(pctrx, pctry)
End If
Timer1.Start()
End Sub
All code:
Code: Select all
If you liked my short and simple tutorial remember to +rep and if you have any questions etc. feel free to ask!Public Class frmFollowExample
Public Mousex As Integer
Public MouseY As Integer
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
Mousex = e.X
MouseY = e.Y
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim pctrx As Integer = pctrEnemy.Location.X
Dim pctry As Integer = pctrEnemy.Location.Y
If pctrx > Mousex Then
pctrx -= 2
pctrEnemy.Location = New Point(pctrx, pctry)
Else
pctrx += 2
pctrEnemy.Location = New Point(pctrx, pctry)
End If
If pctry > MouseY Then
pctry -= 2
pctrEnemy.Location = New Point(pctrx, pctry)
Else
pctry += 2
pctrEnemy.Location = New Point(pctrx, pctry)
End If
Timer1.Start()
End Sub
End Class
My name is Tom | Visit my blog where I post new content every day! Tom's Daily Blog | MineCraft is awesome!
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023