赤裸裸的问题:
有没有办法在 Elm 中定义一对相互依赖的信号?
前言:
我正在尝试编写一个小型 Cookie-clicker 风格的浏览器游戏,玩家在其中收集资源,然后用它们购买自主资源收集结构,这些结构在购买时会变得更加昂贵。这意味着三个相关信号:(gathered
玩家收集spent
了多少资源),(玩家已经花费了多少资源)和cost
(升级成本)。
这是一个实现:
module Test where
import Mouse
import Time
port gather : Signal Bool
port build : Signal String
costIncrement = constant 50
cost = foldp (+) 0 <| keepWhen canAfford 0 <| sampleOn build costIncrement
nextCost = lift2 (+) cost costIncrement
spent = foldp (+) 0 <| merges [ sampleOn build cost ]
gathered = foldp (+) 0 <| merges [ sampleOn gather <| constant 1, sampleOn tick tickIncrement ]
balance = lift round <| lift2 (-) gathered spent
canAfford = lift2 (>) balance <| lift round nextCost
tickIncrement = foldp (+) 0 <| sampleOn cost <| constant 0.01
tick = sampleOn (every Time.millisecond) <| constant True
main = lift (flow down) <| combine [ lift asText balance, lift asText canAfford, lift asText spent, lift asText gathered, lift asText nextCost ]
这编译得很好,但是当我将它嵌入到一个带有适当按钮的 HTML 文件中以将消息发送到上面的适当端口时,我得到了错误
s2 is undefined
Open the developer console for more details.
问题似乎是,如所写,cost
取决于canAfford
,取决于balance
,spent
取决于 ,cost
再次取决于。
如果我修改成本线使得
...
cost = foldp (+) 0 <| sampleOn build costIncrement
...
它开始按预期工作(除了允许玩家花费负资源,这是我想避免的)。
有任何想法吗?