AwDogsGo2Heaven
AwDogsGo2Heaven

Reputation: 972

Scale SKShapeNode from center?

I have a SKShapeNode, and a label under one generic SKNode. If I try to scale that node, its position changes for no reason!. If I try to scale only the nodes it contains under it, it scales properly but not from the center, so it grows out to the up and right.

What am I doing wrong? First before scale First before scale

After scale After scale

Notice its position moves up. Can't figure out why, does it even if I do it as an action or not. Also, the position is not changing based on log, but it appears the frame got bigger (though that is expected)

Steps to recreate issue:

set height and width @h = 50 @w = 256

create rect with height and width CGRectMake(0, 0, @w, @h)

get path for rect UIBezierPath.bezierPathWithRect(rect).CGPath

create skshape node with path, set position

  antialiased: false,
  lineWidth: 1.0,
  strokeColor: SKColor.blackColor,
  fillColor: SKColor.whiteColor

Create sklabel, vertical center alignment, set position

create generic SKNode, no properties, add skshapenode and sklabel to it as children

Scale the generic SKNode.

Upvotes: 2

Views: 3631

Answers (3)

user2280013
user2280013

Reputation: 191

With the SKShapeNode, this is impossible.

I solved it by adding a move Action to center the node manually.

You need to apply this to the y and x coordinates

Upvotes: 0

Kaeden Wile
Kaeden Wile

Reputation: 11

I had a similar problem: I created an SKShapeNode.ellipseInRect which look perfectly fine when I didn't need to scale it. Yet as soon as I tried to scale it down to fit the text it was behind, it would randomly shrink towards the bottom left of the screen!

I did a little debugging, and the number that surprised me was that the position was set to (0, 0) even though the ellipse was in the top left corner of the screen! I then realized that by initializing with a rect the "anchor point" (even though SKShapeNodes don't technically have them) was at (0, 0) too. This behavior would explain my shrinking towards that point and your growing away from that point.

What solved this problem for me was instead of putting my position in the rect, set the position afterwards. Then it scaled around my node's center-point and not the screen origin. Hope that helps!

Upvotes: 1

DogCoffee
DogCoffee

Reputation: 19966

Try this

SKLabelNode *labNode = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
labNode.fontSize = 12.0f;
labNode.fontColor = [SKColor blackColor];
labNode.text = @"SMASH";


SKSpriteNode *topWithText = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(100, 30)];
topWithText.position = CGPointMake(100, 110);
[topWithText addChild:labNode];

SKSpriteNode *bottom = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(100, 10)];
bottom.position = CGPointMake(100, 100);

[self addChild:topWithText];
[self addChild:bottom];

[topWithText runAction:[SKAction sequence:@[
                                      [SKAction waitForDuration:3.0],
                                      [SKAction scaleBy:3.0 duration:3]
                                      ]]];

Upvotes: 0

Related Questions