4

I basically want to add custom font numbers (with images) in Singles and add them for my score in my game with SpriteKit.

This is the font image style

Number 9

They are numbers from 0 to 9

I have been trying to figure this out for a while.

I'm not sure if you can add it to SKLabelNode or if you would have to make it in a class.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
hka
  • 73
  • 5

1 Answers1

1

SKLabelNode would not allow for bitmap fonts. If your custom font was TTF, you would be able to use the custom font with SKLabelNode. An explanation how is here: SKlabelNode custom font

In your case, since you want to use bitmap fonts, you will need to create a class to do it do this. You can either subclass an SKNode or not. But you would want to have one SKNode act as the "root" and then have each digit for your number be an SKSpriteNode (these would be children nodes of the root node representing the number).

One thing you will want to determine is how you want to handle alignment for the number (both horizontally and vertically).

I have used my own custom class to draw numbers before and typically have it configurable to be adjustable horizontally (left justified, right justified, and centered). But out of laziness, I usually always center vertically.

This may sound complicated, but is easy to do. You would just need to have

  • Way to assign texture atlas of bitmaps for each digit. It is easier if you have them in progression of 0-9.
  • Method to set/get number value
  • Method which can convert number into individual digits. You'll need to this to compute which digit maps to which character.
  • Method to compute width of number. If your fonts are monospaced, then this is easy. If they are not, then you'll need to compute width based on digit width and letter-spacing. For monospaced font you still may want to factor in letter-spacing.
  • Method to compute height of number. This should be easy as 0-9 should all be the same height.
  • Method to remove existing SKSpriteNode and add each new digit. Each new digit being offset by the proper amount based on alignment.

Your custom class should, upon the number value being set, build the correct SKSpriteNodes to represent the number.

Some updated info for the last question regarding to how one would use an array with the SKTexture. This code is not tested for compilation and is meant to just give you a better idea.

// Example of storing digits in an array. Assumes you are putting this in an atlas and your
// sub-texture names are 0, 1, etc. If not you'll have to adjust the code accordingly
var numTextures = [SKTexture]()
let numberAtlas = SKTextureAtlas(named: "NumberAtlas")

for i in 0 ..< 10 {
    numTextures.append(numberAtlas.textureNamed("\(i)"))
}

// To get the texture for a digit

func textureForDigit(digit:Int) -> SKTexture {
    return numTextures[digit]
}
Community
  • 1
  • 1
Mobile Ben
  • 7,121
  • 1
  • 27
  • 43
  • Thank you for your Answer. I'll try this out soon as I can an update you. – hka Sep 12 '16 at 17:05
  • Okay I added texture atlases for each digit but adding a method to set/get number value or converting into individual digits gives me errors (can not convert Int to SKTexture...) ... – hka Sep 13 '16 at 04:12
  • Of course not. That is what an array is for. Create an array of `SKTexture` where the 0 index represents 0, index 1 represents 1, etc. – Mobile Ben Sep 13 '16 at 05:11
  • I think a simple example would be best. There's just too many conflicting errors that come up. Sorry I'm new to Swift. I understand if you don't have the time to do so. Thanks. – hka Sep 13 '16 at 17:58
  • I added a code example. In the future, you should provide your code attempts at the very least, lest it comes across that you're just trying to get other people to do your coding for you. Especially when you're asking for examples of pretty basic stuff. New to swift or not, this type of question has been asked numerous times in numerous ways. – Mobile Ben Sep 14 '16 at 02:45
  • I usually do but this time it was very different. Like I mentioned I was getting all kinds of errors from attempting numerous times. Also never really worked with TextureAtlas so its pretty challenging. Thank you for taking the time to write a simple code example. – hka Sep 14 '16 at 18:43
  • I get it. You're new to Swift and TextureAtlases but do you realize how often those excuses are used? Look at it from the side of a person providing an answer. People on SO are answering questions in their spare time, so it stands to reason that good faith needs to be utilized all around. And that includes the people that are asking questions to do enough due diligence. This comment is for your benefit. People will stop answering your questions if they feel you're using them instead of reading/working through the problem yourself. Particularly for more rudimentary items. – Mobile Ben Sep 14 '16 at 21:55
  • Just to be clear, I really do appreciate you taking the time to answer this question and others on the site. I've benefited much from experienced programers such as yourself. Thanks for the advice. – hka Sep 15 '16 at 04:53