Results 1 to 3 of 3

Thread: Graphics Bounds with jump

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    50

    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

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    50

    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:
    1. If doubleJump = False Then
    2.             If e.KeyCode = Keys.Up Then
    3.                 'Resets the animation if the character was moving
    4.                 If tmrJump.Enabled = True Then
    5.                     tmrJump.Enabled = False
    6.                     doubleJump = True
    7.                     characterCount = 0
    8.                 End If
    9.  
    10.                 ' Disables movement animation
    11.                 tmrMove.Enabled = False
    12.                 'Resets character movement
    13.                 characterCount = 0
    14.  
    15.                 ' Sets the animation image to the right direction
    16.                 If movement = "Right" Then
    17.                     movement = "RightJump"
    18.                 ElseIf movement = "Left" Then
    19.                     movement = "LeftJump"
    20.                 End If
    21.                 keyPressed(e.KeyCode) = True
    22.                 tmrJump.Enabled = True
    23.             End If

    vb Code:
    1. Private Sub tmrJump_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrJump.Tick
    2.         ' Next animated frame
    3.         characterCount += 1
    4.         ' Causes the character to jump
    5.         CharacterJump()
    6.         If keyPressed(Keys.Right) Or keyPressed(Keys.Left) Then
    7.             CharacterMove()
    8.         End If
    9.     End Sub

    vb Code:
    1. Private Sub CharacterJump()
    2.         Dim newLocation As Integer = locationY
    3.  
    4.         Select Case characterCount
    5.             Case 1
    6.                 newLocation -= 8
    7.             Case 2
    8.                 newLocation -= 6
    9.             Case 3
    10.                 newLocation -= 4
    11.             Case 4
    12.                 System.Threading.Thread.Sleep(50)
    13.             Case 5
    14.                 If doubleJump = True Then
    15.                     newLocation += 8
    16.                 Else
    17.                     newLocation += 4
    18.                 End If
    19.             Case 6
    20.                 If doubleJump = True Then
    21.                     newLocation += 12
    22.                 Else
    23.                     newLocation += 6
    24.                 End If
    25.             Case 7
    26.                 If doubleJump = True Then
    27.                     newLocation += 16
    28.                 Else
    29.                     newLocation += 8
    30.                 End If
    31.             Case 8
    32.                 'Disables the animation
    33.                 tmrJump.Enabled = False
    34.                 'Resets the character to standing image
    35.                 characterCount = 0
    36.                 If doubleJump = True Then
    37.                     doubleJump = False
    38.                 End If
    39.  
    40.                 keyPressed(Keys.Up) = False
    41.                 If movement = "RightJump" Then
    42.                     movement = "Right"
    43.                 ElseIf movement = "LeftJump" Then
    44.                     movement = "Left"
    45.                 End If
    46.  
    47.                 ' Continues previous movement on ground
    48.                 If keyPressed(Keys.Right) Or keyPressed(Keys.Left) Then
    49.                     tmrMove.Enabled = True
    50.                 End If
    51.         End Select
    52.  
    53.         If CheckForGround(newLocation) = True Then
    54.             newLocation = 342
    55.         End If
    56.  
    57.         locationY = newLocation
    58.  
    59.         ' Animates the character's jump
    60.         Me.Refresh()

    vb Code:
    1. Private Sub tmrMove_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrMove.Tick
    2.         If movement = "Right" Or movement = "Left" Then
    3.             If characterCount = 10 Then
    4.                 ' Loops the animation
    5.                 characterCount = 1
    6.             Else
    7.                 ' Gets the next animation image
    8.                 characterCount += 1
    9.             End If
    10.         End If
    11.  
    12.         CharacterMove()
    13.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width