Simple Question :)

Blitz3D Forums/Blitz3D Beginners Area/Simple Question :)

Shuffles(Posted 2011) [#1]
Hello. This is a very simple question and I hope for a very simple answer. I'm using the ImagesCollide function using Type instance variables. The problem is that one Type instance is created LATER in the program. So the ImagesCollide function reads it as a non existent variable. My question is how can I make it known to the ImagesCollide function WITHOUT putting them in the same loop. I want them to stay in two different functions. All help is very much appreciated. (THE PROBLEM IN THE CODE IS HIGHLIGHTED)

Graphics 640, 450


;constant variables that won't change
Const uparrow = 200
Const downarrow = 208
Const leftkey = 203
Const rightkey = 205
Const spacebar = 57

;Images

Global stickmancyclops = LoadImage("StickmanCyclops.bmp") ; the stickman cyclops himself...
Global bulletimage = LoadImage("bullet.bmp")
Global BlockImage = LoadImage("block.bmp")
MaskImage bulletimage,0,0,0
MaskImage stickmancyclops,0,0,0 
MaskImage BlockImage,0,0,0


;The Types

Type CyclopsType
Field x,y
End Type 

Type BulletType
Field x,y
End Type 

Type BlockType
Field x,y
End Type

Global cyclops.CyclopsType = New CyclopsType 
 cyclops\x = 50
 cyclops\y = 150


Global blockspawnrate
blockspawnrate = 0

SetBuffer BackBuffer()

;------------THE MAIN LOOP--------------
While Not KeyDown(1)

blockspawnrate = blockspawnrate + 1

Cls

DrawImage stickmancyclops,cyclops\x,cyclops\y
MoveCyclops() ;the function that moves the stickman cyclops...
SpawningBlocks()
FireBullets()
CheckBlockSpawnRate()

Flip

Wend 

;-------THE END OF THE MAIN LOOP-----------


;Functions

Function MoveCyclops() ;moves the STICKMAN CYCLOPS!!!!

	If KeyDown(leftkey) Then cyclops\x = cyclops\x - 3 
	If KeyDown(rightkey) Then cyclops\x = cyclops\x + 3
	If KeyDown(uparrow) Then cyclops\y = cyclops\y - 3
	If KeyDown(downarrow) Then cyclops\y = cyclops\y + 3

End Function

;The function that randomly spawns blocks on screen
Function SpawningBlocks()
If blockspawnrate >= 100

	block.blocktype = New blocktype
	block\x = Rand(0,800)
	block\y = Rand(0,800)
;The PROBLEM IS HERE	If ImagesCollide(BlockImage,block\x,block\y,0,BulletImage,bullet\x,bullet\y,0) Then Delete block
;THE PROBLEM IS HERE
;Since bullet\x and bullet\y aren't existent yet, how can I make them known to the ImagesCollide function without putting them in the same loop?
EndIf 



For block.blocktype = Each blocktype
	DrawImage blockimage,block\x,block\y
Next 

End Function 


;The function for firing the bullets
Function FireBullets()
If KeyHit(spacebar)
bullet.bullettype = New bulletType
bullet\x = cyclops\x + 325
bullet\y = cyclops\y + 50
EndIf

For bullet.BulletType = Each BulletType
bullet\y = bullet\y - 5
DrawImage bulletimage,bullet\x,bullet\y
Next  
End Function 


Function CheckBlockSpawnRate()
If blockspawnrate >= 100.25 Then blockspawnrate = 0
End Function


Last edited 2011

Last edited 2011

Last edited 2011


Matty(Posted 2011) [#2]
You should not check for collisions between a bullet and a block as the block is spawned.

Instead check for collisions when you move the bullet...otherwise how is the program supposed to know which bullet to detect the collision with.

Also there is no point in checking if blockspawnrate is bigger than or equal to 100.25 when it is an integer, it is being incremented by 1 each frame as well, so it will simply trigger when it goes from 100 to 101.

Check for collisions in your function where you move the bullets.


Yasha(Posted 2011) [#3]
Wha...?

In the highlighted section, is "bullet" supposed to be a global or a local variable? If it's a global (very bad), I can't find the declaration; but if it's a local, it's not referenced anywhere else in that function, so there's no way it could ever be set anyway...? Or do you want the "bullet" variable to be specific to certain bullets and the function to only apply to those? In that case, you'd have to make it a function parameter (parameters are what functions are all about - a function without parameters is just a glorified Gosub, and can't do much).

So I think the first thing is that there's an actual design error in your code, as that variable simply doesn't do anything, and can't.

Secondly, the simplest answer to your question would probably be to use another For/Each loop; just put it wherever you want to check for collisions against blocks and that way you can check for collisions with all bullets, not just one - the same way that you're using the loop to update all bullets.

(PS: Please consider using IDEal, with Strict mode and auto-indenting turned on. It will make sure all your variables are declared with either a Global or Local line, and all your code is automatically indented correctly. This makes it much easier for the rest of us - and you, once the code gets longer - to read.)

Last edited 2011


Shuffles(Posted 2011) [#4]
Thanks to both of you. Remember I'm a beginner so all of the little things help. :)