blitzbasic

Started by
7 comments, last by Dave Hunt 18 years, 8 months ago
hi guys, im new here, and new to game programming too. Anyway, my code for a 2d shooter game Im making is having problems with the collision. Here is the function im having problems with:

;moves the bullets and detects collision
Function UpdateBullets()

   ;check every bullet
   For bullet.bullet = Each bullet

      ;check if the bullet is from the player 
      If bullet\from = 1

         ;if player is facing right
         If player\direction = MRIGHT
            ;move the bullet right
            bullet\x = bullet\x + 20
         EndIf 
		
         ;if player is facing left
         If player\direction = MLEFT
            ;move the bullet left
            bullet\x = bullet\x - 20
         EndIf
		
      ;End "if bullet\from = 1" 
      EndIf 
		
      ;check if the bullet is from the enemy
      If bullet\from = 2
         ;move the bullet left
         bullet\x = bullet\x - 20
      EndIf 
		
      ;If bullet is from the player then draw the correct image
      If bullet\from = 1
         DrawImage (bulletimage, bullet\x, bullet\y)
      Else
         DrawImage (bulletsimage,bullet\x, bullet\y)
      EndIf
 
      ;if the bullet is from the player
      If bullet\from = 1
         ;CHECK EVERY ENEMY!
         For tank.tank = Each tank 
			
            ;check to see that the player's bullet and the enemy collided
            If ImagesCollide(bulletimage, bullet\x, bullet\y, 0, tankimage,tank\x,tank\y,0) 
				
               ;subtract a hitpoint
               tank\hits = tank\hits - 1
				
               ;if there are no hitpoints left delete enemy
               If tank\hits <= 0
                  Delete tank
               EndIf 
               ;and the bullet
               Delete bullet
					
            EndIf
         Next
      EndIf
		
      ;if the bullet is from an enemy
      If bullet\from = 2 
			
         ;and the player and enemy's bullet collide
         If Imagescollide(bimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0)

            ;subtract a hitpoint
            player\hits = player\hits - 1
						
            ;if the player's hits = 0 delete the player
            If player\hits <= 0
               Delete player
			
               ;end the program
               End 
            EndIf
				  
         EndIf
		 
         ;delete the bullet if it collided
         Delete bullet
		
      EndIf
   Next		
End Function 
When I run the program, it highlights this: If Imagescollide(bimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0) and says "object does not exist". Keep in mind this is not the entire game. it is just 1 function. Thanks in advance. [Edited by - Fruny on August 25, 2005 10:45:43 AM]
Advertisement
I assume you are loading bimage and playerimage. Have you checked they did actually load?

Try printing out the values in bimage and playerimage. If they are 0 then (I believe) the image hasn't been loaded properly.
ok, I made sure That those images exist.And they do. I really don't know what the problem is. . . . .
You have "bimage" in the line with the error, but elsewhere you have "bulletimage". Is that perhaps the problem?

edit - I also noticed you have "bulletsimage" in another place. That might be a problem waiting to happen, as well.


[Edited by - Dave Hunt on August 25, 2005 9:20:16 AM]
well, actually, i just made it bimage so it would fit on 1 line in this site. And im pretty sure there is something wrong with the player. . . .

In my program,the player's bullets are bulletimage. The enemy's bullets are bulletsimage. Im sorry, it is kinda confusing. so replace the top "bimage" with "bulletimage" and the bottom one with "bulletsimage".
Ok. Understood.

In your outer loop, if the player is killed, you delete the player object, but the loop continues. So, on the next iteration, you would be referencing the deleted object.

Also, in your inner your loop, if the bullet was from the player and hits an enemy, you delete the bullet. Then you continue checking that bullet against the next enemy. When the inner loop is finished, you then check that (potentially) deleted bullet to see if it was from the player.

From what I can see, you have a number of places where you might be referencing deleted objects.

Without seeing the rest of the code, I can't say what else might be wrong with your objects.

As an aside, when posting code, try surrounding the code with "[ source ]" and "[ /source ]" (without the quotes and spaces). That will maintain your formatting and make it easier for us to read.
yeah, sorry about that, im new here, so I didn't know how to do that. Thanks for your help, im gonna fool around with the code now and try and make it work.

Oh yeah, Here is all the code in case you need need it.

Graphics 800,600,0,2;load the image Global tankimage = LoadImage("commando.bmp")Global playerimage = LoadAnimImage("alien.bmp" ,40,55,0,8)Global bulletimage = LoadImage("bullet.bmp")Global grassimage = LoadImage("grass.bmp")Global bulletsimage = LoadImage("FBIbullets.bmp");set the buffer, automidhandle and seedingSetBuffer BackBuffer()AutoMidHandle TrueSeedRnd MilliSecs();directions the player is facingConst MLEFT = 1Const MRIGHT = 2Const RKEY = 205Const LKEY = 203Const UKEY = 200Const DKEY = 205Const SPACE = 57;types__________Type player		Field x,y		Field frame		Field direction		Field hitsEnd TypeType bullet 		Field x,y		Field from End Type Type tank		Field x,y		Field hits		Field xv,yvEnd Type;\types_______________;create the playerGlobal player.player = New player;player's coordinates, frame, direction, and hitsplayer\x = 300player\y = 300player\frame = 0player\direction = MLEFTplayer\hits = 1;create the enemyGlobal tank.tank = New tank;tank's coordinates, hits, and velocitytank\x = 400tank\y = 300tank\hits = 15tank\xv = Rand(-5,5) tank\yv = Rand(-5,5);_________________________________________________________________________;MainloopWhile Not KeyDown(1);clear the screenCls;backgroundTileBlock Grassimage ;move player leftIf KeyDown(LKEY)				player\x = player\x - 5			player\frame = (player\frame + 1 ) Mod (4) + (4 * (player\direction) -4)			player\direction = MLEFT	EndIf    ;move player rightIf KeyDown(RKEY)			player\x = player\x + 5			player\frame = (player\frame + 1 ) Mod (4) + (4 * (player\direction) -4) 			player\direction = MRIGHT				EndIf		 	;jumping (not finished)	If KeyHit(UKEY)	player\y = player\y  - 20	player\y = player\y + 20	EndIf							;shoot the bullet!	If KeyDown(SPACE)		Createbullet(player\x + 36, player\y + 22, 1)				EndIf 	 		;call all the necessary functions		UpdateBullets()		drawtank()		Drawplayer()								AI()	;flip it and wait a fraction of a sec 			Flip Delay 50 Wend ;_________________________________________________________________________;moves the bullets and detects collisionFunction UpdateBullets()	;check every bullet	For bullet.bullet = Each bullet				;check if the bullet is from the player 		If bullet\from = 1				;if player is facing right		If player\direction = MRIGHT		;move the bullet right		bullet\x = bullet\x + 20		EndIf 		 				;if player is facing left		If player\direction = MLEFT		;move the bullet left		 bullet\x = bullet\x - 20		EndIf				;End "if bullet\from = 1" 		EndIf 				;check if the bullet is from the enemy		If bullet\from = 2		;move the bullet left		bullet\x = bullet\x - 20		EndIf 			;If bullet is from the player or enemy then draw the correct image		If bullet\from = 1				 DrawImage (bulletimage, bullet\x, bullet\y)		Else											DrawImage (bulletsimage,bullet\x, bullet\y) 		 EndIf 		;if the bullet is from the player		If bullet\from = 1		;CHECK EVERY ENEMY!		For tank.tank = Each tank 					;check to see that the player's bullet and the enemy collided				If ImagesCollide(bulletimage, bullet\x, bullet\y, 0, tankimage, tank\x, tank\y,0)								;subtract a hitpoint				tank\hits = tank\hits - 1								;if there are no hitpoints left delete enemy				If tank\hits <= 0					Delete tank				EndIf 					;and the bullet					Delete bullet									EndIf				  						Next 		EndIf				;if the bullet is from an enemy		If bullet\from = 2 									;and the player and enemy's bullet collideIf ImagesCollide(bulletsimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0)					;subtract a hitpoint						player\hits = player\hits - 1										;if the player's hits = 0 delete the player						If player\hits <= 0							Delete player														;end the program							End 						EndIf						  								EndIf		 				 ;delete the bullet if it collided		Delete bullet				 			 EndIf				  	 	Next		End Function 		Function drawtank();this just creates the enemy and playerFor tank.tank = Each tank	DrawImage tankimage, tank\x,tank\y		Locate 0,0		Print "Tank hits: " + tank\hits NextEnd Function  Function drawplayer() 	For player.player = Each player		DrawImage playerimage, player\x, player\y, player\frame	Next End Function Function AI();the poor enemy AIFor enemy.tank = Each tank		If enemy\y = enemy\y 			Createbullet(Enemy\x + 36, enemy\y + 22, 2)		EndIf enemy\x = enemy\x + enemy\xvIf enemy\x >=790	enemy\xv = -enemy\xvEndIfIf enemy\x <= 10	enemy\xv = -enemy\xvEndIfNext  End Function ;_________________________________________________________________________ ;creates the bulletFunction Createbullet(x,y,from)bullet.bullet = New bullet						bullet\x = xbullet\y = ybullet\from = fromEnd Function 


[Edited by - Porsche911 on August 25, 2005 1:36:44 PM]
UPDATE:

When I put a For. . . . Next loop before the

If ImagesCollide(bulletsimage,bullet\x,bullet\y,0,playerimage,player\x,player\y,0)


The program works, but If I try to move, It highlights either

Player\x = player\x - 5 (If I am trying to move left)

or

player\x = player\x + 5 (If I am trying to move right)

Also, the enemies bullets don't move
The only other thing I see is in the section that handles bullets from the enemy. You have the "delete bullet" statement AFTER the EndIf for the collision test. So, you will basically delete every enemy bullet whether it hits the player or not.

You also still have the case where, after deleting a bullet that hits a tank, you then check to see if that bullet came from the player, which should cause an error.

I'm not sure why it would be stopping in your movement code, unless the player object was being deleted somewhere. I don't see anything that would cause that.

This topic is closed to new replies.

Advertisement