Javascript 根据您选择的选项显示图像
我正在学习一个关于如何用javascript制作文本冒险游戏的教程(),并希望将该代码重新应用到我自己的游戏中,除了取决于您选择的选项外,图像会发生变化。我该怎么做?可以在此处()找到原始代码,但以下是代码的基本要点:Javascript 根据您选择的选项显示图像,javascript,Javascript,我正在学习一个关于如何用javascript制作文本冒险游戏的教程(),并希望将该代码重新应用到我自己的游戏中,除了取决于您选择的选项外,图像会发生变化。我该怎么做?可以在此处()找到原始代码,但以下是代码的基本要点: const textElement = document.getElementById('text') const optionButtonsElement = document.getElementById('option-buttons') let state = {}
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
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,
text: 'You wake up in a strange place and you see a jar of blue goo near you.',
options: [
{
text: 'Take the goo',
setState: { blueGoo: true },
nextText: 2
},
{
text: 'Leave the goo',
nextText: 2
}
]
}
const textElement=document.getElementById('text'))
const optionButtonsElement=document.getElementById('option-buttons'))
让状态={}
函数startName(){
状态={}
showTextNode(1)
}
函数showTextNode(textNodeIndex){
const textNode=textNodes.find(textNode=>textNode.id==textNodeIndex)
textElement.innerText=textNode.text
while(optionButtonsElement.firstChild){
OptionButtonSelection.removeChild(OptionButtonSelection.firstChild)
}
textNode.options.forEach(选项=>{
如果(显示选项(选项)){
const button=document.createElement('按钮')
button.innerText=option.text
button.classList.add('btn')
按钮。addEventListener('单击',()=>selectOption(选项))
optionButtonsElement.appendChild(按钮)
}
})
}
功能显示选项(选项){
return option.requiredState==null | | option.requiredState(状态)
}
功能选择选项(选项){
const nextTextNodeId=option.nextText
如果(nextTextNodeId,您可以在selectOption功能中尝试:
function selectOption(option) {
const nextTextNodeId = option.nextText
switch (nextTextNodeId) {
case 2:
document.body.style.backgroundImage = "url('https://images.pexels.com/photos/2886284/pexels-photo-2886284.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260')";
break;
}
if (nextTextNodeId <= 0) {
return startGame()
}
state = Object.assign(state, option.setState)
showTextNode(nextTextNodeId)
}
功能选择选项(选项){
const nextTextNodeId=option.nextText
开关(nextTextNodeId){
案例2:
document.body.style.backgroundImage=“url('https://images.pexels.com/photos/2886284/pexels-photo-2886284.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260')";
打破
}
如果(nextTextNodeIdconst image=document.createElement('img');img.src=option.imageUrl;
),或者只是有一个固定的图像,并将其src更改为您定义的option.imageUrl对不起,我自己无法使其工作,您能用几个案例展示一个示例吗?