Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SKLabelNode positioning not working as expected

I am working on a game and i need to implement the top bar, holding the details like score and other stuff.

My top bar should have the following (left to right): - a 50x50 icon that changes according to level, but it's the same size - a label with some text that changes according to level - ... other elements

The problem is that when the label text changes, the label sometimes is too far from the icon and other times is over the icon (depending on the text length)

I thought I understood positioning, but apparently i didn't..

//create array for level text label
levelTextArray = [[NSArray alloc] initWithObjects:@"\"Uuu! Bubbles!\"",@"\"Noob\"",@"\"I’m getting it..\"",@"\"This is easy\"",@"\"Wha?\"",@"\"It’s ooon now!\"",@"\"Come on..\"",@"\"Dude…\"",@"\"You’re pushing it..\"",@"\"I’ll show you!!\"",@"\"AAAAAAA!!!\"",@"\"Holy Bubbles… \"",@"\"Ninja mode on!\"",@"\"I’m on fire!!\"",@"\"The wheel's spinning, but the hamsters dead. \"", nil];

//add level text label
levelTextLabel = [[SKLabelNode alloc] init];
levelTextLabel.text = levelTextArray[0];
levelTextLabel.position = CGPointMake(60, CGRectGetMidY(scoreImage.frame)+5);

levelTextLabel.fontColor = [UIColor colorWithRed:0/255.0f green:1/255.0f blue:0/255.0f alpha:1.0f];
levelTextLabel.fontName = @"Noteworthy-Light";
levelTextLabel.fontSize = 14.0;
[self addChild:levelTextLabel];

scoreImage is the icon in this case. Also..for the image to appear completely in the view I position it like this:

  scoreImage = [[ SKSpriteNode alloc] initWithImageNamed:levelText];
    scoreImage.name = @"LevelImage";
    scoreImage.size = CGSizeMake(50, 50);
    scoreImage.position = CGPointMake(0+scoreImage.size.width/2, CGRectGetMaxY(self.frame)-scoreImage.size.height/2);

I have the feeling that I'm doing something wrong here.

Any help is appreciated.

P.S. The label text as well as the icon image is changed according to level in -(void)update:(CFTimeInterval)currentTime {

like image 663
Deea B Avatar asked Mar 21 '23 09:03

Deea B


1 Answers

Try setting the horizontalAlignmentMode of the SKLabelNode:

levelTextLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;

The default is SKLabelHorizontalAlignmentModeCenter.

like image 96
Corey Avatar answered May 07 '23 17:05

Corey