My brain hurts!!

Blitz3D Forums/Blitz3D Programming/My brain hurts!!

Hi everyone,

I am working on my first platform style game and im about to lose my mind!

I have a character that walks around with the arrowkeys.
There are blocks that he can't walk through.
Here's my problem.
When i make one block everything works fine, but when i make more than one block with a for..next loop the characters speed is increased for some reason?

I have no idea what is going on!


I have no idea what is going on!



As you haven't posted any code I completely agree!

And I agree with Stevie.

You can't possibly expect us to help without seeing code.

Graphics 1280,1024,0,1
SetBuffer BackBuffer()




Type block
	Field x#,y#
	Field img
End Type

Type player
	Field x#,y#
	Field xv#,yv#
	Field frame
	Field img
End Type

Global player.player,block.block



bimg = CreateImage(128,128)

	SetBuffer ImageBuffer(bimg)
		
		Rect 0,0,127,127
		
	SetBuffer BackBuffer()


;----Create-Player------------------------------
CreatePlayer(0,100,1,1)
															
;_______________________________________________



CreateBlock(100,100,bimg)






While Not KeyHit(1)
Cls



UpdatePlayer()
























Flip
Wend

Function CreatePlayer(x#,y#,xv#,yv#)

	
p.player = New player							
												
p\img = CreateImage(64,64)
	SetBuffer ImageBuffer(p\img)
	
		Rect 0,0,63,63
		
	SetBuffer BackBuffer()						
												
p\x# = x#										
p\y# = y#	
p\xv# = xv#									
p\yv# = yv#									
p\frame = 0				

End Function

Function UpdatePlayer()

	For i.player = Each player
	For g.block = Each block
	
		DrawImage i\img,i\x#,i\y#,i\frame
			
		If KeyDown(203)
		

			
			If ( ImagesOverlap( i\img , i\x# - i\xv# , i\y# , g\img , g\x# , g\y# ) ) = False
			
				i\x# = i\x# - i\xv#
			
			EndIf

		ElseIf KeyDown(205)
			

			
			If ( ImagesOverlap( i\img , i\x# + i\xv# , i\y# , g\img , g\x# , g\y# ) ) = False
			
				i\x# = i\x# + i\xv#
			
			EndIf
			
		EndIf
		
		If KeyDown(200)
		

			
			If ( ImagesOverlap( i\img , i\x# , i\y# - i\yv# , g\img , g\x# , g\y# ) ) = False
			
				i\y# = i\y# - i\yv#
				
			EndIf

		ElseIf KeyDown(208)
			

			
			If ( ImagesOverlap( i\img , i\x# , i\y# + i\yv# , g\img , g\x# , g\y# ) ) = False
						
				i\y# = i\y# + i\yv#
			
			EndIf
			
		EndIf
		
		
		
		
		; wall collisions
		
			If i\x# < 0
			
				i\x# = 0
				
			ElseIf i\x# > GraphicsWidth() - ImageWidth(i\img)
			
				i\x# = GraphicsWidth() - ImageWidth(i\img)
				
			EndIf
			
			If i\y# < 0
			
				i\y# = 0
				
			ElseIf i\y# > GraphicsHeight() - ImageHeight(i\img)
			
				i\y# = GraphicsHeight() - ImageHeight(i\img)
				
			EndIf
			
					
		
		
		
		
		
		DrawImage g\img,g\x#,g\y#
		
		
		
		
		
		
		
		

		
	Next
	Next

End Function



Function CreateBlock(x#,y#,img)

b.block = New block

b\x# = x#
b\y# = y#

b\img = img

End Function

End







End





Ok now if you add a for...next loop around the part that says CreateBlock(100,100,bimg) the player's speed will be increased.

It's because you're altering the player's x/y every time it doesn't hit a block, so the player's position is updated 100 times if you have 100 blocks.

how would i fix that?

Sorry but your code is a mess - I've tidied it up a bit.

p.s. It's very inefficient to check collisions against all the blocks. Really, you only need to check those around you but I'll leave that for you to figure out.

Graphics 1280,1024,0,1
SetBuffer BackBuffer()

Type block
	Field x#,y#
	Field img
End Type

Type player
	Field x#,y#
	Field xv#,yv#
	Field frame
	Field img
End Type

Global player.player,block.block

bimg = CreateImage(128,128)
SetBuffer ImageBuffer(bimg)
Rect 0,0,127,127
SetBuffer BackBuffer()


CreatePlayer(0,100,1,1)
CreateBlock(100,100,bimg)


While Not KeyHit(1)

	Cls
	DrawBlocks()
	UpdatePlayer()
	Flip

Wend

;======================================================================================
;======================================================================================
;======================================================================================

Function CreatePlayer(x#,y#,xv#,yv#)
	
	p.player = New player							
	p\img = CreateImage(64,64)
	SetBuffer ImageBuffer(p\img)
	Rect 0,0,63,63
	SetBuffer BackBuffer()						
	p\x# = x#										
	p\y# = y#	
	p\xv# = xv#									
	p\yv# = yv#									
	p\frame = 0				

End Function

;======================================================================================
;======================================================================================
;======================================================================================

Function UpdatePlayer()

	For p.player = Each player

		Mx# = ( KeyDown(205) - KeyDown(203) ) * p\xv
		My# = ( KeyDown(208) - KeyDown(200) ) * p\yv

		If Mx <> 0 Or My <> 0
			For b.block = Each block
				If ImagesOverlap( p\img, p\x + Mx, p\y, b\Img, b\x, b\y ) Mx = 0
				If ImagesOverlap( p\img, p\x, p\y + My, b\Img, b\x, b\y ) My = 0
			Next
			p\x = LIMIT( p\x + Mx , 0 , GraphicsWidth()-ImageWidth( p\img ) )
			p\y = LIMIT( p\y + My , 0 , GraphicsHeight() - ImageHeight( p\img ) )
		EndIf

		DrawImage p\img,p\x,p\y,p\frame
	
	Next

End Function

;======================================================================================
;======================================================================================
;======================================================================================

Function LIMIT#( v#, low#, high# )

	If v < low v = low
	If v > high v = high
	Return v
	
End Function

;======================================================================================
;======================================================================================
;======================================================================================

Function CreateBlock(x#,y#,img)

	b.block = New block
	b\x# = x#
	b\y# = y#
	b\img = img

End Function

;======================================================================================
;======================================================================================
;======================================================================================

Function DrawBlocks()

	For b.block = Each block
	
		DrawImage b\img, b\x, b\y
		
	Next
	
End Function