Hey everybody,
lacking deeper knowledge of JavaScript I am facing a major problem here: For an educational game I want to include passages, in which the users have to drag and drop words into the right order. I found a script which covers about 90% of what I need, but I don´t know how to adapt it to make it work with twine / sugarcube.
I hosted an example here:
http://deutschdetektiv.net/exp/put_words_in_order.html
Could anybody help making this work in Twine/SC?
I´d need the following changes:
* instead of comparing the user result with the right answer and then displaying the <span id="correct!">, I´d like the user to drag and drop the words until they think they are in the correct order.
* On clicking a button, the script shall compare the user sentence with the correct sentence (as stored in var content) and pass a boolean variable to Twine, which can be further processed within the passage.
* Remove the <span class='exclamation' data-info='"+solution+"'>!</span>. I tried to do so myself, but the script stopped working.
I know this is a huge favor to ask, I can only hope that the feature might be handy for other users as well.
Comments
best, euba
I thought I give it another try and paste the javascript and html here. I got the javascript issue figured out, I now have a "var corrects" which is either true or false depending on how the user dragged the words.
So here we go for the html:
The drag.js can be found here: http://pastebin.com/jUmqCKJP
Could anybody help me make this work in SC 2.6.2 in Twine 2? I'd like to be able to put the entire javascript in the story's JS-section and use the function in several passages.
Again the link to the example code: http://deutschdetektiv.net/exp/put_words_in_order.html
So my questions are:
1. I understand Sugarcube is based on jQuery. The above page also loads jQueryUI, so I thought I could just copy-and-paste the jQueryUI into my story´s javascript. Didn't work.
Which would be the best way to use jQueryUI in my story?
2. Where do I go from there? I guess I need to turn the javascript code (http://pastebin.com/jUmqCKJP) into a macro, so I can call it from the passages want to use the drag-and-drop-feature?
Any advice?
The files must be in the same domain.
In the passage:
This in the Story Javascript:
I changed the code of the test button:
It is important to add the function to the window.top
I tested it with SugarCube 1.0.34.
On the other hand, the word ordering script. It pains me how many issues that script has. I fixed the most egregious syntax issues and a few other things—it's still pretty terrible though—as well as wrapping it up for use in a story format. The interface changed a bit. The cleaned up version was pasted into the Story JavaScript—after jQuery UI.
I'll attach an example Twine 2 project archive to this post—just import in into Twine 2.
PS: It's current set to use SugarCube (v1), so you'll need to change its story format to SugarCube (v2), if that's what you're using.
Could I bother you for some further guidance? For repeated use and smooth integration in the existing SC2 variables, I thought it best to turn the entire thing into a widget, e.g.
my go looks like this:
Twine says, "required #container element is missing", so I reckon it has to be prerended?
That said, you cannot base the status of static elements on an interactive one and it expect it to work. In other words, the following: Is never going to do what you want, because it only gets evaluated at passage render time. The dragging script is interactive and based on player input, which only happens well after passage rendering has completed.
There are other issues as well, but they're of lesser import.
That´s when I got the error about the container element.
Concerning the other thing, thx for the explanation. What I am having trouble with (among a lot of other things) is building a bridge between SugarCube and javascript.
In your working example you used an html button element to "alert" the return of a javascript function. Is there a way to do this from within a SugarCube <<button>> element? Like calling a javascript function, have the $variable set there and further process it with SugarCube only?
I'm attaching another example Twine 2 project archive to this post—just import in into Twine 2.
The included widget shows an example of <<button>> usage—it's also been updated for SugarCube v2.
I also made a few changes to the dragging script in this example—e.g. removing all references to the #correct container, since the code never modified it for whatever reason.