typewriter.. text effect.

Blitz3D Forums/Blitz3D Beginners Area/typewriter.. text effect.

Well, I'm trying to code a type writer effect for my text. But I've ran into a small error:

parameter must be greater then 0

Once I succeed at printing all my text letter by letter on the screen if I add in a timer... I think I could create the type writer effect... What am I doing wrong guys?

Function cText(txt$,x#,y#)
	txtlen#=Len(txt$)
	
	x2#=x#
	
	For t = 0 To txtlen#
	
		txt2$=Mid(txt$,t,1)
		Text x2#,y#,txt2$,False,False
		x2#=x#+10
	
	Next
	
End Function


For t = 0 To txtlen#

should be,
For t = 1 To txtlen#

Just a simple mistake.

I just noticed,
x2#=x#+10

should be (I think),
x2#=x+((t*10)-10)


Heres how i would do it:

Function ComputerType(tex$,x,y,dely)

For e=1 To Len(tex$)

Text x,y,Mid$(tex$,e,1)

Delay dely

x=x+8

Next

End Function

;!@#$!@#%!^!#^#!^!^#$^#@%^#^!
;!@#%$!@%!@#$%!@#$%!@#$%!%!%!
;@#!$%@%!$%!$#%!#%!@#%!@%!#$%

;Program example:

ComputerType("Hello HAHAH i'm talking to you!!",0,0,100)


Delay 5000


the function:

ComputerType(Textyouwanttoenter$,X start pos.,ypos.,the delay on typeing 1000=1second)

you could use write instead and get rid of the updating x+x+8

But then you cant control the Y and X pos.

I came up with this example for ya to study :)

Basicly gives the effect your talking to the computer..

Function ComputerType(tex$,x,y,dely)

For e=1 To Len(tex$)

Text x,y,Mid$(tex$,e,1)

Delay dely

x=x+8

Next

End Function

;!@#$!@#%!^!#^#!^!^#$^#@%^#^!
;!@#%$!@%!@#$%!@#$%!@#$%!%!%!
;@#!$%@%!$%!$#%!#%!@#%!@%!#$%

;Program example:

Graphics 800,600,16,2

ComputerType("Hello HAHAH i'm talking to you!!",0,0,100)
ComputerType("HAHAHA i'm still talking to you!!!",0,20,100)
ComputerType("You still cant do anything about it!!",0,40,100)
ComputerType("Still talking................................",0,60,100)
ComputerType("I bet your getting bored about now eh? TO BAD!!",0,80,100)
ComputerType("Lets try a input... type in your name!",0,100,100)
Print
Print
Print
Print
Print
Print
Print
Inpt$=Input()
Computertype("I'm talking to "+Inpt$+" now, How are you doing "+Inpt$+"?",0,140,100)
Print
Print
howare$=Input()
If howare$="bad"
computertype("That sucks. I'll give you 5 seconds till i turn off",0,180,100)
ElseIf howare$="good"
computertype("good for you. i'll give you 5 seconds till i turn off",0,180,100)

Else
computertype("Well thats a dumb answer, Bye bye i don't like you.",0,180,100)
End
End If


Delay 5000


Have fun.

does delay... delays the whole application? cuz well when the typing is done if i want things to move they wont

Yes, 'Delay' delays everything.

i use "Delay" simply to make a "typing" effect, because if i didnt, everything would instantly appear, the delay at the end is to make the program stay up for 5 seconds, though it is a bit of a problem when you use the comand and want somthing else happening at the same time... because it delays everything untill its done typing its given string..

soo custom timer for delay then? :o

Graphics 640,480,0,2
SetBuffer BackBuffer()

Type TypeText

	Field X,Y,Letter$,Time#
	
End Type

Function TypeText (X,Y,Txt$,Space#=8,Wait#=10)

Local TT.TypeText,Time#,Gap#,A

For A = 1 To Len (Txt)

	Time	= Time + Wait
	Gap		= Gap + Space

	TT.TypeText = New TypeText
	TT\X		= X + Gap
	TT\Y		= Y
	TT\Letter	= Mid (Txt,A,1)
	TT\Time		= Time
	
Next

End Function

Function UpdateTypeText()

Local TT.TypeText

For TT.TypeText = Each TypeText
	
	TT\Time = TT\Time - 1
	
	If TT\Time <= 0
		
		Text TT\X,TT\Y,TT\Letter,True,True
			
	EndIf
	
Next

End Function

Local TxtA$ = "Hello, this is how to make typewriter text :)"

TypeText (320-StringWidth(TxtA)/2,240,TxtA,8,10)

While Not KeyDown (1)

	Cls
	
	UpdateTypeText()

	Flip
	
Wend

End


Here's a better example. I made these functions ages ago. You need to play about with the 'Tolerance' value to get some sentences type out properly. You could probably find a word wraping function to do it better tho :)

Graphics 640,480,0,2
SetBuffer BackBuffer()

Type Letter

	Field Letter$,Xpos,Ypos,Time#,Sound
	
End Type

;--------------;
; Type Letters ;----------------------------------------------------------
;--------------;

Function TypeText (Xpos=0,Ypos=0,MaxX=100,MaxY=100,Texts$="",Space=10,Height=12,Time=5,Tolerance=10,Sound=0)

Local LR.Letter,A,L

For A = 1 To Len (Texts)

	LR.Letter 	= New Letter
	LR\Letter 	= Mid (Texts,A,1)
	LR\Xpos		= Xpos + (L*Space)
	LR\Ypos		= Ypos
	LR\Time		= A*Time
	LR\Sound	= Sound

	L = L + 1

	If LR\Letter = Chr (13) Or LR\Letter = Chr (10)

		LR\Xpos = Xpos
		LR\Ypos = Ypos + Height
		LR\Letter = " "

		Ypos	= Ypos + Height
		L 		= 1
			
	EndIf
	
	If LR\Xpos >= (MaxX-Tolerance)
	
		If LR\Letter = " "
	
			LR\Xpos = Xpos
			LR\Ypos = Ypos + Height

			Ypos	= Ypos + Height
			L 		= 1
			
		EndIf
		
	EndIf

	If LR\Xpos >= MaxX
	
		LR\Xpos = Xpos
		LR\Ypos = Ypos + Height

		Ypos	= Ypos + Height
		L 		= 1
		
	EndIf
			
	If LR\Xpos = Xpos
	
		If LR\Letter = " "
		
			Delete LR
			
			L = 0
			
		EndIf

	EndIf
		
Next

End Function

;------------------;
; Draw all Letters ;------------------------------------------------------
;------------------;

Function DrawLetters()

Local LR.Letter

For LR.Letter = Each Letter

	LR\Time = LR\Time - 1

	If LR\Time = 0 And LR\Sound <> 0
	
		PlaySound LR\Sound
		
	EndIf

	If LR\Time <= 0

		Text LR\Xpos,LR\Ypos,LR\Letter
		
	EndIf
	
Next

End Function

;--------------------;
; Remove all Letters ;----------------------------------------------------
;--------------------;

Function DeleteLetters()

Local LR.Letter

For LR.Letter = Each Letter
	
	Delete LR
		
Next
	
End Function

;---------;
; Example ;---------------------------------------------------------------
;---------;

; Text from Wikipedia :)
;
; Xpos		= Xpos of text start
; Ypos		= Ypos of text start
; MaxX		= Width of text box
; MaxY		= Height of text box
; Texts		= Text to type
; Space		= Space between letters
; Height	= Height of each line
; Time		= Delay between letters
; Tolerance = Max word size from end of line (?)
; Sound		= Sound to play for each letter

Local Txt$ = "The Waterboys are a band formed in 1983 by Mike Scott. The band's membership, past and present, has been composed mainly of musicians from Scotland and Ireland. London, Dublin, Spiddal, New York and Findhorn have all served as a home for the group."+Chr(13)+Chr(13)+"The band has played in a number of different styles, but most often their music can be described as a mix of Celtic folk music with rock and roll, or folk rock. After ten years of recording and touring, the band dissolved in 1993 and Scott pursued a solo career."+Chr(13)+Chr(13)+"The band reformed in 2000, and continues to release albums and tour worldwide. The early Waterboys sound was dubbed `The Big Music' after a song on their second album, A Pagan Place. This musical style was described by Scott as `a metaphor For seeing God's signature in the world'. Their songs, largely written by Scott, often contain literary references And are frequently concerned with spirituality."+Chr(13)+Chr(13)+"Both the group and its members' solo careers have received much praise from both rock and folk music critics, but The Waterboys as a band has never received the commercial success that some of its members have had independently."

TypeText (20,20,600,440,Txt,10,12,5,50,0)

While Not KeyDown (1)

	Cls
	
	DrawLetters()

	Flip
	
Wend

DeleteLetters()

End


Hope it helps :)