New Bug in Platform Code.

BlitzMax Forums/BlitzMax Beginners Area/New Bug in Platform Code.

here this is the best I can do with out rewritting your code:

SuperStrict


Graphics 800 , 600

Global Tiles:TImage = LoadAnimImage("ice_floortiles.png",32,32,0,8)
Global Player:TImage = LoadImage("player.png")


Const MAPWIDTH:Int = 800/32
Const MAPHEIGHT:Int = 600/32

Global Map:Int[MAPWIDTH , MAPHEIGHT]

Global PlayerX:Float 'The players X Location On Screen
Global PlayerY:Float ' The Players Y Location On Screen

Global CheckPlayerPosition:Int = 0 ' A Flag to check the Players Current position. 

Global Direction:Int = -1' The Direction the Player is travelling in

Global Jump:Int = 0 ' Are we Jumping
Const Gravity:Float = 0.2 ' Amount of Gravity to apply to JumpHeight
Global JumpHeight:Float = 5.3' The height The player Jumps in pixels
Global CanJump:Int = 1 ' Whether we can Jump. If we're already jumping we can't jump again ie. double jump.
Global Falling:Int = 0 ' Whether the player is falling. 1 = Falling
'Global Jumping:Int = 0 ' Are we jumping

ReadLevelData() ' We Read the level data

While Not KeyHit(KEY_ESCAPE)
	
	Cls
	
	DrawMap()
	CheckIfPlayerCollideWithTile()
	DrawPlayer()
	MovePLayer()
	DoJump()
	
	
	Flip
	
	
Wend

'######################################################
'#### Function DrawPLayer()							  #
'#### Draws Player to Screen and gets Player Position #
'######################################################
Function DrawPlayer()
	If CheckPlayerPosition = 0 ' We use this to check if there is a number 2 in the tilemap array. If you notice there is a number 2
							   'in the defdata statements. This is used to start the player at that position.		
	For Local x:Int = 0 Until MAPWIDTH ' Loop until MapWidth
		For Local y:Int = 0 Until MAPHEIGHT' Loop Until MapHeight
			Select Map[x,y]' We use a select function to select check data in the MapArray
				Case 2 ' If the data in the map array = 2\/ 
					PlayerX = getX(x) 'we get the location in pixels of the tiles x position and pass it to PlayerX
					PlayerY = getY(y) ' we get the location in pixels of the tiles y position and pass it to PlayerY
					CheckPlayerPosition = 1 ' We switch this to one because we don't want to check the start position of the player anymore
			End Select
		Next
	Next
	EndIf
	DrawText "PlayerX = " +PlayerX , 0 , 20 
	
	DrawImage Player, PlayerX , PlayerY , 0	' Now we have the where the number 2 was in the MapArray and have passed this position
											'to Player X and PlayerY, we use this to draw the player at that location. So if you move the number 2	
											'elsewhere in the defdata statements the player will be drawn according to where it is.
End Function

'#################################################
'###	Function MovePLayer()					 #	
'## 	Moves Player and checks outer boundaries #	
'#################################################
Function MovePLayer()
	
	If KeyDown(KEY_LEFT) And Not KeyDown(KEY_RIGHT)
			Direction = 0 ' set direction to 0. Going Left
			PlayerX:-2 ' Minus 2 from Players X position to move him left
	ElseIf KeyDown(KEY_RIGHT) And Not KeyDown(KEY_LEFT)
			Direction = 1 ' set direction to 1. Going Right
			PlayerX:+2 ' Add 2 to the players X position to move him right
	End If
	
	If PlayerX-32 <=0 ' Boundary checks to prevent our Player Jumping out the screen
		PlayerX:+2
	ElseIf PlayerX >= 800-64 
		PlayerX:-2
	End If
End Function

'##########################################################
'### Function CheckIfPlayerCollideWithTile()			  #
'### Checks to see if the player has collided with a tile #
'### Checks also if the Player is falling                 #
'##########################################################
Function CheckIfPlayerCollideWithTile()
'#Region 
	Select Direction
		Case 1
		If Jump = 0
			If Map[(PlayerX+31)/32, PlayerY/32] = 1 ' If we put the players X&y position within the map array and divide them by 32
				PlayerX:-2                       ' which is the size of the player and the tiles, we are able to determine which
			EndIf								 ' tile the Player is on or near. if we have a +1 in PlayerX/32+1 or PlayerY/32+1		
		EndIf									 ' it just means we are checking the tiles next to the player. so if we add a +1		
		Case 0									 ' to PlayerY/32 we are checking the tile under the players feet.
		If Jump = 0								 ' In this we are checking the tiles to the right and left and if they = 1 (Tile is present)			
			If Map[PlayerX/32, PlayerY/32] = 1   ' then we stop the player from moving.
				PlayerX:+2
			End If
		EndIf
	End Select
		
'#End Region 

	If Falling = 1 ' If falling = 1 we minus 3.2 from the players Y position making him fall.
		PlayerY:+3.2
		If Map[PlayerX/32,PlayerY/32+1] = 1 Or Map[(PlayerX+31)/32,PlayerY/32+1]' While he's falling if we check to see if a tile is underneath and if there is
			
			If (PlayerY Mod 32.0) <= 6.4 'checks to see if player is close to top of tile if not keep falling
			                             ' 6.4 is twice the falling distance of 3.2 
				PlayerY = PlayerY - (PlayerY Mod 32) 'subtract the remainder to obtain exact start of tile
				Jump = 0                        ' We stop falling. We reset Jump to 0 and CanJump to 1 and also reset the JumpHeight
				Falling = 0
				CanJump = 1
				JumpHeight = 5.5
			EndIf
		EndIf
	End If

	If Not Map[(PlayerX+31)/32,PlayerY/32+1] And Not map[PlayerX/32,PlayerY/32+1]
								' This is used seperately and does nothing apart from enabling me to use the "else bit"
                                ' left of player as well as right of player needs to be checked against 
                                ' the tile so it wont fall of the middle of the tile.
							    ' If there's nothing under the players feet we start falling but only if we're not jumping
		If Jump = 0 And Not (PlayerX Mod 32.0) 'check if not jumping and at the beggining of a tile
			Falling = 1
		EndIf
	End If
   
End Function

'##############################################
'### Function DoJump()                        #
'### Makes our player jump                    #
'##############################################
Function DoJump()
	If KeyHit(KEY_SPACE) And CanJump = 1 ' If we hit the space button and CanJump = 1
		Jump = 1 ' We enable Jump
		CanJump = 0 ' We Disable CanJump. Can't jump while jumping 
	End If
	
	If Jump = 1 ' If Jump is enabled
		PlayerY:-JumpHeight ' we minus JumpHeight from Players Y Position
		JumpHeight:-Gravity ' We minus Gravity from JumpHeight 
		If JumpHeight <=-1.0 Or PlayerY < 32' If JumpHeight <= -1.0  and reached the top tile
			Falling = 1 ' We beging to fall by setting Falling to 1
		EndIf
	End If
	
	
End Function

'########################################
'### Function DrawMap()                 #
'### We Draw the Map to screen          #
'########################################
Function DrawMap()
	For Local x:Int = 0 Until MAPWIDTH ' we loop until MapWidth
		For Local y:Int = 0 Until MAPHEIGHT ' We loop until MapHeight
			Select Map[x,y] ' We select the data stored in the MapArray
				Case 1 ' if it = 1
					DrawImage Tiles , x*32 , y*32 , 0 ' we draw the tiles to there corresponding position
			End Select
		Next
	Next
End Function

'#######################################################
'### Function ReadLevelData()            			   #
'### We read the data stored in the defdata statements #
'#######################################################
Function ReadLevelData()
	For Local y:Int = 0 Until MAPHEIGHT' We loop through the Y position first until MapHeight
		For Local x:Int = 0 Until MAPWIDTH ' We Loop Until MAPWIDTH
			Local data:Int  ' Setup a local variable to store the data in
			ReadData data ' We read the data from the defdata statements in to the data variable
			Map[x,y] = data ' we assign the data in the data variable to our MapArray
		Next
	Next
End Function

'Function getX:Int(x:Int)
'   Return sizeX * x '+ offsetX
'End Function
'
'Function getY:Int(y:Int)
'   Return sizeX * y '+ offsetX
'End Function


Function getX:Int(x:Int)
   Return 32 * x '+ offsetX
End Function

Function getY:Int(y:Int)
   Return 32 * y '+ offsetX
End Function

DefData 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 2, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
DefData 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
DefData 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1


it will not allways fall and it has to do with the 6.4 test being performed while still moving left or right. if you happen to stop moving at the exact end of the tile then it will fall, such is the case when reaching a wall. you can add a test to only accept movement input when not falling and this will allow falling allways. You can figure that out right?