我希望有人能夠藉助信號和效果來幫助我。無法觸及榆樹勾選操作
我正在調查信號/效果,並一直在研究榆樹體系結構,尤其是example 8。
在這個例子中(據我所知),如果你點擊形狀(S):
- 信號消息與動作
- 一起發送到地址
SPIN
動作射擊更新 - 如果他的形狀目前沒有動畫,則也會調用Tick。
我想複製完全相同的流程(使用類似的代碼),而不是點擊一個按鈕並使用HTML包,我想只使用空格鍵發送信號。
在我的代碼中,空格鍵將信號發送給Punch
操作。就像示例8一樣,我還想在我的模型中調用Tick
並更新debounceState
。
你會在我的評論中看到我可以達到我的Punch
動作,但我似乎沒有達到Tick
。
我看過this question,但因爲我使用的是鍵盤信號而不是榆樹StartApp,所以我認爲它不適用。
如果有人能解釋我爲什麼無法達到Tick
在我的例子中,我會非常感激。
module Main (..) where
import Graphics.Element exposing (..)
import Time exposing (Time, second)
import Effects exposing (Effects)
import Keyboard
type alias DebounceState =
Maybe
{ prevClockTime : Time
, elapsedTime : Time
}
type alias Model =
{ punching : Bool
, count : Int
, randomString: String
, debounceState : DebounceState
}
duration = second
-- MODEL
initialModel : (Model, Effects Action)
initialModel =
({ punching = False
, count = 0
, randomString = ""
, debounceState = Nothing
}
, Effects.none
)
model : Signal (Model, Effects Action)
model =
Signal.foldp update initialModel (punch Keyboard.space)
-- ACTIONS
type Action
= NoOp
| Punch Bool
| Tick Time
-- UPDATE
update : Action -> (Model, Effects Action) -> (Model, Effects Action)
update action (model, fx) =
case action of
NoOp ->
(model, Effects.none)
Punch isPunching ->
case model.debounceState of
Nothing ->
({ model |
punching = isPunching
, count = model.count + 1 }, Effects.tick Tick)
Just _ ->
({ model |
punching = isPunching
, count = model.count + 2 }, Effects.none)
-- I don't seem to reach `Tick` at all
-- You'll notice that I try to apply a value to `randomString` in the
-- conditional to indicate where I end up
Tick clockTime ->
let
newElapsedTime =
case model.debounceState of
Nothing ->
0
Just {elapsedTime, prevClockTime} ->
elapsedTime + (clockTime - prevClockTime)
in
if newElapsedTime > duration then
({model | randomString = "foo"} , Effects.none)
else
({model | randomString = "bar"} , Effects.tick Tick)
-- SIGNAL
punch : Signal Bool -> Signal Action
punch input =
Signal.map Punch input
-- VIEW
view : (Model, Effects Action) -> Element
view model =
show model
main : Signal Element
main =
Signal.map view model
直接粘貼到Try Elm。
什麼是優秀的答案。感謝您的幫助和其他鏈接 –