1
votes

So I have an SKSpriteNode which has undergone two SKActions which modify it's shape and coloring however I want to return the modified SKSpriteNode as an SKTexture which I can apply to another SKSpriteNode. This is what I'm currently using.

1) Generate the "modified" laserNode and store it as a property

-(void)generateLaserSprite
{
    SKSpriteNode *laserNode = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"Laser.png"]];
    [laserNode runAction:[SKAction group:@[[SKAction colorizeWithColor:[SKColor redColor] colorBlendFactor:100 duration:0.0],
                                           [SKAction scaleXBy:0.4 y:0.7 duration:0.0],
                                           ]]];
    _laserSprite = laserNode
}

2) Then a method from the SKScene calls this method to retrieve a copy of the property

-(SKSpriteNode*)retrieveLaserSprite
{
        SKSpriteNode *laserNode = [_laserSprite copy];
        laserNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(laserNode.size.width*0.8, laserNode.size.height*0.8)];
        //other laserNode.physicsBody modifications

        return laserNode;

The problem with this currently is that when I call the second method (retrieveLaserSprite), the returned LaserNode then shows up on screen as the original image (pre-SKAction), and then you can visibly see the SKActions take place on-screen.

2

2 Answers

1
votes

It should actually be possible to create a texture from a sprite whose rendering properties have been modified using the -(SKTexture *)textureFromNode: method of the SKView class.

Your code would go like this:

-(SKSpriteNode*)retrieveLaserSprite {
    SKTexture *tempTexture = [self.view textureFromNode:_laserSprite];
    SKSpriteNode *laserNode = [SKSpriteNode spriteNodeWithTexture:tempTexture];
    laserNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(laserNode.size.width*0.8, laserNode.size.height*0.8)];
    //other laserNode.physicsBody modifications

    return laserNode;
}
-1
votes

You should be able to accomplish this task using something like:

-(SKSpriteNode*)retrieveLaserSprite 
{
    SKTexture *texture = [self.view textureFromNode:[_laserSprite copy]];
    SKSpriteNode *laserNode = [SKSpriteNode spriteNodeWithTexture:texture];
    laserNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(laserNode.size.width*0.8, laserNode.size.height*0.8)];
    //other laserNode.physicsBody modifications

    return laserNode;
}