简体   繁体   English

我正在制作一个基于文本的RPG,你可以在每个屏幕上说是或否,我需要帮助删除eventlistener

[英]I am making a text-based RPG, and you can say yes or no on each screen, and I need help removing the eventlistener

So, I have a function that checks if you pressed 'n' or 'y', when you press 'n', a figure attacks you, after it is supposed to remove the event listener, and then give you a chance to win the fight, but before that there is a screen that says press any key to continue, but that screen doesn't show. 所以,我有一个函数可以检查你是否按了'n'或'y',当你按下'n'时,一个数字会攻击你,之后它应该删除事件监听器,然后给你机会赢得战斗,但在此之前有一个屏幕,说按任意键继续,但该屏幕不显示。 Help? 救命?

I've been working on this for a while, and it just wont work, it doesn't remove the event listener. 我已经研究了一段时间,它只是不工作,它不会删除事件监听器。

This is the start screen code, after naming your character and stuff 在命名你的角色和东西之后,这是开始屏幕代码

document.body.innerHTML = "<h4>As you are walking to the bar at a pub, a dark, hooded figure, pulls you aside to a corner: 'Listen, I have a job for you...'</h4><h2>Press 'Y' to accept, and 'N' to turn back to the bar</h2><img id='hoodedFigure' src='https://render.fineartamerica.com/images/rendered/default/print/6.125/8.000/break/images-medium/the-hooded-figure-kayleigh-semeniuk.jpg'>";

//Then it adds the event listener: //然后它添加了事件监听器:

var event1 = window.addEventListener("keydown", checkKeyPressed, false);

//This is the function it calls: //这是它调用的函数:

        {if (evt.keyCode == "89") {
        changeContentOnPressY(1);
    } if (evt.keyCode == "78") {
changeContentOnPressN(1);
    }}}```

//Then it calls this function to change the screen:

```function changeContentOnPressN(num) {
        if (num == 1) {
    window.removeEventListener("keydown", checkKeyPressed, false);
    document.body.innerHTML = "The strange figure draws his weapons and agresses on you. You draw your small blade, and attempt to slash his stomach.<h3 style='text-align:center;'>Press any key to continue...</h3>";
            window.addEventListener("keydown", chance(1), false);
        } if(num == 2) {
window.addEventListener("keydown", checkKeyPressed2, false);
document.body.innerHTML = ""; // for 2nd n path}}```

//This is the part that is not working:

```window.removeEventListener("keydown", checkKeyPressed, false);```

This is the chance() function:

```function chance(num) {
    if (num == 1) {
        var chance = Math.random(0.1, 1);
 if (chance >= 0.80) {
 document.body.innerHTML = "Suddenly a swaggering figure walks towards you he says intrduces himself as Jean-Jacques le Tourment des Ombres, Grand Connoisseur of the Ombres House, he says 'You made the right choice, he was not to be trusted. I too dont like the Sabbath Family. Together we can get revenge'. Press Y to accept and N top refuse";
 } else {
 document.body.innerHTML = "He quickly slits your right wrist, and collects the blood spilled in a barrel. Soon nobles will enjoy this aged red. <br> <h1 style='text-align:center;'>GAME OVER<h1><br><h3 style='text-align:center;'>Press any key to continue...</h3>";
 window.addEventListener("keydown", startGame1, false);}}}}```

There is a lot happening in your question, I am going to focus on what I assume to be the skipped screen. 你的问题中发生了很多事情,我将重点关注我认为是跳过的屏幕。 As far I can tell it should remove the event listener. 到目前为止,我可以告诉它应该删除事件监听器。

In your code you have this segment, which is supposed to wait for the player to press anykey. 在你的代码中你有这个段,它应该等待玩家按任意键。

window.addEventListener("keydown", chance(1), false);

The problem is that you call the "chance" function immediately, instead of waiting. 问题是你立即调用“机会”功能,而不是等待。

If you want to be able to unbind that function again you have 如果你想再次解开该功能,你就可以了

function waitForChance() {
    chance(1)
}

window.addEventListener("keydown", waitForChance, false);

Then you can unbind that later with 然后你可以在以后取消绑定

window.removeEventListener("keydown", waitForChance, false);

Btw If I were you I would probably try to avoid unbinding and rebinding event listeners as much since it is error prone(and difficult to read). 顺便说一下,如果我是你,我可能会尽量避免重新绑定和重新绑定事件监听器,因为它容易出错(并且难以阅读)。 I would probably create a state system like this where you create objects and switch between them. 我可能会创建一个这样的状态系统,您可以在其中创建对象并在它们之间切换。 I think it makes it clearer which functions belong to which state. 我认为它更清楚哪些功能属于哪种状态。

 // every state has 2 properties text and options // text is what is displayed on the screen // options contain the functions that are executed when the user presses a key // if you want to bind a specific just bind enter that key in lower case // if you want to accept any key add a function on anyKey //---------------- STATES -------------------------- let startState = { text: `<h4>As you are walking to the bar at a pub, a dark, hooded figure, pulls you aside to a corner: 'Listen, I have a job for you...'</h4><h2>Press 'Y' to accept, and 'N' to turn back to the bar</h2><img id='hoodedFigure' src='https://render.fineartamerica.com/images/rendered/default/print/6.125/8.000/break/images-medium/the-hooded-figure-kayleigh-semeniuk.jpg'>`, options: { y: function() { loadState(takeDealState) }, n: function() { loadState(ambushState) } } } let takeDealState = { text: `What ever happend when you take the deal (for now i reload the game on anykey)`, options: { /*Your options when you take the deal for now I just reset the game*/ anyKey: function() { loadState(startState) } } } let ambushState = { text: `The strange figure draws his weapons and agresses on you. You draw your small blade, and attempt to slash his stomach.<h3 style='text-align:center;'>Press any key to continue...</h3>`, options: { anyKey: function() { var chance = Math.random(0.1, 1) if (chance >= 0.80) { loadState(successfullAmbush) } else { loadState(failedAmbush) } } } } let successfullAmbush = { text: `Suddenly a swaggering figure walks towards you he says intrduces himself as Jean-Jacques le Tourment des Ombres, Grand Connoisseur of the Ombres House, he says 'You made the right choice, he was not to be trusted. I too dont like the Sabbath Family. Together we can get revenge'. Press Y to accept and N top refuse`, options: { y: function() { console.log("accept") }, n: function() { console.log("decline") } } } let failedAmbush = { text: `He quickly slits your right wrist, and collects the blood spilled in a barrel. Soon nobles will enjoy this aged red. <br> <h1 style='text-align:center;'>GAME OVER<h1><br><h3 style='text-align:center;'>Press any key to continue...</h3>`, options: { anyKey: function() { loadState(startState) } } } ///---------------- LOGIC show and handle keypresses ---------------------------------- let currentState; function loadState(state) { // set the state as the current state, so it can currentState = state; // let the state descibe the scene document.body.innerHTML = state.text; } function keyHandler(event) { if(currentState.options.anyKey) { // handle anykey currentState.options.anyKey(event.key) } else if(currentState.options[event.key]) { // handle other key presses such as y/n currentState.options[event.key](event.key) } } window.addEventListener("keypress", keyHandler, true) // start the game loadState(startState) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要用JavaScript制作的迷你游戏的帮助 - I need help on a mini game that i am making in javascript 如何更有效地提供基于文本的网页? - What can I do to serve text-based webpages more efficiently? 我需要帮助为 html 文本字段创建动态值属性 - I need help making a dynamic value attribute for html text fields 在 Fabric.js 中调整基于文本的对象的大小时,如何防止包含文本的垂直和水平拉伸? - How can I prevent both vertical and horizontal stretching of contained text while resizing text-based objects in Fabric.js? 我想把用户输入的文本打印到屏幕上,附加他们说的每一句话 - i want to take the text a user has entered and print it to the screen, appending each new thing they say JavaScript,我正在发出ajax请求,我需要遍历该对象的帮助 - JavaScript, I am making an ajax request and I need help going through the object 如果我处于其EventListener的函数中,如何获取Element - How can I get the Element if I am in the function of its EventListener 我需要在JavaScript中进行HitTest的帮助 - I need help making a HitTest in JavaScript 我需要在JS中制作交互式Sprite的帮助 - I need help making an interactive sprite in JS 用于基于文本范围的Javascript模糊过滤器,也许需要正则表达式? - Javascript fuzzy filter for text-based ranges, need a regex perhaps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM