Twine Version: 2.7.1
SugarCube Version: 2.36.1
I am having an issue getting Twine to recognize my JavaScript Function. I have looked at similar posts on the board and I’ve tried using the “setup” built into SugarCube. It’s stillnot working. I get reference errors stating that “setup is not defined”.
I’m trying to create an escape room type game where the player has to answer math questions to escape “Math Mansion”. (I’m a Math teacher btw.)
I want a reusable function in javascript that checks the answers to systems of equations but I keep getting the *** function not defined error. I’m currently using Twine 2.7.1 and SugarCube 2.36.1. (I have attempted several iterations and several builds including Harlowe and SnowMan with varying function names)
The passage where I attempt to call the javascript is:
::Solution1
X-coordinate: <input type="text" id="userAnswerX" placeholder="Enter x">
Y-coordinate: <input type="text" id="userAnswerY" placeholder="Enter y">
<button onclick="setup.checkAnswer(setup.correctX1, setup.correctY1, 1)">Submit</button>
And the “Story JavaScript” is as follows:
//Declare and initialize user input variables for x and y
setup.correctX1 = 2;
setup.correctY1 = 0.6;
setup.score = 0;
setup.penalty = 0;
setup.attempts = 0;
// Functions defined below
setup.checkAnswer = function checkAnswer(correctX, correctY, qID) {
do {
// Get the user's input for x and y and convert to floats
var userInputX = parseFloat(document.getElementById("userAnswerX").value);
var userInputY = parseFloat(document.getElementById("userAnswerY").value);
// Define a tolerance for comparison to handle decimal precision
var tolerance = 0.01; // You can adjust this value as needed
// Check if both x and y are within an acceptable range
if (Math.abs(userInputX - correctX) < tolerance && Math.abs(userInputY - correctY) < tolerance) {
setup.attempts = 0; // Reset attempts on correct solution
setup.score++; // Add a point to questions answered correctly
// If correct, guide to the next passage
State.history.go("Jbox");
}
else {
setup.attempts++; // Add one to the number of failed attempts
setup.penalty++; // Add one to the total number of incorrect solutions
// If incorrect, display a message
alert("Sorry, that's not correct. Please try again.");
// Clear the input fields (optional)
document.getElementById("userAnswerX").value = "";
document.getElementById("userAnswerY").value = "";
}
}while (attempts < 5);
// If the player exceeds the allowed attempts, you can handle it accordingly
switch (qID) {
case 1:
alert("You've exceeded the allowed attempts. A trap door has dropped you into a dungeon.");
State.history.go("Dungeon");
break;
case 2:
alert("You've exceeded the allowed attempts. Sadly, your fate has been sealed.");
State.history.go("Death");
break;
}
}
I have some programming experience but I’m very out of practice.
Thanks for any help.