2

i am making a game with sprite kit for iphone. In the gameScene.m im adding score with image numbers so i can get the 1 and 2 digit score by the below 2 cases but i want to add 3 digit score in case3,please as me if u need to know more about this ask me before downgrading thanks.

 //int _gamescore;
_gameScore = 0;
_finalScore = [SKSpriteNode node];
 NSString *digit = [NSString stringWithFormat:@"%i",_gameScore];
[self setScore:(int)digit.length];

-(void)setScore:(int)numberofDigitsInScore
  {
    NSMutableString *scorestring = [NSMutableString stringWithFormat:@"%i", _gameScore];
    [_finalScoreSprite removeAllChildren];
    double val1;
    int sides;
     CGFloat y = self.size.height/2 -10;//position of score on screen

switch (numberofDigitsInScore) {
   case 1:      //  0-9 numbers
       {
           NSString *digit =[scorestring substringFromIndex:0];
           SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:[NSString      
           stringWithFormat:@"Number%@",digit]]; // Number(images 0-9 number images)
           sprite.position = CGPointMake(self.size.width/2, y );
           [_finalScore addChild:sprite];
           break;
       }

 case 2:     //   10-99 numbers
       {
        for (int i = 0; i < scorestring.length; i++) {
            sides =(i!=0)? 1 :-1;
            NSString *digit= [scorestring substringWithRange:NSMakeRange(i, 1)];
            SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:[NSString
             stringWithFormat:@"Number%i",digit.intValue]];

            if (digit.intValue == 1)
            {
                val1 = 5   ;
            } else{
                val1 = 0;
            }
            sprite.position = CGPointMake(self.size.width/2 + (17 -val1)*sides, y);
            [_finalScore addChild:sprite];
        }

        break;
}
anjani
  • 165
  • 2
  • 12

2 Answers2

4

I suggest you generalize the code to avoid having to create multiple cases (one for each score length). Here's one way to do that...

  1. Create an SKNode
  2. Add the digits to the SKNode and set their x positions using the width of each digit (i.e., sprite.size.width) plus some character spacing
  3. Use -(CGRect)calculateAccumulatedFrame to determine the bounding box of the SKNode
  4. Center the node using the dimensions of the bounding box

EDIT: Here's code to center a string of proportional bitmap characters

- (SKSpriteNode *) createScoreNode
{
    SKSpriteNode *score = [SKSpriteNode node];
    SKNode *node = [SKNode new];
    NSString *scoreString = [NSString stringWithFormat:@"%i", 12122];
    CGFloat posX = 0;
    for (int i=0; i<[scoreString length]; i++) {
        NSString *digit = [scoreString substringWithRange:NSMakeRange(i,1)];
        SKSpriteNode *digitSprite = [SKSpriteNode spriteNodeWithImageNamed:
                                     [NSString stringWithFormat:@"Number%@",digit]];
        digitSprite.anchorPoint = CGPointMake(0,0);
        digitSprite.position = CGPointMake(posX,0);
        [node addChild:digitSprite];
        posX += digitSprite.size.width + kCharacterSpacing;
    }
    CGRect boundingBox = [node calculateAccumulatedFrame];
    CGFloat nodeX = -boundingBox.size.width/2;
    CGFloat nodeY = -boundingBox.size.height/2;

    node.position = CGPointMake(nodeX, nodeY);

    [score addChild:node];
    return score;
}
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • it will be good if u place some example here or modify my code it will be great – anjani Aug 27 '14 at 00:24
  • I see that you already accepted an answer, but I edited my answer anyway so you could compare approaches. – 0x141E Aug 29 '14 at 20:14
  • did not tried this yet but will do soon now im working on sound on/off – anjani Aug 30 '14 at 16:56
  • hi today i got funny bug when run this on simulator number with images getting prfectly but when im running on iphone its showing just x boxes and bud (SKTexture: Error loading image resource: "Number1" 2014-10-01 20:25:14.916 Ring[1454:518317] SKTexture: Error loading image resource: "Number1") for all the numbers? – anjani Oct 01 '14 at 19:26
  • Two things to check: 1) is the image's name spelled correctly? the simulate is not case sensitive but devices are and 2) you were testing on a non-retina simulator device and your device is retina. In latter case, you will need a retina version of the image called "Number1@2x.png". – 0x141E Oct 01 '14 at 20:10
  • yea its spelled correctly all the numbers and made retina versions as well tried with retina and non retina on my iphone 5s its given same problem i donno what to do – anjani Oct 02 '14 at 03:51
  • i just fixed it :) i change the sizes to 1x-30x42 and @2x-60x82 of all the numbers and deleted the file from the folder and added again then its working fine – anjani Oct 02 '14 at 04:07
3

An acute case of spaghetti code. Try to simplify. Eliminate cryptic variable names such as val1 or sides. You should not write the same code twice and you should generalize enough to work for any number of digits.

CGFloat widthOfDigit = .... // figure this out beforehand. Include spacing.
NSString *scoreString = [NSString stringWithFormat:@"%i", _score];
CGFloat currentX = (self.size.width - (scoreString.length-1) * widthOfDigit) / 2.0
for (int i=0; i<scoreString.length; i++) {
   NSString *digit = [scoreString substringWithRange:NSMakeRange(i,1)];
   SKSpriteNode *digitSprite = [SKSpriteNode spriteNodeWithImageNamed:
      [NSString stringWithFormat:@"Number%@",digit]];
   digitSprite.position = CGPointMake(currentX,y);
   [_finalScore addSprite:digit];
   currentX += widthOfDigit;
}
Mundi
  • 79,884
  • 17
  • 117
  • 140