<<timed macro>> Autoscroll Twine 2 Sugarcube

0 votes
asked 5 days ago by aagames (220 points)
Hi all,

Another question: When I use the <<timed>> and <<next>> macros in Twine 2 Sugarcube 2, how do I get the screen to automatically scroll down so people can read the next passage?

Thanks!

1 Answer

0 votes
answered 4 days ago by TheMadExile (42,760 points)
selected 4 days ago by aagames
 
Best answer

Try the following <<scrolldown>> macro.

JavaScript: (goes in Story JavaScript)

/*
	<<scrolldown [duration]>>

	duration : (optional) The length of time to animate the scroll, as the
	           strings 'fast' or 'slow' or a valid CSS time value (e.g. 5s
	           and 500ms).  The default duration is 'slow'.
*/
Macro.add('scrolldown', {
	handler : function () {
		var target   = document.scrollingElement || 'html,body';
		var duration;

		if (this.args.length > 0) {
			switch (this.args[0]) {
			case 'fast':
			case 'slow':
				duration = this.args[0];
				break;
			default:
				try {
					duration = Math.max(Engine.minDomActionDelay, Util.fromCssTime(this.args[0]));
				}
				catch (ex) {
					return this.error(ex.message);
				}
				break;
			}
		}
		else {
			duration = 'slow';
		}

		setTimeout(function () {
			$(target).animate({
				scrollTop: $(document).height() - $(window).height()
			}, duration);
		}, Engine.minDomActionDelay);
	}
});

Usage:

First block of text.
<<timed 2s>>
Next block of text.\
<<scrolldown>>
<<next>>
Next block of text.\
<<scrolldown>>
<<next>>
Next block of text.\
<<scrolldown>>
<</timed>>
Et cetera.

 

commented 4 days ago by aagames (220 points)
This is great! Thank you!

The one thing is, it only works when I put the macro after the sixteenth line of text (which is the bottom of the screen). Otherwise, it doesn't scroll. However, if I can remember that, I don't think I'll have any problems.
commented 4 days ago by TheMadExile (42,760 points)

That's what the macro does.  It scroll's to the current bottom of the page, so any content which was rendered below/off the bottom of the screen will be scrolled up into view.  If there's no content below the current view, then there's nothing for it to do.

As to when to use it.  Since people have different sized monitors and/or devices, I'd recommend against using it only after some line that looks appropriate for your setup, because it likely won't be correct for some others.

commented 2 days ago by aagames (220 points)
Is there any way I could combine <<scrolldown>> with the <<timed>> and <<next>> macros? That would save me a lot of typing and make sure that it executes whenever necessary.
Welcome to Twine Q&A, where you can ask questions and receive answers from other members of the community.

You can also find hints and information on Twine on the official wiki and the old forums archive.

See a spam question? Flag it instead of downvoting. A question flagged enough times will automatically be hidden while moderators review it.
...