我正在制作一款游戲,根據(jù)玩家所做的選擇,它將顯示新的文本供選擇。我正在嘗試將圖像與該文本連接起來(lái),以便它們能夠適應(yīng)每個(gè)選擇。現(xiàn)在,我的文字可以正常工作,但我的圖像無(wú)法顯示。我在這里做錯(cuò)了什么? <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 } ] },
用文字改變圖像
慕容森
2023-11-02 19:58:06