Varun Naharia
Varun Naharia

Reputation: 5420

How to resize the child node fit inside SKSpriteNode when resizing?

I am creating a virtual gamepad for my game, It has one main SKSpriteNode with circle image and four child node(up, down, left, right) with image. When I am using default size for all it looks perfect but it's big in size and when I tried to resize the main circle SKSpriteNode all it's child node are bigger then it's parent and I am unable to calculate size for child node that will fit for every condition(different size of Main node).

-(id)initWithJoystickImage:(NSString *)baseImage
                   upImage:(NSString *)upImage downImage:(NSString *)downImage leftImage:(NSString *)leftImage rightImage:(NSString *)rightImage
{
    if((self = [super initWithImageNamed:baseImage]))
    {
        SKSpriteNode *up = [[SKSpriteNode alloc] initWithImageNamed:upImage];
        up.position = CGPointMake(0, up.frame.size.height/2);
        [self addChild:up];

        SKSpriteNode *down = [[SKSpriteNode alloc] initWithImageNamed:downImage];
        down.position = CGPointMake(0, -down.frame.size.height/2);
        [self addChild:down];

        SKSpriteNode *right = [[SKSpriteNode alloc] initWithImageNamed:rightImage];
        right.position = CGPointMake(right.frame.size.width/2, 0);
self.size.height/2.5);
        [self addChild:right];

        SKSpriteNode *left = [[SKSpriteNode alloc] initWithImageNamed:leftImage];
        left.position = CGPointMake(-left.frame.size.width/2, 0);
 self.size.height/2.5);
        [self addChild:left];
    }
    return self;
}

Default size

When using default size

When resize

when resize according to device size

Edit 1: adding joystick and resize code

Joystick *joystick = [[Joystick alloc] initWithJoystickImage:@"cricle.png" upImage:@"up.png" downImage:@"down.png" leftImage:@"left.png" rightImage:@"right.png"];
    joystick.size=CGSizeMake(DeviceHeight/3, DeviceHeight/3);
    joystick.position=CGPointMake(DeviceWidth/2-joystick.frame.size.width/2, -DeviceHeight/2+joystick.frame.size.height/2);

Upvotes: 1

Views: 801

Answers (1)

WangYudong
WangYudong

Reputation: 4413

You only change the size of the parent node (joystick). This will resize your parent and child nodes together:

[joystick runAction:[SKAction scaleTo:2.0/3.0 duration:0.0]];  // Change scale value

Upvotes: 2

Related Questions