CGContextDrawTiledImage当超过一定的尺寸时显示黑色背景
问题描述:
一个完整的新手与Swift和SpriteKit所以请温柔。CGContextDrawTiledImage当超过一定的尺寸时显示黑色背景
我正在开发一个简单的游戏,目前它只不过是一个游戏世界和一个只是向下移动直到撞到游戏世界的边缘而死的玩家(嘿!我在这里学习并获得了解游戏物理等)。
无论如何,我有我的背景渲染与下面的代码,我想创建一个半径为2000的圆形地图,我想平铺填充空间的背景图像,下面的代码创建平铺背景:
let coverageSize = CGSizeMake(8000, 8000)
let textureSize = CGRectMake(0, 0, 599, 519)
let backgroundCGImage = UIImage(named: "background")?.CGImage
UIGraphicsBeginImageContext(CGSizeMake(coverageSize.width, coverageSize.height))
let context = UIGraphicsGetCurrentContext()
CGContextDrawTiledImage(context, textureSize, backgroundCGImage)
let tiledBackground = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let backgroundTexture = SKTexture(image: tiledBackground)
let backgroundTiles = SKSpriteNode(texture: backgroundTexture)
backgroundTiles.yScale = 0.5
backgroundTiles.xScale = 0.5
let cropNode = SKCropNode()
let mask = SKShapeNode(circleOfRadius: 2000)
mask.fillColor = UIColor(red: 0/255, green: 0/255, blue: 255/255, alpha: 1.0)
cropNode.maskNode = mask
cropNode.addChild(backgroundTiles)
self.world?.addChild(cropNode)
通知的coverageSize
我不得不创建覆盖圆半径的大小,如果我离开coverageSize
在停机坪的大小。 2000然后背景图像正确显示,只要我超过2000年,那么背景显示为黑色。如果我将coverageSize
降到2000,那么我会看到纹理边缘与圆形边缘之间的蓝色背景。
任何人都可以看到我做错了什么?有没有更好的方法来绘制平铺在特定半径内的图像?
答
只需从一张简单的蓝色图像开始,并确保您的作物在开始合并作物和瓦节点之前先工作。另外,虽然在CoreGraphics中使用tile会起作用,但速度会很慢并且会占用大量内存。在我的blog post上可以找到一个大大改进的impl,您可以直接在SpriteKit着色器中进行平铺,然后裁剪结果。一定要用白色作物裁剪,作物是一个简单的0/1阈值测试,你不应该使用一种颜色。