如何在js中实现嵌套的事件侦听器

问题描述 投票:0回答:1

我正在构建一个文字冒险游戏,它接受用户输入并引导玩家沿着树状结构走下去。我的思考过程是让一个主要事件侦听器侦听将开始游戏的提交动作。然后,我根据初始响应继续添加另一个事件侦听器,但没有任何反应。

嵌套事件侦听器甚至是处理这种情况的正确方法吗?

//prompts
const bed = 'Your lack of desire has lead you towards a life of bordeom and dread. [[GAME OVER]]'.split('');
const explore = 'As your eyes adjust to the early monning sun, you glance around the room. To your left, you notice a small, but sturdy bed side table. In front of you, a TV is broadcasting re-runs of the show "Friends". You also consider walking and exploring more of this strange room.What is your next inclination?'.split('')
const walkAroundRoom = 'Walking around the room seems like a good idea. After all, you tell yourself, "I should at least aquainte and introduce myself to this bewildering experience. After a bit of pondering and wandering, you look straight ahead and notice a bathroom. To your right, a window.'.split('')

submit.addEventListener('submit', () => {
  if (response('bed')) {
    go(bed)
  } else if (response('ex')) {
    go(explore)

    submit.addEventListener('submit',()=>{
      if(response('tv')){
        go(watchTV)
      } else if(response('walk')){
        go(walkAroundRoom)
      }
    })
  }
})
javascript events event-handling submit
1个回答
1
投票

我将使用一个对象,该对象的键唯一地标识一组文本,并且值包含要显示的文本以及每个可能的响应将指向的提示键。这样,您只需要添加一个侦听器:

© www.soinside.com 2019 - 2024. All rights reserved.