Changing the image of a SKSprite to an SKShapeNode

大城市里の小女人 提交于 2019-12-02 05:55:25

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!

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.

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