Question

How can I group two SKSpriteNodes together so that I can z-rotate them as if they are one? Let's say one SKSpriteNode is a rope and the other is a ball attached to the end of the rope. How can I make it look like they swing together? What would be the SKAction to do this?

Était-ce utile?

La solution

Two options:

  1. Put them both in a SKNode and rotate the SKNode (around a certain anchor point)

    SKNode *container = [[SKNode alloc] init];
    [container addChild:ballSprite];
    [container addChild:ropeSprite];
    container.zRotation = someValue;
    
  2. Or rotate them separately and them move them so it looks as if they were rotated together.

Autres conseils

Probably not the most elegant solution but here is how i have added SKSpriteNodes to a SKNode (container). containerArray contains the nodes that should be added. The newSpriteName (NSString) is used when deciding if the sprite is displayed with it's front side or back side.

// Add a container
_containerNode = [SKNode node]; 
_containerNode.position = _theParentNodePosition;
_containerNode.name = @"containerNode";
[_background addChild:_containerNode];

for (SKNode *aNode in containerArray) {

        if (![aNode.name isEqualToString:@"background"] && ![aNode.name isEqualToString:@"title1"] && ![aNode.name isEqualToString:@"title2"]) {
            // Check if "back" sprite should be added or the front face
            if ([[aNode.name substringFromIndex:[aNode.name length] - 1] isEqualToString:@"b"]) {
                newSpriteName = @"back";
            } else {
                newSpriteName = aNode.name;
            }

            // Prepare the new node
            SKSpriteNode *newNode = [SKSpriteNode spriteNodeWithImageNamed:newSpriteName];
            newNode.name = aNode.name;
            newNode.zPosition = aNode.zPosition;
            newNode.position = CGPointMake(aNode.position.x - _theParentNodePosition.x, aNode.position.y - _theParentNodePosition.y);

            // Delete the old node
            SKNode *deleteNode = [_background childNodeWithName:aNode.name];
            [deleteNode removeFromParent];

            // Add the new node
            [_containerNode addChild:newNode];
        }
    }

And then do a rotation as DrummerB suggested

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top