我正在尝试将绘图函数映射到列表中的每个元素.函数本身,drawMap似乎没问题但是当我使用它时,我收到以下错误:
Couldn't match type `[]' with `IO'
Expected type: IO (IO Bool)
Actual type: [IO Bool]
In the return type of a call of `drawMap'
In a stmt of a 'do' block: drawMap testMap 32 img screen
In the second argument of `($)', namely
`do { screen <- SDL.setVideoMode 640 480 32 [SDL.SWSurface];
img <- SDL.loadBMP "grass.bmp";
drawMap testMap 32 img screen;
SDL.flip screen;
.... }'
Run Code Online (Sandbox Code Playgroud)
从阅读本文开始,我现在明白它与函数返回的内容有关,但我现在还不知道如何修复它.
我的代码看起来像这样:
testMap = [Tile 0 0 1, Tile 0 1 1, Tile 0 2 1, Tile 0 3 1]
drawTile :: Int -> SDL.Surface -> SDL.Surface -> Tile -> IO Bool
drawTile tilesize img dst tile = applySurface (tileX tile * tilesize) (tileY tile * tilesize) img dst
drawMap :: [Tile] -> Int -> SDL.Surface -> SDL.Surface -> [IO Bool]
drawMap tiles tilesize img dst =
map (drawTile tilesize img dst) tiles
main :: IO ()
main = SDL.withInit [SDL.InitEverything] $ do
screen <- SDL.setVideoMode 640 480 32 [SDL.SWSurface]
img <- SDL.loadBMP "grass.bmp"
--drawTile 32 img screen (testMap !! 1)
drawMap testMap 32 img screen
SDL.flip screen
mainLoop
Run Code Online (Sandbox Code Playgroud)
我想你想要:
drawMap tiles tilesize img dst =
mapM (drawTile tilesize img dst) tiles
Run Code Online (Sandbox Code Playgroud)
mapM是来自Control.Monad.类型drawMap变为:
drawMap :: [Tile] -> Int -> SDL.Surface -> SDL.Surface -> IO [Bool]
Run Code Online (Sandbox Code Playgroud)
即它是一个IO动作返回Bool列表,而不是IO动作列表.