Object "flicker"when deleted

Blitz3D Forums/Blitz3D Beginners Area/Object "flicker"when deleted

Hallo to all.
I have a piece of code that allow me to create object and move them across the screen following a path that i load from a file.
Those object fire at the player that is controlled by the mouse.
Each object (player,enemy bullet,enemy,player bullett) are a separate type.
And when an object is deleted i play a small explosion anim.
My problem is that when i shoot a enemy all is ok,but when the enemy axplode i see ALL other object of the same type flicker (like for a fraction of a second they disappear and appear)!
If explode an alien...ALL alien flicker(it seems that the flicker don't afect other types...).
I use a function to move all alien,a separate function to move enemy bulle,and so on for the other movement.
Each movement is in this manner "for a.alieno = each alieno".
The code for the collision is inside the bullet move function (if i put elsewere the collision don't work...don't know why)and works in this manner "for a.bullet = each bullet------move bullet for a.alieno = each alieno----calculate collision----if collision delete a----exit (to prevent error)----next----next".
Someone know if the flicker is caused in my method of doing collision?(in every demo that i made with collision i see this effect....sometimes smaller and somethimes [like with this demo...] very annoying).
Thanks.

Are you in 2D or 3D?

Sorry:it's a 2d demo,with 2d graphics ecc ecc. and a simple background (no scrolling).


for a.bullet = each bullet------move bullet for a.alieno = each alieno----calculate collision----if collision delete a----exit (to prevent error)----next----next".



Valgar, can you post the actual code this refers to. I have my suspicions about it. :)

Sounds to me if you're using 2D that you're drawing to the wrong buffer, or flipping at the wrong time.

Ok i post the code,just don't care of the italian comment.
Here it comes:
Function muovi_proiettile()
	For missile.bullet=Each bullet
		missile\durata=missile\durata-1
		missile\positionx=missile\positionx
		missile\positiony=missile\positiony-missile\velocita
		counter=counter+1
		DrawImage (missile\immagine,missile\positionx,missile\positiony)
		If missile\durata=<0		;se il missile si e spostato di TOT unita cancellalo(per liberare memoria e tempo processore...)
			Delete missile
			Exit
		EndIf
	
;**************************
;inizio codice relativo alle collisioni,deve stare all'interno della funzione che muove i proiettili
		For a.alieno =Each alieno
			If ImagesCollide(bullet_image,missile\positionx,missile\positiony,0,ObjectFrames(fotogramma),a\posizionex,a\posizioney,0)
				a\vitalita=a\vitalita-missile\potenza	;quando il proiettile collide con l'alieno cala l'energia di esso 
				Delete missile
				Exit		;esce se i type sono cancellati....
			EndIf
		Next
;**************************
;fine codice collisioni
	Next
End Function


Function muovi_alieno()
For a.alieno=Each alieno
	If a\vitalita=<0 
		crea_esplosione(a\posizionex,a\posizioney)
		hi_score=hi_score+a\punteggio
		Delete a
		Exit
	EndIf
	Select a\percorso
		Case 1 path_movement()
		Case 2 strightMov()
	End Select
	If jetdistrutto=False
		casuale=Rnd(0,200)	;generazione di un numero casuale
		If casuale=2 Then create_bullet(a\posizionex,a\posizioney,MouseX(),MouseY(),200,25)	;se e 2
		;la partenza del proiettile nemico sono le coordinate del nemico stesso
	EndIf
Next
End Function
Function read_path()				;funzione che carica da file i dati dei nodi
	file=ReadFile("path_points.txt")					;apro il file creato precedentemente per la lettura
	If file=0 Then Goto uscita			;se non ho aperto nessun file o se il file non esiste esci dalla funzione
	pointer=0							;puntatore al movimento nel file...aumenta di 4 in 4 siccome per un valore nei
									;file vengono allocati automaticamente 4 byte di dati (numeri,stringhe ecc ecc)
	pointer2=0							;puntatore al valore che,incrementando,fa si che leggo i valori nell'array ciclicamente
	For x=1 To max_path_points						;scorre tutti e 10 i valori
		SeekFile (file,pointer)				;mi muovo all'interno del file cosi
		a\pathx#[pointer2]=ReadFloat (file)	;leggo il valore x ,un float-number dentro il primo valore nell'array
		pointer=pointer+4					;mi sposto al prossimo dato dentro il file
		a\pathy#[pointer2]=ReadFloat (file)	;leggo il valore y
		pointer2=pointer2+1					;passo ai prossimi due valori nell'array
		pointer=pointer+4					;mi sposto ancora al prossimo dato nel file
	Next
	CloseFile (file)					;chiudo il file
	.uscita
End Function
Function crea_esplosione(x,y)
c.esplosione=New esplosione
c\posizX=x	;the explosion position is the same as the alien position(considering mid-handle)
c\posizY=y	;see up :P
c\grandezza=1
End Function
Function esplosione()
For c.esplosione=Each esplosione
	Select c\grandezza
		Case 1	;small
			If MilliSecs() > c\timer + 50 Then	;i use a slowdown of 50 millisecs
				c\timer=MilliSecs()
			c\frame=(c\frame+1) Mod 15	;i loop trough the 7 frames of animation:this is a smaller explosion
			EndIf
			DrawImage explosion1,c\posizX,c\posizY,c\frame
			If c\frame => 14 Then Delete c
	End Select
Next
End Function


I think that those 2 function is what contain the "bug".

I've only had time for a quick look but I think you might be using 'Exit' incorrectly, for one thing. Anyway, I'll try and have another look tonight. Someone else will probably have sorted it for you ny then, though. :)

Thanks,i don't know what to do for this,so i make other thing :)
Eventually i post the whole code,but i think that the other function are correct (they make simple thing after all...).

Well, I've had a go at changing things that looked problematic but it's very difficult to debug code when you don't have the whole source and it's in another language. :)
Anyway, give it a go - not sure anything I changed would solve the flickering, though.

Function muovi_proiettile()

	For missile.bullet = Each bullet
		missile\durata = missile\durata-1
		
		If missile\durata=<0
			Delete missile
		Else
			;missile\positionx = missile\positionx ;This does nothing.
			missile\positiony = missile\positiony-missile\velocita
			;counter = counter+1 ;This doesn't appear to be used.

			For a.alieno = Each alieno
				If ImagesCollide(bullet_image,missile\positionx,missile\positiony,0,ObjectFrames(fotogramma),a\posizionex,a\posizioney,0)
					a\vitalita = a\vitalita-missile\potenza
					Delete missile
					Exit
				EndIf

				DrawImage (missile\immagine,missile\positionx,missile\positiony)
			Next		
		EndIf		
	Next

End Function


Function muovi_alieno()

	For a.alieno = Each alieno
		If a\vitalita=<0 
			crea_esplosione(a\posizionex,a\posizioney)
			hi_score = hi_score+a\punteggio
			Delete a
		Else
			Select a\percorso
				Case 1 path_movement()
				Case 2 strightMov()
			End Select
			
			If jetdistrutto = False
				casuale = Rnd(0,200)	;generazione di un numero casuale
				If casuale=2 Then create_bullet(a\posizionex,a\posizioney,MouseX(),MouseY(),200,25)	;se e 2
				;la partenza del proiettile nemico sono le coordinate del nemico stesso
			EndIf
		EndIf
	Next

End Function


Function read_path()				;funzione che carica da file i dati dei nodi

	file=ReadFile("path_points.txt")					;apro il file creato precedentemente per la lettura
	If file=0 Then Goto uscita			;se non ho aperto nessun file o se il file non esiste esci dalla funzione
	pointer=0							;puntatore al movimento nel file...aumenta di 4 in 4 siccome per un valore nei
									;file vengono allocati automaticamente 4 byte di dati (numeri,stringhe ecc ecc)
	pointer2=0							;puntatore al valore che,incrementando,fa si che leggo i valori nell'array ciclicamente

	For x=1 To max_path_points						;scorre tutti e 10 i valori
		SeekFile (file,pointer)				;mi muovo all'interno del file cosi
		a\pathx#[pointer2] = ReadFloat (file)	;leggo il valore x ,un float-number dentro il primo valore nell'array
		pointer = pointer+4					;mi sposto al prossimo dato dentro il file
		a\pathy#[pointer2] = ReadFloat (file)	;leggo il valore y
		pointer2 = pointer2+1					;passo ai prossimi due valori nell'array
		pointer = pointer+4					;mi sposto ancora al prossimo dato nel file
	Next

	CloseFile (file)					;chiudo il file

	.uscita

End Function


Function crea_esplosione(x,y)

	c.esplosione = New esplosione
	c\posizX = x	;the explosion position is the same as the alien position(considering mid-handle)
	c\posizY = y	;see up :P
	c\grandezza = 1

End Function


Function esplosione()

	For c.esplosione = Each esplosione
		Select c\grandezza
			Case 1	;small
				If MilliSecs() > c\timer + 50 ;i use a slowdown of 50 millisecs
					c\timer = MilliSecs()
					c\frame = (c\frame+1) Mod 15	;i loop trough the 7 frames of animation:this is a smaller explosion
				EndIf

				If c\frame => 14
					Delete c
				Else
					DrawImage explosion1,c\posizX,c\posizY,c\frame
				EndIf
		End Select
	Next

End Function


Well greath it works!
I have made a simple cut&paste and deleted the old function and i don't see the flicker!!!
Just some info...i use the same manner of doing things in all my code....how do you have indiduated the problem?
Last but not least.....where's the problem? O_O'

Hey, glad it works. :)

As I said before, the main cause was using Exit when you didn't need it. For example, your original muovi_proiettile() function:

Function muovi_proiettile()

	For missile.bullet=Each bullet
		missile\durata=missile\durata-1
		missile\positionx=missile\positionx
		missile\positiony=missile\positiony-missile\velocita
		counter=counter+1
		DrawImage (missile\immagine,missile\positionx,missile\positiony)
		If missile\durata=<0
			Delete missile
			Exit ;<-------- error!
		EndIf
	
		For a.alieno =Each alieno
			If ImagesCollide(bullet_image,missile\positionx,missile\positiony,0,ObjectFrames(fotogramma),a\posizionex,a\posizioney,0)
				a\vitalita=a\vitalita-missile\potenza
				Delete missile
				Exit
			EndIf
		Next
	Next

End Function


I have marked an Exit command as an error. This is wrong because it causes the 'For missile.bullet = Each bullet' loop to be exited whenever a missile's 'durata' reaches zero, meaning all the remaining bullet's that come after it in the type list don't get processed OR drawn for that frame. This constantly causes some bullets to get drawn on some frames, but not on others, hence the flickering.

I understand now!
I have read MUCH better the online help and i have read that "exit" break a for----next loop immediately;so if i used exit even inside of a "if---endif" i exit the WHOLE "for---next" instead.
Exit must be used when i whant to exit "totally" a function,and because i use a for next at the beginning ov most of my function....this will cause the other object to not display themselves at that frame.
I think that i need much more experience :)
Many thanks

Argh i have see another "bug" :P
With your modification now the object don't flicker but....if no enemy are on screen the jet don't shoot!but if i create an enemy immediately after i have shooted...the bullet start it's position from where it was when he isn't on screen -_-'
I must check the creation function to see the error.

Sorry for bothering,i have individuated the error.
Usually i use function in this manner:make all calculation of position and AFTER the calculation i display the object....but in this manner if no enemy are onscreen no object is created because the display bullet function is AFTER the condition check...so if the condition is positive...no bullet displayed -_-
I have simply put the drawimage as one of the first command and voila,all go well now!