将SKLabelNode放在SKSpriteNode上居中 [英] Centre a SKLabelNode on a SKSpriteNode

查看:58
本文介绍了将SKLabelNode放在SKSpriteNode上居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个SKLabelNode,它是SKSpriteNode的子级,因为我试图创建一个Button类,以更简单的方式创建按钮.我已经尝试过使用SKSpriteNode的锚点进行一些操作,但是我不太了解发生了什么.如何将标签居中放置在Sprite(它的父节点)上?

I have an SKLabelNode that is the child of a SKSpriteNode because I'm trying to create a Button class to create buttons in an easier way. I've tried a couple of things using the anchor point of the SKSpriteNode, but I don't quite understand exactly what is going on. How do I centre the label onto the sprite (it's parent node)?

推荐答案

我意识到了如何解决这个问题……这就是我所做的.请记住,我有一个名为Button的类,它是SKSpriteNode的子类.

I realized how to solve this...here's what i did. Keep in mind that I have a class called Button that is a subclass of SKSpriteNode.

在Button.m类中,我有一个名为label的实例变量,它是SKLabelNode.我将标签节点作为子节点添加到按钮,然后将水平和垂直对齐方式设置为居中.

In the Button.m class I have an instance variable called label that is a SKLabelNode. I add the label node as a child to the button then set the horizontal and vertical alignment modes to centre.

label = [[SKLabelNode alloc] init];
[self addChild:label];
[label setHorizontalAlignmentMode:SKLabelHorizontalAlignmentModeCenter];
[label setVerticalAlignmentMode:SKLabelVerticalAlignmentModeCenter];

这篇关于将SKLabelNode放在SKSpriteNode上居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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