Reputation: 987
I'm trying to use cocos2d to take a screenshot of the current scene. Working off of this answer to a previous question, I translated it over to Swift, and got this:
func takeScreenshot() -> UIImage {
CCDirector.sharedDirector().nextDeltaTimeZero = true
let viewSize: CGSize = CCDirector.sharedDirector().viewSize()
let renderTexture: CCRenderTexture = CCRenderTexture.renderTextureWithWidth(viewSize.width, height: viewSize.height)
renderTexture.begin()
CCDirector.sharedDirector().runningScene.visit()
renderTexture.end()
return renderTexture.getUIImage()
}
However, I get a compiler error on:
let renderTexture: CCRenderTexture = CCRenderTexture.renderTextureWithWidth(viewSize.width, height: viewSize.height)
It says "cannot invoke 'renderTextureWithWidth' with an argument list of type '(CGFloat, height: CGFloat)'".
I checked the CCRenderTexture.h
file, and it says:
+(instancetype)renderTextureWithWidth:(int)w height:(int)h;
I tried type casting the viewSize.width
and the viewSize.height
to Int
, but it still gave the same "cannot invoke" error again.
Anyone know why this is the case and how it can be fixed?
Upvotes: 0
Views: 79
Reputation: 348
These type conversions can be tricky. But if you use the default Swift constructor CCRenderTexture(width: Int32, height: Int32)
and not the objective-c factory method (the one you tried to use) auto completion will help you out. You need to cast to Int32
.
Upvotes: 1