Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to generate/draw on texture with SpriteKit?

Tags:

ios

sprite-kit

Can't google anything. Actually it is quite few information about this new framework. Only thing I could find is the applying Core Image filter to a texture. But I guess to draw simple rectangle above the image I need to write own CI filter..

Does anybody know something on topic?

like image 840
andrey.s Avatar asked Sep 17 '13 21:09

andrey.s


2 Answers

If all you need is a rectangle, use @Kex's solution.

In general, you can create a texture from any UIImage or CGImage. [SKTexture textureWithImageNamed:@"Spaceship.png"] is really just a convenience to [SKTexture textureWithImage:[UIImage imageNamed:@"Spaceship.png"]]

So to do some custom drawing into a texture, you can use, for example:

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];
like image 169
Ben Taitelbaum Avatar answered Sep 24 '22 03:09

Ben Taitelbaum


Textures are just raw graphic representations, I won't solve this with them.

I'd create a sprite from the texture, then a second rectangle sprite with a given size (e.g. 100x100) & I'll add it to the original one at a given position (e.g. CGPointZero).

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

That way the the rectangle will be a part of the sprite & all the transformations/actions to the parent sprite (the one with the texture) would apply to it too.

like image 27
Kex Avatar answered Sep 22 '22 03:09

Kex