How to make 1 bullet go through 2 animation frames

BlitzPlus Forums/BlitzPlus Beginners Area/How to make 1 bullet go through 2 animation frames

En929(Posted 2011) [#1]
In my game, I'm trying to make it so that when ONE bullet hits an enemy plane, I want the enemy plane to go through 2 frames before it explodes. That is, I'm trying to give the appearance that when a bullet hits a plane, the plane's catches fire (the first frame) and then later explodes (the second frame) and disappears (is deleted) afterwards.

Below is my game program and the part I'm trying to fix is in the "Collision()" function section. Below, I wrote notes and drew a line there so that it can be easy to find. Thanks.



Graphics 1600, 900
SetBuffer BackBuffer ()
AppTitle "Crazy Airline"



Global AIRPLANE = LoadAnimImage ("Airplane Animation.PNG",260,195,0,5)
Global ENEMYAIRPLANE = LoadAnimImage ("Small Airplane Exploding.PNG", 100,75,0,5)
Global BULLETS = LoadImage ("Bullet.png")
Global ENEMYPLANE

Type AIRPLANE

	Field x,y
	Field image
	Field frame

End Type 

Type BULLETS

	Field x,y
	Field image
End Type 

Type ENEMYPLANE
	Field x,y
	Field image
	Field frame
End Type 




Global ap.AIRPLANE = New AIRPLANE
ap\x = 150
ap\y = 407
ap\image = AIRPLANE
ap\frame = 1



While Not KeyDown (1)


Cls


DrawImage (ap\image, ap\x,ap\y,ap\frame)

Controls()
EnemyCharacteristics()
Collisions()
BulletInstructions()


Flip

Wend



Function EnemyCharacteristics()	



	en.ENEMYPLANE = New ENEMYPLANE
	en\image =  ENEMYAIRPLANE
	en\frame = 1
	en\x = 1000
	en\y = 300

	
		For en.ENEMYPLANE = Each ENEMYPLANE
			
			DrawImage (en\image,en\x,en\y,en\frame)
		

			en\x = en\x - 3
						
		Exit 
		Next 		
		
	
End Function 




Function Controls()

	If KeyDown (205) Then 

	ap\x = ap\x + 3
	
	EndIf 	
	
	If KeyDown (203)  Then 

	ap\x = ap\x - 3
	
	EndIf 



	If KeyDown (208) Then 

	ap\y = ap\y + 3
	
	EndIf 



	If KeyDown (200)  Then 

	ap\y = ap\y - 3
	
		

	EndIf 
	
	If KeyHit(57) Then 
	
		Shoot()
		
		
		
	EndIf 	

End Function 

Function Shoot()



	blt.BULLETS = New BULLETS
		blt\x = ap\x 
		blt\y = ap\y-200
		blt\image = BULLETS
		


End Function 
 




Function BulletInstructions() 

 
	
	For blt.BULLETS = Each BULLETS
	
		DrawImage (blt\image,blt\x,blt\y)
		
			blt\x = blt\x + 20
			
			
	Next 


End Function 










Function Collisions(); --------- I need help here --------------------------------------------------------------------------------------------------------------




;here's the part that I need help with. I want ONE bullet to
cause 2 animation frames to occur. Frame 1 
would be the normal plane (before the bullet), Frame 2 would be 
the plane catching fire after the bullet, and Frame 3 would be
 the explosion, Then afterwards, the plane would disappear
(be deleted). Thanks.


	For blt.BULLETS = Each BULLETS

		For en.ENEMYPLANE = Each ENEMYPLANE

			If ImagesCollide (en\image,en\x,en\y,0,blt\image,blt\x,blt\y,0)  Then
	
							
								en\frame = en\frame + 1
															
											
								If en\frame > 3 Then 
							
							             Delete en
							             Delete blt


								EndIf
				
			Exit 					 
			EndIf 
			
		Next
	Next 					
				

			


;-------------- Above and between these lines is all I need help with for now. Thanks-----------------------------------------------------------------
	









For en.ENEMYPLANE = Each ENEMYPLANE

	If en\x <= 150 Then
	
	
		Delete en
	
	EndIf 
Next 


For blt.BULLETS = Each BULLETS
		
		If blt\x >= 1200 Then
		
			Delete blt
		EndIf 
		


	Next 
	
	
	
	
End Function 





Last edited 2011


Midimaster(Posted 2011) [#2]
your problem shoud be solved by using timers.

Add two new Field to the ENEMY_PLANE: Action% and Time%. Devide the function EnemyCharacteristics() in two functions EnemyCreate() and EnemyPaint().

In normal situation the em\Action is 0 which causes to paint the regular em\frame in EnemyPaint().

When a Collision happens, it would change the em\Action=1 (which means "dying") and set em\Time=Millisecs()+500. This means wait for further 500msec (=0.5sec), until do anything.

In the EnemyPaint() you can now react on the em\Action when the em\Time overruns the Millisecs(). You could step forward em\frame=em\frame+1 and/or set em\Time again for a next waitung period and/or set em\Action to a new value:

...
Type ENEMYPLANE
	Field x,y
	Field image
	Field frame , Time% , Action%
End Type 
....

Function EnemyCreate()	
	en.ENEMYPLANE = New ENEMYPLANE
	en\image =  ENEMYAIRPLANE
	en\frame = 1
	en\Action=0
	en\x = 1000
	en\y = 300
End Function



Function EnenyPaint
		For en.ENEMYPLANE = Each ENEMYPLANE
			;first draw it like before
			DrawImage (en\image,en\x,en\y,en\frame)
			en\x = en\x - 3
			;then check, whether there is an action to do:
			If en\Action=1
				;then check, whether the time is right for doing:
				If en\Time<MilliSecs()
					;do what you want:
					en\frame = en\Frame+1
					en\Time=Milisecs()+500
					;if reached frame 4 (which does not exist) kill me
					If en\frame=4
						Delete en
					EndIf
				EndIf
			EndIf
		Next 				
End Function 




Function Collisions()
	For blt.BULLETS = Each BULLETS
		For en.ENEMYPLANE = Each ENEMYPLANE
			If ImagesCollide (en\image,en\x,en\y,0,blt\image,blt\x,blt\y,0)  Then
					en\Action=1
					en\Time=MilliSecs() + 500
					Delete blt
					Exit					
			EndIf
		Next
	Next
End Function
...


Last edited 2011


En929(Posted 2011) [#3]
Wow, Midimaster, thanks a lot. It worked. Thanks for helping me. I'll come back if I have more questions.