0
\$\begingroup\$

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;
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

You have two solutions:

1 - Set scale of your gamepad:

self.gamepad.scale = 0.5f

This will make all the gamepad look smaller (the main circle and the direction sprites, as they are child of the main circle).

2 - Create a init method with a size, and calculate there the size of your childs:

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

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

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

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

You may need to change the proportions, but this is a good way to solve your problem and will help you when you want to resize it at any size. In my opinion, this is the best solution.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .