7

SKScene is a subclass of SKNode and we can initialize it with a certain size. But SKNode itself lacks this ability and its size is the smallest rectangle that contains the children. Sometimes I need my SKNode to stretch to the window no matter how small the contents are. Therefore I would like to be able to customize SKNode class by adding the ability to set its size. Do you have any idea how?

Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206
  • 1
    A SKNode has no size and I believe its frame.size will always be 0,0. If you want to stretch a node's contents you'd have to do so for each child individually. – CodeSmile Feb 17 '14 at 21:10
  • And do you have any idea how this is done in SKScene class? As you kno w it's a subclss of SKNode, so obviously some more fetures, as well as the ability to set size was added to it. – Mikayil Abdullayev Feb 18 '14 at 05:11

1 Answers1

12

Oddly enough, there doesn't seem to be a way to do this built into sprite kit. You may have to settle by adding a transparent SKSpriteNode to your node:

- (id)initWithSize:(CGSize)size{
    self = [super init];
    if (self) {
        SKSpriteNode *node = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithWhite:1.0 alpha:0.0] size:size];
        [self addChild:node];
        node.zPosition = -1;
        node.name = @"transparent";
        node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
    }
    return self;
}

Now you can initialize it to the size you want, you would just have to be sure to change the transparent nodes size if you wanted to change the nodes size.

Andrew97p
  • 565
  • 6
  • 11
  • I think it makes a lot of sense for a "node" not to have a frame of its own, and the idea presented here is the best way to approach this problem. IMHO, of course – Mazyod Mar 20 '15 at 19:00