How to Combine SKTextures

ε祈祈猫儿з 提交于 2019-12-11 09:31:11

问题


I'm looking for a way to combine an SKTexture on top of another SKTexture, similar in appearance to how a textured SKSpritenode would look when adding another textured SKSPritenode as a child. I need it to be a single SKTexture as an end-product please.


回答1:


The short of it is that you can't do it. A SKSpriteNode can only ever take one SKTexture. The only way to do this within the SpriteKit framework is to add children on top of the parent node.

Another way is to use a series of images and combine them before adding the final product as a texture to your sprite node.

CGSize mergedSize = CGSizeMake(maxWidth, maxHeight); // <- use the largest width and height for your images if they are different sizes
UIGraphicsBeginImageContextWithOptions(mergedSize, NO, 0.0f);
[textureImage1 drawInRect:CGRectMake(0, 0, maxWidth, textureImage1.size.height)];
[textureImage2 drawInRect:CGRectMake(0, 0, 75, textureImage2.size.height)];
// ... add any additional images ...
UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

self.texture = [SKTexture textureWithImage:mergedImage];

Make sure you have your SKSpriteNode size property set to (maxWidth, maxHeight) so nothing gets clipped.



来源:https://stackoverflow.com/questions/23091001/how-to-combine-sktextures

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!