简体   繁体   English

如何在javascript画布中为不同的东西重用相同的键?

[英]How can I reuse the same key for different things in javascript canvas?

In the canvas I want to be able to use the enter key to do one thing and then press it again to do another thing.在画布中,我希望能够使用回车键做一件事,然后再次按下它来做另一件事。

For example,例如,

if(enterPressed) {

   // do one thing


}

if (enterPressed) {

    //Do some other thing


}

The problem is when I press enter once it automatically does both things at once whereas I want it to want it do each statement separately.问题是当我按下回车键时,它会自动同时执行两件事,而我希望它分别执行每个语句。

For more context what I want to do is similar to the style of text in pokemon games where the game will display some text then wait until you have pressed a button and then display the next set of text.对于更多上下文,我想要做的类似于 pokemon 游戏中的文本样式,游戏将显示一些文本,然后等到您按下按钮,然后显示下一组文本。

Your question is not specific to JavaScript or Canvas, but related more to general programming techniques.您的问题并非特定于 JavaScript 或 Canvas,而是更多地与通用编程技术相关。 What you describe is different states of your program, rather than 2 actions that are done as a reaction to an event.您所描述的是程序的不同状态,而不是作为对事件的反应而完成的 2 个操作。

So think your program consist of multiple states: Displayed first text to user -> Displayed second text to user因此,认为您的程序由多个状态组成: Displayed first text to user -> Displayed second text to user

You will switch between these states based on certain actions user takes, in your case when they hit enter.您将根据用户采取的某些操作在这些状态之间切换,在您的情况下,当他们按下 Enter 键时。 There're many ways to hold program states, and switch between these ones, but one of the easiest ones I can give an example is, holding your text in an array and an index value that holds which one is the current index:有很多方法可以保存程序状态并在这些状态之间切换,但我可以举出的最简单的方法之一是,将文本保存在一个数组中,并使用一个索引值来保存哪个是当前索引:

var currentIndex = 0;
var texts = ["This is first message", "This is second message"]
if (enterPressed) {
    //you need to have boundary checks of course, skipping for simplicity
    displayText(texts[currentIndex])
    currentIndex++;
}

This is one way to hold the state of the program and switch between those states based on the user action.这是保持程序状态并根据用户操作在这些状态之间切换的一种方法。

The example code in the question has two statements in a row which I assume is how you want to handle enterPressed问题中的示例代码在一行中有两个语句,我认为这是您要如何处理enterPressed

To have a second (or third and more) action you will need to store the state of the enter actions.要进行第二个(或第三个或更多)操作,您需要存储输入操作的状态。 eg Is this the first press?例如,这是第一次印刷吗?

// this in the setup code.
var enterAction = "firstPress"

Then each time enterPressed is handled you must also check the state so that the correct action happens.然后每次处理enterPressed ,您还必须检查状态,以便发生正确的操作。

if (enterPressed && enterAction === "firstPress") {

When you handle the action you also set up the next state to be handled.当您处理操作时,您还设置了下一个要处理的状态。 Because you have two statements in a row that both check if enterPressed is true you need to also indicate that you have handled the press.因为您连续有两个语句都检查enterPressed是否为真,所以您还需要表明您已经处理了新闻。 This can just be setting enterPressed = false这可以只是设置enterPressed = false

    enterAction = "secondPress";
    enterPressed = false; 
}

Thus you code would look like因此你的代码看起来像

// Init the first enter state
const FIRST = 1, SECOND = 2;
var enterAction = FIRST;

And replacing the questions code with并将问题代码替换为

// Handling first press
if (enterPressed && enterAction === FIRST) {
    enterAction = SECOND;  // set action for next press
    enterPressed = false;  // clear press to indicate its been handled

    // do one thing
}

// Handling second press
if (enterPressed && enterAction === SECOND) {
    enterAction = FIRST;    // set action for next press
    enterPressed = false;   // clear press to indicate its been handled

    //Do some other thing
}    

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

相关问题 如何在同一页面中多次重复使用JavaScript函数? - How can I reuse a JavaScript function many times in the same page? 如何为不同的按钮重用 JavaScript 函数? - How can I reuse a JavaScript function for different buttons? 如何检查是否存在具有相同密钥的 object 并使用数组(javascript)中的相同密钥和不同属性进行更新? - How can I check if there's an object with a same key and update with a same key and different properties in array(javascript)? 如何在同一张图片中创建不同的图案<canvas> - How can I create different patterns for the same image in <canvas> 如何优化更新HTML5画布上的许多内容? - How can I optimize updating lots of things on an HTML5 canvas? 如何用JavaScript替换更多内容? - How can I replace more things with Javascript? 如何在它们自己的范围内保留用于不同事物的相同函数名(它可能不是正确的词)? - How can I keep same function names meant for different things in their own scope (It may not be the right word)? 在通过JavaScript的Canvas中,如何将对象移动到其他坐标? - In Canvas via JavaScript, How Can I move an object to a different coordinate? 如何更新Javascript画布中许多不同图像的变量? - How can I update variables for many different images in a Javascript canvas? 如何在不同的范围内重用变量? - How can I reuse a variable in a different scope?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM