Graphics Bounds with jump
Hi, I am trying to make bounds for a character that can jump. I have made the character graphic into a rectangle and I have a square block in the game that the character must jump onto and I have set that as a rectangle.
How would I make it so when the character jumps onto the square block, he stays up there and then when he jumps down, and touches the ground, he stays on the ground. My current jump code is standardized; it does not detect changes in the Y-coordinate. What I have done in the jump for the character is I have made it so the Y-coordinate is increased by a certain amount and decreased by the same amount so the difference is equal to 0.
For example:
Character.LocationY += 8
Character.Location -= 8
Re: Graphics Bounds with jump
What you'll need is collision detection. Calculate where the character will be if you were to apply the new coordinates (but don't actually assign them yet). Then, check if these new (not-yet-assigned) bounds of the character intersect with the bounds of the block/ground/whatever. If they do, the character is going to collide (but hasn't yet because you haven't assigned the new coordinates yet), so you simply set its coordinates so that it is on top of the block/ground/whatever.
If there is going to be no collision, you simply assign the new coordinates and wait for the next step (where you do the same thing).
In pseudo-code:
Code:
' Calculate new location
Dim newLocation = character.Location.Y + 8 'or something
' Check every obstacle (not necessarily a loop, but i can't know what kind of obstacles you have)
For Each obstacle
If newLocation is inside obstacle Then
' we are going to collide, so prevent us from going inside the obstacle
newLocation = obstacle.Location.Y
Exit For
End If
Next
' Assign the new location
character.Location.Y = newLocation
If the new location does not intersect with any obstacles, the entire For Each loop does nothing so you just assign the new location. If there is a collision though, newLocation is changed so that it stays on that obstacle and does not go inside it.
Re: Graphics Bounds with jump
Thank you very much! I have a small problem now, if the user holds down the up arrow key after the character has doublejumped then the character will no longer doublejump, but just do one jump instead.
Here is my jump code in the form_Keydown event:
vb Code:
If doubleJump = False Then
If e.KeyCode = Keys.Up Then
'Resets the animation if the character was moving
If tmrJump.Enabled = True Then
tmrJump.Enabled = False
doubleJump = True
characterCount = 0
End If
' Disables movement animation
tmrMove.Enabled = False
'Resets character movement
characterCount = 0
' Sets the animation image to the right direction
If movement = "Right" Then
movement = "RightJump"
ElseIf movement = "Left" Then
movement = "LeftJump"
End If
keyPressed(e.KeyCode) = True
tmrJump.Enabled = True
End If
vb Code:
Private Sub tmrJump_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrJump.Tick
' Next animated frame
characterCount += 1
' Causes the character to jump
CharacterJump()
If keyPressed(Keys.Right) Or keyPressed(Keys.Left) Then
CharacterMove()
End If
End Sub
vb Code:
Private Sub CharacterJump()
Dim newLocation As Integer = locationY
Select Case characterCount
Case 1
newLocation -= 8
Case 2
newLocation -= 6
Case 3
newLocation -= 4
Case 4
System.Threading.Thread.Sleep(50)
Case 5
If doubleJump = True Then
newLocation += 8
Else
newLocation += 4
End If
Case 6
If doubleJump = True Then
newLocation += 12
Else
newLocation += 6
End If
Case 7
If doubleJump = True Then
newLocation += 16
Else
newLocation += 8
End If
Case 8
'Disables the animation
tmrJump.Enabled = False
'Resets the character to standing image
characterCount = 0
If doubleJump = True Then
doubleJump = False
End If
keyPressed(Keys.Up) = False
If movement = "RightJump" Then
movement = "Right"
ElseIf movement = "LeftJump" Then
movement = "Left"
End If
' Continues previous movement on ground
If keyPressed(Keys.Right) Or keyPressed(Keys.Left) Then
tmrMove.Enabled = True
End If
End Select
If CheckForGround(newLocation) = True Then
newLocation = 342
End If
locationY = newLocation
' Animates the character's jump
Me.Refresh()
vb Code:
Private Sub tmrMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMove.Tick
If movement = "Right" Or movement = "Left" Then
If characterCount = 10 Then
' Loops the animation
characterCount = 1
Else
' Gets the next animation image
characterCount += 1
End If
End If
CharacterMove()
End Sub