I'm trying to rotate a Picture in Haskell, using the current time as a value for a rotate function. I've got the following main function:
main :: IO ()
main = do
args <- getArgs
time <- round <$> getPOSIXTime
let initial' = initial time
let (w, h, display) = chooseDisplay args
let background = black
let fps = 60
play display background fps initial' (draw w h) eventHandler timeHandler
The triangle (=player) is stored inside a 'World' data type: module Model where
data World = World {
-- all kinds of World properties --
player :: Picture
}
Then I have a function initial which initializes the World, and a function playerBody which, given a rotation value, returns a picture of player:
initial :: Int -> World
initial _ = World{player = playerBody 0}
playerBody :: Float -> Picture
playerBody rot = Rotate rot $ color red $ Polygon[(-10, 100), (10, 100), (0, 125)]
The draw function is defined as follows:
draw :: Float -> Float -> World -> Picture
draw _ _ world = player world
It currently simply returns the playerPicture.
Now, in my timeHandler module, I want to use the time (given to timeHandler in the main function) to rotate player as follows:
timeHandler :: Float -> World -> World
timeHandler time = (\world -> world {player = playerBody time} )
This doesn't work. I replaced time with a constant value (in the timeHandler function), and that did rotate the Picture. So it seems like time is not being updated.. what am I doing wrong?
timeinmainand thetimeintimeHandlerhave nothing to do with each other -- in fact, they don't even have the same type. The former is a local value you got by executinggetPosixTime, while the latter is a parameter in an independent function definition. They just happen to have the same name. - duplodeplayfunction description,timeHandlershould be given the time in seconds.. - Felixmainfunction" confused me a bit. (You don't give thetimeto ` timeHandler` yourself; rather, Gloss picks it up by itself somewhere in theplayimplementation.) - duplode