【问题标题】:Is it possible to generate/draw on texture with SpriteKit?是否可以使用 SpriteKit 生成/绘制纹理?
【发布时间】:2013-09-17 21:32:35
【问题描述】:

无法搜索任何内容。实际上,关于这个新框架的信息很少。 我唯一能找到的是将核心图像过滤器应用于纹理。但我想在图像上方绘制简单的矩形我需要编写自己的 CI 过滤器..

有人知道这个话题吗?

【问题讨论】:

  • 这里的答案是“绘制”基本形状的正确方法。 SKNode 的文档清楚地说明了更复杂的绘图:“与视图不同,您不能创建执行自定义绘图的 SKNode 子类。”

标签: ios sprite-kit


【解决方案1】:

如果您只需要一个矩形,请使用@Kex 的解决方案。

通常,您可以从任何 UIImage 或 CGImage 创建纹理。 [SKTexture textureWithImageNamed:@"Spaceship.png"]真的只是方便[SKTexture textureWithImage:[UIImage imageNamed:@"Spaceship.png"]]

所以要对纹理进行一些自定义绘制,您可以使用,例如:

UIGraphicsBeginImageContext(CGSizeMake(256, 256));
CGContextRef ctx = UIGraphicsGetCurrentContext();
[[SKColor yellowColor] setFill];
CGContextFillEllipseInRect(ctx, CGRectMake(64, 64, 128, 128));

UIImage *textureImage = UIGraphicsGetImageFromCurrentImageContext();
SKTexture *texture = [SKTexture textureWithImage:textureImage];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:texture];

[self addChild:sprite];

【讨论】:

  • 谢谢 Ben,这就是我们好奇的地方。应该为我工作。
  • 不是独立于平台的。
  • @Jonny 因为这个问题是用 ios 标记的,所以它不需要也适用于 osx 的解决方案。也就是说,我希望看到添加一个跨平台解决方案作为答案,因为这将是有用的参考。
  • 从技术上讲,您对标签的看法是正确的。我目前正在将 cocos2d iOS 游戏移植到 Sprite Kit (OS X),并且在任何有 Sprite Kit 代码的地方都有很多关于 iOS 的假设。出于这个原因,尽可能地留在框架内会更方便。
  • 对于 OSX:NSImage *textureImage = [[NSImage alloc] initWithSize:NSMakeSize(256, 256)]; [纹理图像锁定焦点]; [[SKColor yellowColor] setFill]; [[NSBezierPath bezierPathWithOvalInRect:NSMakeRect(64, 64, 128, 128)] 填充]; [纹理图像解锁焦点]; SKTexture *texture = [SKTexture textureWithImage:textureImage]; SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:texture];
【解决方案2】:

纹理只是原始的图形表示,我不会用它们来解决这个问题。

我会从纹理创建一个精灵,然后是第二个具有给定大小(例如 100x100)的矩形精灵,然后我会将它添加到给定位置的原始精灵(例如CGPointZero)。

SKSpriteNode *rectSprite = [SKSpriteNode spriteNodeWithColor:[SKColor magentaColor] size:CGSizeMake(100, 100)];
rectSprite.position = CGPointZero;
[originalSprite addChild:rectSprite];

这样,矩形将成为精灵的一部分,并且对父精灵(具有纹理的精灵)的所有转换/动作也将应用于它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    相关资源
    最近更新 更多