objective-c - 如何组合 SKTextures

标签 objective-c sprite-kit

我正在寻找一种将 SKTexture 组合到另一个 SKTexture 之上的方法,其外观类似于添加另一个纹理 SKSPritenode 作为子节点时纹理 SKSpritenode 的外观。我需要它成为一个单一的 SKTexture 作为最终产品。

最佳答案

缺点是你做不到。一个 SKSpriteNode 只能采用一个 SKTexture。在 SpriteKit 框架中执行此操作的唯一方法是在父节点之上添加子节点。

另一种方法是使用一系列图像并将它们组合起来,然后将最终产品作为纹理添加到 Sprite 节点。

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];

确保将 SKSpriteNode 大小属性设置为 (maxWidth, maxHeight),这样就不会剪切任何内容。

关于objective-c - 如何组合 SKTextures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23091001/

相关文章:

iphone - 使用从 JSON 数据中提取的值时出现 NSInvalidArgumentException

objective-c - 在 Objective-C cocoa 中使用方法

objective-c - 已弃用的 userInfo 字典键的可移植代码 (Cocoa)

ios - Swift/SpriteKit 与自定义类的碰撞

ios - 用颜色绘制 SKPhysicsBody 的形状

objective-c - 在核心数据项目中加载 View Controller 时出现问题

iphone - 将核心数据结果排序到 NSFetchedResultsController

swift - 如何为 SKShapeNode 的不透明度设置动画?

ios - 如果删除 SKAction,补全仍然运行吗?

ios - 加速计在第一款游戏中有效,但在第二款游戏中变得疯狂