3

Sprite Kit, Xcode.

I need to find a way to change a sprites image within the program itself. I know how to create jpg files and make them into the sprite image...

But for this program, I need to draw circles/polygons (which may change inside the program) using SKShapeNode, and then transferring this to the SKSpriteNode's image.

Let's say I have declared:

SKSpriteNode *sprite;
SKShapeNode *image;

How would I do this with these variables?

Thanks!

EDIT: I mean texture when I say image.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78

2 Answers2

3

If I understand your question correctly, you can achieve what you're after using the textureFromNode method on SKView.

In your SKScene:

-(void)didMoveToView:(SKView *)view {
    SKShapeNode *shape = [SKShapeNode shapeNodeWithCircleOfRadius:100];
    shape.fillColor = [UIColor blueColor];
    shape.position  = CGPointMake(self.size.width * 0.25, self.size.height * 0.5);
    [self addChild:shape];

    SKTexture *shapeTexture = [view textureFromNode:shape];
    SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture: shapeTexture];
    sprite.position = CGPointMake(self.size.width * 0.75, self.size.height * 0.5);
    [self addChild:sprite];
}

Hope that helps!

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78
  • Now the problem is, I seem to be using a different language from u... I've never seen the word "let"? Using SKSpriteKit, Xcode... Is that swift? I'm quite new to Xcode, btw so plz tell me if I'm wrong ^^ –  May 13 '15 at 21:18
  • I've updated my answer :) It's been a little while since I wrote any Objective-C! – ABakerSmith May 13 '15 at 21:26
  • Is there any other way to do this? In my new program I need to directly change my texture in an already existing sprite, say from a triangle to a circle. Thanks –  Jun 06 '15 at 14:05
0

You cannot change a SKSpriteNode image once you assign it. To do what you want, you need to create a SKSpriteNode using a texture.

- (instancetype)initWithTexture:(SKTexture *)texture

To change a SKSpriteNode's texture you assign a new texture using its texture property. You can also do this using an image converted to a texture like this:

myNode.texture = [SKTexture textureWithImageNamed:@"imageName"];

As for a SKShapeNode, you cannot assign an image. Only a path, rect, circle, ellipse or points. Look at the SKShapeNode class docs section Creating a Shape Path for more info.

sangony
  • 11,636
  • 4
  • 39
  • 55
  • No... I meant I want to assign the SKShapeNode to the SKSpriteNode's image. So sprite.texture = SKShapeNode. When I say image in the question, I really meant texture, sry –  May 13 '15 at 20:07
  • @MasterShuriken - To my knowledge not possible. – sangony May 13 '15 at 20:24