Image Does Not Exist Problem

Blitz3D Forums/Blitz3D Beginners Area/Image Does Not Exist Problem

Shuffles(Posted 2011) [#1]
In this program the problem starts when I shoot a bullet from the cyclops and it hits a block. Sometimes the block and bullet disappear. Most of the time it gives me the Image does not exist error. I'm pretty sure this is a type problem. The cause of the problem, I believe, is in the last function at the very bottom of the code. All help is appreciated. :)




Graphics 640,400


;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...
MaskImage stickmancyclops,0,0,0 

Global bulletimage = LoadImage("bullet.bmp")
MaskImage bulletimage,0,0,0

Global BlockImage = LoadImage("block.bmp")
MaskImage BlockImage,0,0,0

;______________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 = 50

Global BlockSpawnRate = 0

SetBuffer BackBuffer()

SeedRnd MilliSecs
;--------------------MAIN LOOP---------------------
While Not KeyDown(1)
	
	BlockSpawnRate = BlockSpawnRate + 1
		
	Cls 

	DrawImage stickmancyclops,cyclops\x,cyclops\y

	MoveCyclops() ; the function that moves the stickman cyclops...
	
	CreateBallandBlock()
	UpdateBallandBlock()
	
	Flip

Wend 
;-------------END OF MAIN LOOP----------------

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

;FUNCTION WHICH CREATES THE BULLETS AND BLOCKS
Function CreateBallandBlock()


	If KeyHit(spacebar)
		bullet.BulletType = New BulletType 
		bullet\x = cyclops\x + 316
		bullet\y = cyclops\y + 28
	EndIf 
	
	If BlockSpawnRate >= 120
		block.blocktype = New BlockType
		block\x = Rand(1,200)
		block\y = Rand(1,200)
	EndIf 
	
	If BlockSpawnRate >= 120 Then BlockSpawnRate = 0
	
End Function 



;FUNCTION WHICH UPDATES THE BULLETS AND BALLS
Function UpdateBallandBlock()

 For bullet.bullettype = Each bullettype
	bullet\y = bullet\y - 4
	DrawImage BulletImage,bullet\x,bullet\y
 	
    
	For block.blocktype = Each blocktype
		If ImagesCollide(blockimage,block\x,block\y,0,bulletimage,bullet\x,bullet\y,0) 
		Delete block
		Delete bullet
		EndIf
    Next


Next

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

		
End Function 



Matty(Posted 2011) [#2]
The reason it doesn't work is because you delete both the block and tghe bullet in this for loop:

For block.blocktype = Each blocktype
		If ImagesCollide(blockimage,block\x,block\y,0,bulletimage,bullet\x,bullet\y,0) 
		Delete block
		Delete bullet
		EndIf
    Next



The problem is that after deleting the bullet you then check other blocks against the same bullet that no longer exists.

Instead you should put an exit statement within the block loop so that after deleting the bullet it exits that for loop and then checks the other bullets...unless you want 1 bullet to be able to destroy more than 1 block at a time...

change to this:

For block.blocktype = Each blocktype
		If ImagesCollide(blockimage,block\x,block\y,0,bulletimage,bullet\x,bullet\y,0) 
		Delete block
		Delete bullet
                Exit
		EndIf
    Next




Warner(Posted 2011) [#3]
Are you sure you get the error 'image does not exist'? Since that wouldn't make much sense.


Shuffles(Posted 2011) [#4]
Matty was completely right on this one. Thank you VERY much Matty.


Matty(Posted 2011) [#5]
Alternatively - if you wanted a bullet to be able to take part in more than one collision and then be deleted have a counter in your bullet type which counts the number of collisions it has taken part in, when it exceeds a certain amount then delete it separately.