My brain hurts!!

Blitz3D Forums/Blitz3D Programming/My brain hurts!!

Jerome Squalor(Posted 2007) [#1]
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!


Stevie G(Posted 2007) [#2]

I have no idea what is going on!



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


big10p(Posted 2007) [#3]
And I agree with Stevie.

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


Jerome Squalor(Posted 2007) [#4]



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.


big10p(Posted 2007) [#5]
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.


Jerome Squalor(Posted 2007) [#6]
how would i fix that?


Stevie G(Posted 2007) [#7]
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