添加自定义“数字”使用Swift将图像作为SpriteKit中的分数存入SKLabelNode [英] Add custom "number" images to SKLabelNode in as a Score in SpriteKit with Swift

查看:104
本文介绍了添加自定义“数字”使用Swift将图像作为SpriteKit中的分数存入SKLabelNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上想在Singles中添加自定义字体编号(带图片),并使用SpriteKit在我的游戏中为我的分数添加它们。

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

这是字体图像样式

它们是从0到9的数字

我一直试图解决这个问题。

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

我不确定你是否可以将它添加到SKLabelNode,或者你是否必须在课堂上进行。

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

推荐答案

SKLabelNode 不允许使用位图字体。如果您的自定义字体是TTF,则可以使用 SKLabelNode 的自定义字体。解释如何: SKlabelNode自定义字体

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

在您的情况下,由于您要使用位图字体,因此您需要创建一个类来执行此操作。您可以子类化 SKNode 。但是你想要一个 SKNode 充当root,然后让你的号码的每个数字都是 SKSpriteNode (这些是表示数字的根节点的子节点)。

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


  • 为每个数字指定位图的纹理图集。如果你有0到9的进展,它会更容易

  • 设置/获取数字值的方法

  • 可以将
    数转换为单个数字的方法。你需要这个来计算哪个
    数字映射到哪个字符。

  • 计算数字宽度的方法。如果
    你的字体是等宽的,那么这很容易。如果不是,那么
    你需要根据数字宽度和字母间距来计算宽度。对于等宽字体,您仍然可能需要考虑字母间距。

  • 计算数字高度的方法。这应该很容易,因为0-9应该是
    都是相同的高度。

  • 删除现有 SKSpriteNode
    的方法添加每个新数字。根据对齐方式,每个新数字都会被适当的金额
    抵消。

  • 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.

您的自定义类应该在数字值为设置,构建正确的 SKSpriteNode 来表示数字。

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

关于如何设置的最新问题的一些更新信息一个人将使用 SKTexture 的数组。此代码未经过编译测试,只是为了让您更好地了解。

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]
}

这篇关于添加自定义“数字”使用Swift将图像作为SpriteKit中的分数存入SKLabelNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆