Posted: 8th Nov 2021 7:27
So my bullets delete at collision, about 90 percent of the time, I guess it is just me or my code.

Here is my collision and delete code, I need help on this one.

+ Code Snippet
For x = 0 to All_Enemy.Length
For Bull = 0 to shot.Length
 
		 
if GetSpriteExists(shot[Bull].ID)=1
	
        shot[Bull].LifeTime= shot[Bull].LifeTime+1
        if shot[Bull].LifeTime=>30
		SetSpritePhysicsOff(shot[Bull].ID)
        DeleteSprite ( shot[Bull].ID ) 
        DeleteSprite ( explodeid )
      
      endif	
      
BX# = GetSpriteXByOffset(shot[Bull].ID)	:	BY# = GetSpriteYByOffset(shot[Bull].ID)

 createclone=1
 if createclone=1 then explodeid=CloneSprite(explode)
 createclone=0  
  
 
SetSpritePositionByOffset(explodeid,BX#,BY#)

if GetPhysicsCollision(shot[Bull].ID,All_Enemy[x].enemyID)=1 then SetSpriteVisible(shot[Bull].ID,0)

if GetPhysicsCollision(shot[Bull].ID,All_Enemy[x].enemyID)=1 

EX# = GetSpriteXByOffset(All_Enemy[x].enemyID)	:	EY# = GetSpriteYByOffset(All_Enemy[x].enemyID)

SetSpriteVisible(explodeid,1)
PlaySprite(explodeid,10,0,1,8)

SetSpritePosition(All_Enemy[x].enemyID,EX#+300,20)
SpawnCoin(All_Enemy[x].enemyID)
inc Coins,1

 DeleteSprite ( shot[Bull].ID )


playsound(sound_1)	
//endif
    
endif
endif

explodeidframe=GetSpriteCurrentFrame(explodeid)

 if explodeidframe>7 then SetSpriteFrame( explodeid,7 ) 
 if explodeidframe=7 then DeleteSprite ( explodeid )  
  
next Bull 
next x
Posted: 8th Nov 2021 7:51
I'm not sure what the problem is but I can see where there should be a change..


+ Code Snippet
For x = 0 to All_Enemy.Length
For Bull = 0 to shot.Length
 
		 
if GetSpriteExists(shot[Bull].ID)=1
	
        shot[Bull].LifeTime= shot[Bull].LifeTime+1
        if shot[Bull].LifeTime=>30
		SetSpritePhysicsOff(shot[Bull].ID)
        DeleteSprite ( shot[Bull].ID ) 
        DeleteSprite ( explodeid )
      
      endif	
      
BX# = GetSpriteXByOffset(shot[Bull].ID)	:	BY# = GetSpriteYByOffset(shot[Bull].ID)

 createclone=1
 if createclone=1 then explodeid=CloneSprite(explode)
 createclone=0  
  
 
SetSpritePositionByOffset(explodeid,BX#,BY#)

if GetPhysicsCollision(shot[Bull].ID,All_Enemy[x].enemyID)=1  //       moved from here
   SetSpriteVisible(shot[Bull].ID,0)     //                              to this line

//  if GetPhysicsCollision(shot[Bull].ID,All_Enemy[x].enemyID)=1     delete this line because you just check it above

EX# = GetSpriteXByOffset(All_Enemy[x].enemyID)	:	EY# = GetSpriteYByOffset(All_Enemy[x].enemyID)

SetSpriteVisible(explodeid,1)
PlaySprite(explodeid,10,0,1,8)

SetSpritePosition(All_Enemy[x].enemyID,EX#+300,20)
SpawnCoin(All_Enemy[x].enemyID)
inc Coins,1

 DeleteSprite ( shot[Bull].ID )


playsound(sound_1)	
//endif
    
//endif     XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX   delete this line
endif

explodeidframe=GetSpriteCurrentFrame(explodeid)

 if explodeidframe>7 then SetSpriteFrame( explodeid,7 ) 
 if explodeidframe=7 then DeleteSprite ( explodeid )  
  
next Bull 
next x
Posted: 8th Nov 2021 7:56
Hi ando

No I just added that for testing to see what would happen because not every bullet get deleted so if not I try to hide them if there not deleted.

It still does nothing.

I ask for help because there is a collision, the bullet bounce's off the target point but does not delete.

about 90 percent of the time it does.
Posted: 8th Nov 2021 8:14
I usually do little tests to track down the problem something like this..


hit=GetPhysicsCollision(shot[Bull].ID,All_Enemy[x].enemyID)
Print(" hit = "+str(hit))

just to see if the collision is actually working or is it something else.

Sorry I'm not much help
Posted: 8th Nov 2021 8:18
Sorry I'm not much help


Your here helping, at least some people care enough to say something lol.

I'm kidding.

No, The collision makes the bullet bounce so there is collision to every bullet should all should delete, so it has to be my code some how.

I am making each bullet into a array so maybe this is why, maybe if I clone each bullet?
Posted: 8th Nov 2021 8:33
Maybe it's a timing thing.

looks like you are deleting the bullet if end of lifetime then checking collision.

What if you move this code down lower...

+ Code Snippet
if GetSpriteExists(shot[Bull].ID)=1
     
        shot[Bull].LifeTime= shot[Bull].LifeTime+1
        if shot[Bull].LifeTime=>30
        SetSpritePhysicsOff(shot[Bull].ID)
        DeleteSprite ( shot[Bull].ID ) 
        DeleteSprite ( explodeid )
 endif
Posted: 8th Nov 2021 8:44
What if you move this code down lower


I tried this and nothing, that was another test thing to try to get rid of the lost bullets.

I wonder if you only get a real collision if a sprite hits the other sprites box point.

I have my sprites in boxes and only 4 points for collision.

I duck and shoot and they hit every time, but when I stand it hits most the time but not every time.
Posted: 8th Nov 2021 8:52
Maybe you are right.

I don't usually use collision for things like that.
I usually check distance between bullet and target and if it is very close then call it a hit.
Posted: 8th Nov 2021 9:04
I usually check distance between bullet and target and if it is very close then call it a hit.


Well, you just helped me, I will do this and it is brilliant.

Thanks

Edit::::

This worked, I guess collisions are not 100 percent always.

Thank you.
Posted: 8th Nov 2021 9:26
Cool, I hope it works for you.

Just one little thing to watch out for.
If you are moving a bullet at speed 5 and the target is 2.5 away and you are checking if the bullet is 2 or closer then it will not count as a hit.
You probably already know that stuff.

EDIT... NICE!
Posted: 8th Nov 2021 9:31
Yes it worked, thank you,

I had to get every bullet number to make it work but it works every time now.
Posted: 8th Nov 2021 14:01
You are checking your bullets from 0 to the length of the array.
If you are deleting a bullet then you will not check the next bullet in the array because your loop will step past the next element
you should check bullets from the length of the array to 0
Posted: 9th Nov 2021 3:32
you will not check the next bullet in the array because your loop will step past the next element


I figured this also, this is why I am, using another value to get the bullet number and then it worked so I believe you are correct, as soon as I used

Bullet_Num=Shots.ID

This took it out of the array and then worked every time.

From now on I'm going to have to remember if I resize my arrays to get the value first.

Thanks blink0k

But also Using a distance I noticed there is no bullet misses now also, so every bullet is used.
Posted: 9th Nov 2021 15:16
But also Using a distance I noticed there is no bullet misses now also, so every bullet is used.


Some times collisions can fail to register if the sprite is moving faster then the physics is updating, I stumbled across this problem years ago in GameMaker, I think I ditched the bullet collision and cast a 2d ray instead ..

From now on I'm going to have to remember if I resize my arrays to get the value first.


If you resize an array in a loop you always, without exception, run the loop in reverse, from the top down, from end to start, iE: backwards!

For index=myArray.length to 0 Step -1
// do some stuff
myArray.remove(index) << safe to remove from the top
Next
Posted: 9th Nov 2021 15:35
For index=myArray.length to 0 Step -1


I do this and most the time I get a error of out of bounds a lot and also I did this for my bullets and it still did not work, I still had to get the number this way to remove it from the array because I am resetting the array back to 0 every bullet shoot.

But yes, I understand what you are saying.