Anim too fast...
BlitzPlus Forums/BlitzPlus Programming/Anim too fast...| 
 | ||
| I have made an anim of when i destroy an alien he explode,but...the explode anim is a bit too fast to see o_O' Here's the code: Graphics 640,480,32,1 SetBuffer BackBuffer() Global posizionex Global posizioney Global posalienx Global posalieny Global aereo=LoadImage("d:\jet1.tga") ;a simple sprite Global terreno=LoadImage("d:\terreno.bmp") ;a simple texture of 256*256 Global missil=LoadImage("c:\missile2.bmp") ;a simple sprite Global alien=LoadImage("d:\velivolo1.tga") ;a simple sprite Global explosion=LoadAnimImage("d:\explode.bmp",64,64,0,9);an animstrip of 10 frames MidHandle explosion MidHandle aereo MidHandle missil MidHandle alien HidePointer Const limite_sx=100 Const limite_dx=540 Global b.alieno Global c.esplosione Type missile ;an object called missile Field posx Field posy Field avanzamento Field contatore Field image End Type Type alieno Field movx,movy,movimento,energia,image End Type Type esplosione Field posizionex,posizioney,frame End Type ;MAIN Repeat Cls DrawImage aereo,MouseX(),MouseY() If MouseHit(2)=1 create_alien() EndIf move_alien() If MouseHit(1)=1 ;if i hit the mouse button i create a missile create_missile() EndIf play_missile() ;this start the missil anim Flip Until KeyDown(1)=1 End ;END MAIN Function create_missile() a.missile=New missile a\image=missil a\posx=MouseX() ;the x position of missile creation is the mouse coords(the same coords of my jet of course) a\posy=a\posy+MouseY()-45 ;the missile creation start a bit up to the jet(for simulating the launch-bay hehe) a\avanzamento=4 ;the speed of the missile a\contatore=0 ;the starting value of the animation End Function Function play_missile() For a.missile=Each missile DrawImage a\image,a\posx,a\posy a\posy=a\posy-a\avanzamento ;this move the missile up a\contatore=a\contatore+a\avanzamento ;this start the counter of the missile course ;If a\contatore=> 200 Then Delete a ;if the course reach 200 the missile explode(hehe) For b.alieno=Each alieno If ImagesCollide(a\image,a\posx,a\posy,0,b\image,b\movx,b\movy,0) ;if the alien collide with the missile the energy go down of one unit b\energia=b\energia-1 If b\energia=<0 Then ;Delete b crea_esplosione() esplosione() Delete b EndIf Delete a;if the missile collide with the alien the missile stop it's existance :P Exit ;exit loop if missile is dead. will cause error as they's no missile to check against the aliens EndIf Next Next End Function Function create_alien() b.alieno=New alieno b\image=alien b\movx=100 b\movy=60 b\movimento=1 b\energia=5 ;the "teoric" energy of the alien,from when reach 0 the alien explode End Function Function move_alien() For b.alieno =Each alieno posalienx=b\movx posalieny=b\movy DrawImage b\image,b\movx,b\movy b\movx=b\movx + b\movimento If b\movx => limite_dx b\movx=b\movx-b\movimento b\movimento=-1 EndIf If b\movx =< limite_sx b\movx=b\movx+b\movimento b\movimento=+1 EndIf Text b\movx,b\movy-20,b\energia Next End Function Function crea_esplosione() c.esplosione=New esplosione c\posizionex=b\movx c\posizioney=b\movy c\frame=0 End Function Function esplosione() For c.esplosione=Each esplosione DrawImage explosion,b\movx,b\movy,c\frame c\frame=c\frame+1 If c\frame=9 Then Delete c Next End Function How i can repair this problem? And...there's a better method of doing an animation?? Thanks | 
| 
 | ||
| One way to fix it would be to modify you explosion type to have an additional field Type esplosione Field posizionex,posizioney,frame Field frameCount End Type Then in your crea_Explosion() set the frameCount to 0. In the esplosio() function you increment the frameCount field and when it reaches a specified value reset it to zero and increment your frame. For example to animate the explosion to make it 10 times slower Function esplosione() For c.esplosione=Each esplosione DrawImage explosion,b\movx,b\movy,c\frame c\frameCount = c\frameCount + 1 if c\frameCount > 9 then c\frameCount = 0 c\frame=c\frame+1 If c\frame=9 Then Delete c end if Next End Function | 
| 
 | ||
| -- edit -- beaten to the post by john with a more elegant fix | 
| 
 | ||
| Strange i have modified the code but the anim is always too fast... Here's the two functions: Function crea_esplosione() c.esplosione=New esplosione c\posizionex=b\movx c\posizioney=b\movy c\frame=0 c\frameCount=0 End Function Function esplosione() For c.esplosione=Each esplosione DrawImage explosion,b\movx,b\movy,c\frame c\frameCount = c\frameCount + 1 If c\frameCount > 9 Then c\frameCount = 0 c\frame=c\frame+1 If c\frame=9 Then Delete c End If Next End Function | 
| 
 | ||
| Ooo..OK.. Im not at home so I cant test the code but it seems you have a logic problem. The function esplosione() only gets called once in the play_missile() function after you create the explosion. I think maybe you should remove the call to esplosione() from the play_missile() function and insert it into your main loop after the call to play_missile(). I might be of more help once I get home tonight. | 
| 
 | ||
| ARGH ok i have made it! The logical problems that a person can encounter when coding (a simple routines also...)anything are very tricky!!! I called the function crea_esplosione() into the function play_missile() for ONLY the creation of the object "esplosione"....so when the alien explode i create an instance of the explosion. But the animation of the explosion may be called into the MAIN program or it doesn't work!And i have resolved also the speed problem with another method :P Here's the whole code: 
Graphics 640,480,32,1
SetBuffer BackBuffer() 
Global aereo=LoadImage("d:\jet1.tga") ;a simple sprite 
Global terreno=LoadImage("d:\terreno.bmp") ;a simple texture of 256*256 
Global missil=LoadImage("c:\missile2.bmp") ;a simple sprite 
Global alien=LoadImage("d:\velivolo1.tga") ;a simple sprite
Global explosion=LoadAnimImage("d:\explode.bmp",64,64,1,10);an animstrip of 10 frames
Global animation
Global posizionex 
Global posizioney 
Global b.alieno 
Const limite_sx=100 
Const limite_dx=540 
MidHandle explosion
MidHandle aereo 
MidHandle missil 
MidHandle alien 
HidePointer 
 
Type missile ;an object called missile 
	Field posx Field posy Field avanzamento Field contatore 
	Field image
End Type 
Type alieno 
	Field movx,movy,movimento,energia,image
End Type
Type esplosione
	Field posizionex,posizioney,frame,timer
	End Type
	 
;MAIN
Repeat
	Cls 
	
	DrawImage aereo,MouseX(),MouseY()
 
	If MouseHit(2)=1 
		create_alien()	;if i hit the right-mouse button i create an alien  
	EndIf
	 
	move_alien()	;start the alien movement
	
	If MouseHit(1)=1	;if i hit the mouse button i create a missile 
		create_missile() 
	EndIf
	 
	play_missile() ;this start the missil anim
	
	esplosione()	;this start the explosion anim
	
	Flip
	 
	Until KeyDown(1)=1 
End 
;END MAIN
Function create_missile() 
	a.missile=New missile
	a\image=missil
	a\posx=MouseX() ;the x position of missile creation is the mouse coords(the same coords of my jet of course) 
	a\posy=a\posy+MouseY()-45 ;the missile creation start a bit up to the jet(for simulating the launch-bay hehe) 
	a\avanzamento=4 ;the speed of the missile 
	a\contatore=0 ;the starting value of the animation 
End Function 
Function play_missile() 
	For a.missile=Each missile 
		DrawImage a\image,a\posx,a\posy 
		a\posy=a\posy-a\avanzamento ;this move the missile up 
		a\contatore=a\contatore+a\avanzamento ;this start the counter of the missile course 
		For b.alieno=Each alieno
			If ImagesCollide(a\image,a\posx,a\posy,0,b\image,b\movx,b\movy,0)	;if the alien collide with the missile the energy go down of one unit
					b\energia=b\energia-1
					If b\energia=<0 Then	;if the alien energy go to zero i create an explosion but DON'T animate it now!
					crea_esplosione()	;creation of the object explosion
					Delete b 
					EndIf 
					Delete a	;if the missile collide with the alien the missile stop it's existance :P
					
					Exit ;exit loop if missile is dead. will cause error as they's no missile to check against the aliens
			EndIf
		Next
	Next 
End Function 
Function create_alien() 
	b.alieno=New alieno
	b\image=alien
	b\movx=100 
	b\movy=60 
	b\movimento=1 
	b\energia=5 ;the "teoric" energy of the alien,from when reach 0 the alien explode 
End Function 
Function move_alien() 
	For b.alieno =Each alieno 
		posalienx=b\movx 
		posalieny=b\movy 
		DrawImage b\image,b\movx,b\movy 
		b\movx=b\movx + b\movimento 
		
		If b\movx => limite_dx 
			b\movx=b\movx-b\movimento 
			b\movimento=-1 
		EndIf 
		
		If b\movx =< limite_sx 
			b\movx=b\movx+b\movimento 
			b\movimento=+1 
		EndIf 
		Text b\movx,b\movy-20,b\energia
	
	Next
End Function
Function crea_esplosione()
c.esplosione=New esplosione
c\posizionex=b\movx	;the explosion position is the same as the alien position(considering mid-handle)
c\posizioney=b\movy	;see up :P
End Function
Function esplosione()
For c.esplosione=Each esplosione
If MilliSecs() > c\timer + 50 Then	;i use a "slowdown"of 50 millisecs
	c\timer=MilliSecs() 
	c\frame=( c\frame + 1 ) Mod 10	;i loop trough the 10 frames of the explosion
EndIf
DrawImage explosion,c\posizionex,c\posizioney,c\frame
If c\frame=>9 Then Delete c	;when the frame 10 is displayed the explosion delete itself
Next
End Function
 | 
| 
 | ||
| hey, see if you use the forum tags you can get that in a nice black box, alot better for cutting and pasting. the code is (code) graphics3d 800,600 setbuffer backbuffer() (/code) replace ( with [ and ) with ] which becomes graphics3d 800,600 setbuffer backbuffer() | 
| 
 | ||
| hehehe very thanks! I'm about to ask why some people have black box around the code :P | 
| 
 | ||
| glad to hear you got it working :) | 
| 
 | ||
| Thanks to you all :) |