Hi, there.
I'm newish to Twine, but was interested in trying out a javascript/html interface for a game idea. In a browser, this functions about as I want it to, but after trying a few things in Twine (using
Sugarcane) I'm still getting errors I don't get otherwise. I'm wondering if I'm completely misunderstanding how to integrate these things. Help would be tremendously appreciated.
This was what I was trying to adapt, which works as it should outside of Twine:
<style>
#div1 {width:350px;height:70px;padding:10px;border:1px solid #aaaaaa;}
</style>
<script>
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.getData("text");
ev.target.appendChild(document.getElementById(data));
}
</script>
<p>Drag the image into the box:</p>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<br>
<img id="drag1" src="blue_circle.gif" draggable="true" ondragstart="drag(event)>
...in twine, though, I have tried a few things and keep getting an error that says
"ReferenceError: drag is not defined at HTMLImageElement.ondragstart".Specifically, I tried:
[list type=decimal]
That whole thing, copied to a passage without changes
Cutting out the part between script tags and moving it to another passage with was tagged with "script" (brown).
Cutting out the part between script tags and moving it to an external .js file and loading it with a macro
Trying to make
#2 into macro itself and adding the <<macroname>> to the corresponding passage
...I was never able to get rid of that error, though, nor get the dragging to work, so I think I must be missing something basic about how I'm supposed to use things other than twinecode with twine. Sorry if this is obvious. Still would be super grateful if someone could point me in the right direction, if only so I can get a better handle on how I'm supposed to use javascript with twine. Thanks in advance.
Comments
Window.drag = function (ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
<img>
tag'sondragstart
attribute.EDIT: Attached a working example.
Thank you so much for this; works perfectly and I now feel like a have a better handle on the general case.