Can yo tell me how link the keybord button to the game button? To conditionally “Space”, press on <<button>><</button>>
or <button></button>
.
I tried to do this, and i have the space bar pressed only after clicking on the <<button>>
or <button>
.
Each mouse click works correctly, doing the right action. The space bar does not work without clicking the mouse, as if does not work, and when you click on the button with the mouse 1 time it starts working correctly.
How to make the space bar work immediately without selecting an element with a mouse click?
I did it with macro <<button>>
and when i fail, i try do it in pure JS:
<div class="passageText">
<span id="passageText"><<=$texts[$currentIndex]>></span>
<div class="buttonStoryText">
<button id="buttonStoryText">Next</button>
</div>
</div>
<<done>>
<<script>>
let buttonStoryText = document.getElementById('buttonStoryText');
let currentIndex = 0;
const sv = State.variables;
let texts = [
"Первый текстовый блок. Это начало вашей истории.",
"Второй текстовый блок. Сюжет развивается.",
"Третий текстовый блок. Сюжет продолжается.",
"Четвертый текстовый блок. Приближение к развязке.",
"Последний текстовый блок. Конец вашей истории."
];
buttonStoryText.addEventListener('keydown', function(e) {
sv.currentIndex++;
if (e.keyCode === 32 && document.activeElement === buttonStoryText) {
if (sv.currentIndex < texts.length - 1) {
replaceElementContent('passageText', texts[sv.currentIndex]);
}
}
});
buttonStoryText.addEventListener('click', function(e) {
sv.currentIndex++;
if (currentIndex < texts.length) {
replaceElementContent('passageText', texts[sv.currentIndex]);
} else {
sv.currentIndex = texts.length - 1;
}
});
function replaceElementContent(elementId, newContent) {
var element = document.getElementById(elementId);
if (element) {
element.innerHTML = newContent;
}
}
<</script>>
<</done>>
PS: one more question: When we clicked on the button and the index of the array with the text changed to any other than 0, showing our text, we make a transition along the segment. There and back. This text remains in the output field, which is good. When you save the game and load it, this text is correctly displayed in the block (for example, with the index 2 of array). But this only works when you made a pass to the passage and back before saving. If you save it immediately after clicking on the output button, but without leaving the passage, the text will not be saved. Question: “How do I make sure that the text is saved at any time?”