Twine Version: 2.5.1
Story Format: sugarcube 2.36.1
Hello,
My example code is below. It works perfectly except it does not go to a passage when conditions are met. It throws an error saying “Engine is not defined”. I’m pretty new to JavaScript and cannot figure out how to get to a new passage using just JavaScript code. Using macros outside of the script just sends the player immediately to a new passage.
The premise is the player is in a dark cave and there are bats flying around. The small bat images will fade in and out pretty quickly in random spots around the screen and the player must click on them before they fade or a giant bat image will briefly appear on screen and the player will take damage. If the player clicks enough bats they should go to one passage, and if they take too much damage they should go to another.
Any help is appreciated, I’ve been combing old forum posts for a few days now and I’ve reached my limit. Cheers.
<div id="image-container">
<img id="image1" src="images/smallBat.png" style="display:none; position: absolute;">
<img id="image2" src="images/smallBat.png" style="display:none; position: absolute;">
<img id="image3" src="images/smallBat.png" style="display:none; position: absolute;">
<img id="damage-image" src="images/bat.png" style="display:none; position: absolute;">
</div>
<<run UIBar.stow()>>
<script>
var image1 = document.getElementById("image1");
var image2 = document.getElementById("image2");
var image3 = document.getElementById("image3");
var damageImage = document.getElementById("damage-image");
var audio = new Audio("music/umph.mp3");
var clickCount = 0;
var damageCount = 0;
function fadeInOut(image) {
var x = Math.random() * (window.innerWidth - image.width) + window.scrollX;
var y = Math.random() * (window.innerHeight - image.height) + window.scrollY;
image.style.left = x + "px";
image.style.top = y + "px";
image.style.display = "block";
image.addEventListener("click", function(){
clickCount++;
image.style.display = "none";
if (clickCount >= 20) {
Engine.play("killedBats");
}
});
setTimeout(function() {
if(image.style.display != "none"){
image.style.display = "none";
damageCount++;
damageImage.style.left = (window.innerWidth - damageImage.width)/2 + "px";
damageImage.style.top = (window.innerHeight - damageImage.height)/2 + "px";
damageImage.style.display = "block";
audio.play();
if (damageCount >= 10) {
Engine.play("killedByBats");
}
setTimeout(function(){
damageImage.style.display = "none";
},100);
}
setTimeout(fadeInOut, 1000, image);
}, 1000);
}
setTimeout(fadeInOut,500, image1);
setTimeout(fadeInOut,1000, image2);
setTimeout(fadeInOut,18000, image3);
</script>
<<audio caveBats loop play>>