Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Effect: Typewriter with sounds and carriage return

I'm using SugarCube and Twine 1.4.2. I have the Typewriter t8n script working fine--I'll include it below for reference. What I've been scouring the forums and docs trying to figure out is how I could:
1. add a brief typing sound to each printed character
2. add a pause at the end of each printed line.

Coincidentally, the below typewriter transition also seems to botch up the effective use of <<timedcontinue>> macro and its ilk in a passage that is using the below post render script.

Any thoughts?

Thanks!


:: Typewriter [script]
(function(){
	postrender.typewriter = function (b) {
	  	if(this.tags){
    		var r=new RegExp("t8n.typewriter.([0-9]+)(?:[^0-9]|$)","g");
    		var t=r.exec(this.tags.toString());
    		if(t){ 
    			typeout(b,t[1]+0); 
    		}
		}
  		return b;
	};

  	var typeout=function(c,t){
	    var Furl=function(current){
	      	this.n=current;
	      	this.out=false;
	     	this.data=current.nodeValue;
	      	current.nodeValue="";
	      	this.kids=[];
	      	var cn=current.childNodes;
	      	if(current.style && current.style.display=="none"){
				return;
		  	}
		  	while(cn.length>0){
				var f=new Furl(cn[0]);
				current.removeChild(cn[0]);
				f.out=true;
				this.kids.push(f);
			}
		};
		
		var nodes=new Furl(c);

		var unfurl=function(furled,d){
			var n=furled.n;
			if(furled.out){
				d.appendChild(n);
				furled.out=false;
			}
			if(furled.data){
				n.nodeValue+=furled.data[0];
				furled.data=furled.data.slice(1);
				return true;
			}
			for(var j=0;j<furled.kids.length;j++){
				var ret=unfurl(furled.kids[j],n);
				if(ret){
					return true;
				}
			}
			return false;
		};

		var title=state.active.title;
		
		var intr=setInterval(function(){
			if(state.active.title==title&&unfurl(nodes,null)){
				return;
				}
			clearInterval(intr);
		},t);
	};
}());

Comments

  • edited May 2015
    I don't know how to do exactly what you're asking, but another method would be:
    T<<playsound "typing">><<timedcontinue 20ms>Y<<playsound "typing">><<timedcontinue 20ms>P<<playsound "typing">><<timedcontinue 20ms>I<<playsound "typing">><<timedcontinue 20ms>N<<playsound "typing">><<timedcontinue 20ms>G<<playsound "typing">>
    
    <<timedcontinue 200ms>>
    New line
    

    That's a typewriter effect plus carriage returns without the above script, albeit a bit more fiddly, but useful enough for very short segments of typing.

  • Thanks but I'm typing out entire passages so that won't be feasible. The post render function is the most likely place to handle this but I'm not sure:
    -where to check for a newline
    character,
    -best way to manage two different types of setInterval
    -How to call sugarcube audio functions from within JavaScript


    Sent from phone
  • You might want to look into a pure JavaScript audio player like Howler.js.
  • Brad wrote: »
    -where to check for a newline character
    I assume you mean in the original passage text? If so, you don't. The content buffer passed into postrender task callbacks is the post-wikified version, there are no line termination sequences left.

    That said, you could check for <br> elements instead, which amounts to the same thing, really.

    Brad wrote: »
    -best way to manage two different types of setInterval
    Details?

    Brad wrote: »
    -How to call sugarcube audio functions from within JavaScript
    First. If you only need the audio to trigger at the start of a passage, just use the macros normally. They should work fine, even with your script, since they're processed by the Wikifier before your script ever sees the output buffer.

    Second. You don't, currently—at least, not directly anyway*. I suppose that's something I can add to the TODO list.

    * I assume you want this for the typing sounds. So, though it's a bit hacky, you could do so via Wikifier calls. For example:
    (new Wikifier(null, "<<audio …>>"));
    
Sign In or Register to comment.