Okay, so I have managed to modify and tweak some existing javascript to achieve an effect I would like to use in Twine as a custom macro (discussed in this topic
lhere). I have tried looking at the tutorial posted
here but I'm kind of confused as it looks like I need to alter the existing javascript to get it working in Twine?
This is the current code:
$('.inlinechoice').on('click','.question', function(e) {
var $this = $(this),
$id = $this.prop('id'),
$class = '.' + $('.answer-' + $id).prop('class').replace('hide', '');
$('.answerblock').hide();
$('.answer-' + $id).show();
$('div[class*=answer]').not($class).hide();
});
Codepen version here:
http://codepen.io/anon/pen/HkCgGBasically it allows the user to set up a number of clickable links, and when the user clicks on one of the links, the links disappear and an appropriate response shows in its place.
I think the code should look something like this:
try {
version.extensions['fakechoice'] = { major:1, minor:0, revision:0 };
macros['fakechoice'] = {
handler: function(place, macroName, params, parser) {
[...]
};
} catch(e) {
throwError(place,"recall fakechoice Error: "+e.message);
}
But with the javascript sitting in the [...] somehow. Also, I'm not sure if I need more than one macro, i.e. one to set up the inlinechoice, and another to set up the answerblock.
I'm also unsure as to whether I also need to include
//requires jquery
//requires Modernizr
but when I share the javascript from codepen, I can see it makes reference to both libraries.
Sorry if this seems similar to my other topic, but I suppose this is more focused on actually using javascript in Twine, so hopefully it's okay to make the post. Advice and thoughts appreciated as always, thanks in advance!
Comments
For future reference, I found out how to do this here. Basically, to call custom javascript, you need to save the script as a .js file in the same folder as your Twine game, then add this macro as a script passage. Then, when you want to call your javascript in Twine, you just type into a passage: In my case I also went into the 'Story>Special Passages>StorySettings menu and set both jQuery and Modernizr to on.
Apologies if this is repeating common knowledge, but I couldn't find this in the wiki/forum or googlegroups, nor was it obvious anywhere else.
It'd still be nice to change this to a proper twine macro, as ultimately I'm not convinced my javascript is any less clumsy than the combination of using Leon's revision macros for the task but it's doing what I want it to, so that's pretty cool.
Yeah, some of that is news to me. Good post!
Modernizr is used almost exclusively for HTML5 and CSS3 cross-browser compatibility. It's not required in this case, let me assure you.
I've added the script to a script passage (brown header) and called it fakechoice.
Then I've added <<display fakechoice>> and an arrow joins the game passage to the script passage. But it doesn't seem to work (basically I can't click the text and get the response).
https://dl.dropboxusercontent.com/u/11379019/Twine/scripttest.tws
I guess I'm doing something wrong?
Edit: Made more tweaks to the code now, which will pick up clicked choices in order. Working codepen here.