我正在制作一款游戏,根据玩家所做的选择,它将显示新的文本供选择。我正在尝试将图像与该文本连接起来,以便它们能够适应每个选择。现在,我的文字可以正常工作,但我的图像无法显示。
我在这里做错了什么?
<script type ="text/javascript">
const textElement = document.getElementById('text')
const optionButtonsElement = document.getElementById('option-
buttons')
let state = {}
function startGame() {
state = {}
showTextNode(1)
}
function showTextNode(textNodeIndex) {
const textNode = textNodes.find(textNode => textNode.id ===
textNodeIndex)
textElement.innerText = textNode.text
document.getElementById('img').src=textNode.img
while (optionButtonsElement.firstChild) {
optionButtonsElement.removeChild(optionButtonsElement.firstChild)
}
textNode.options.forEach(option => {
if (showOption(option)) {
const button = document.createElement('button')
button.innerText = option.text
button.classList.add('btn')
button.addEventListener('click', () =>
selectOption(option))
optionButtonsElement.appendChild(button)
}
})
}
function showOption(option) {
return option.requiredState == null ||
option.requiredState(state)
}
function selectOption(option) {
const nextTextNodeId = option.nextText
if (nextTextNodeId <= 0) {
return startGame()
}
state = Object.assign(state, option.setState)
showTextNode(nextTextNodeId)
}
const textNodes = [
{
id: 1,
Image:url('invitation.jpg'),
text: "You are cordially invited to celebrate Sir Troy
Bennet's 70th birthday! RSVP to attend the event this
evening at the Cherry Hill mansion.",
options: [
{
text: 'RSVP',
nextText: 2
},
{
text: "I'm going to stay home",
nextText: 24
}
]
},
墨色风雨
相关分类