我想做的第一件事就是为我的英语道歉,我会尽可能地写下来。 此外,我已经尝试搜索此问题的解决方案,但我直到现在都找不到..
代码中有问题的部分如下:
//eventmanagement for the bat
scene.setOnKeyPressed (new EventHandler <KeyEvent> () {
public void moveBat(double speed) {
if ((speed > 0) && ((bat.getLayoutX() + bat.getWidth())<scene.getWidth())){
bat.setLayoutX(bat.getLayoutX() + speed);
}
if ((speed < 0) && ((bat.getLayoutX() > 0))){
bat.setLayoutX(bat.getLayoutX() + speed);
}
};
@Override
public void handle(KeyEvent event){
if (event.getCode().toString() == "RIGHT"){
this.moveBat(batVelocity);
}
if (event.getCode().toString() == "LEFT"){
this.moveBat(-batVelocity);
}
}
});
这件事有效,但是如果我按下LEFT键,并且我没有按下它,那么只是让它保持按下,然后“蝙蝠”将向左移动一次,然后延迟约1秒,然后继续朝左方向移动。
我想在按下LEFT按钮的时候向左方向移动。任何人都知道如何解决这个问题?
非常感谢你的时间和答案!
Crutz
答案 0 :(得分:0)
因此,ApiType.hs
在第一次按下按键时会触发,然后每隔一秒左右触发一次。如果您想要持续发生某些事情,而不是在{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
module Server where
import Prelude ()
import Prelude.Compat
import Control.Monad.Except
import Control.Monad.Reader
import Data.Aeson.Compat
import Data.Aeson.Types
import Data.Attoparsec.ByteString
import Data.ByteString (ByteString)
import Data.List
import Data.Maybe
import Data.String.Conversions
import Data.Time.Calendar
import GHC.Generics
import Lucid
import Network.HTTP.Media ((//), (/:))
import Network.Wai
import Network.Wai.Handler.Warp
import Servant
import System.Directory
import Text.Blaze
import Text.Blaze.Html.Renderer.Utf8
import qualified Data.Aeson.Parser
import qualified Text.Blaze.Html
type UserAPI =
"users" :> Get '[JSON] [User]
-- :<|> "user" :> Capture "id" Integer :> Get '[JSON] User
-- :<|> "user" :> ReqBody '[JSON] User :> Post '[JSON] User
data User = User {
id :: Int,
name :: String,
age :: Int,
email :: String,
registration_date :: UTCTime
}
instance ToJSON User
users1 :: [User]
users1 =
[ User 1 "Isaac Newton" 372 "isaac@newton.co.uk" (fromGregorian 1683 3 1)
, User 2 "Albert Einstein" 136 "ae@mc2.org" (fromGregorian 1905 12 1)
]
server1 :: Server UserAPI
server1 = return users1
userAPI :: Proxy UserAPI
userAPI = Proxy
app1 :: Application
app1 = serve userAPI server1
main :: IO ()
main = run 8081 app1
事件触发时发生,请考虑让OnKeyPressed
和OnKeyPressed
控制某种OnKeyPressed
,并在某种循环。因此OnKeyReleased
会设置boolean keyIsPressed
而OnKeyPressed
会设置相反的
答案 1 :(得分:0)
所以你要做的就是将你的KeyCode保存到List并在你的AnimationTimer中检查它:
<强>字段强>:
private List<KeyCode> input = new ArrayList<>();
和您的听众:
scene.setOnKeyPressed((KeyEvent event) -> {
input.add(event.getCode());
});
scene.setOnKeyReleased((KeyEvent event) -> {
input.remove(event.getCode());
});
在您的 AnimationTimer :
中 if (input.contains(KeyCode.RIGHT)) {
//do something
}
if (input.contains(KeyCode.LEFT)) {
//do something else
}