Changing the image of a SKSprite to an SKShapeNode

前端 未结 2 1153
粉色の甜心
粉色の甜心 2021-01-21 19:00

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...

<
2条回答
  •  梦毁少年i
    2021-01-21 19:15

    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!

提交回复
热议问题