在 Xcode 中的 SKSpriteNode 中设置 LabelNode 的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22464375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting text of LabelNode within an SKSpriteNode in Xcode
提问by user2887097
I have an SKLabelNode within an SKSpiteNode within yet one more SKSpriteNode. Purpose is to create a graphic with a blank button on it and then text for that button. I am able to create this but later in my code I want to change the text of this Label Node and I can't seem to do it.
我在另一个 SKSpriteNode 中的 SKSpiteNode 中有一个 SKLabelNode。目的是创建一个带有空白按钮的图形,然后是该按钮的文本。我能够创建它,但稍后在我的代码中我想更改此标签节点的文本,但我似乎无法做到。
SKSpriteNode * barStart = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:baseColor]];
barStart.name=barName;
SKSpriteNode * playPauseButton = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"button"]];
playPauseButton.name=[NSString stringWithFormat: @"%@-playPauseButton",barName];
playPauseButton.position=CGPointMake( playPauseButton.frame.size.width*2.2, 0);
playPauseButton.alpha=0;
[barStart addChild:playPauseButton];
SKLabelNode * playPauseLabel = [SKLabelNode labelNodeWithFontNamed:@"HelveticaNeue-Thin"];
playPauseLabel.name=[NSString stringWithFormat: @"%@-playPauseButtonLabel",barName];
NSLog(@"playPauseLabel.name : %@",playPauseLabel.name);
playPauseLabel.position=CGPointMake(0,-5);
playPauseLabel.fontSize = 10;
playPauseLabel.horizontalAlignmentMode=SKLabelHorizontalAlignmentModeCenter;
playPauseLabel.fontColor = [SKColor grayColor];
playPauseLabel.text=@"start/stop";
[barStart addChild:playPauseLabel];
I am trying to change as follows:
我试图改变如下:
[[[[self childNodeWithName:@"bar1"] childNodeWithName:@"bar1-playPauseButton"]
childNodeWithName:@"bar1-playPauseButtonLabel"] setText:@"somethingElse"];
and I get an error:
我收到一个错误:
No visible @interface for 'SKNode' declares the selector 'setText:'
I also tried creating a local SKNode, passing the childNode reference to it and the trying to set the text with dot notion like thisLabelNode.text=@"somethingElse"
and while this doesn't cause an error, it also does not change the text.
我还尝试创建一个本地 SKNode,将 childNode 引用传递给它,并尝试使用点概念设置文本,thisLabelNode.text=@"somethingElse"
虽然这不会导致错误,但它也不会更改文本。
Any ideas?
有任何想法吗?
thanks, rich
谢谢,有钱
回答by LearnCocos2D
childNodeWithName
returns an object of type SKNode*
, not SKLabelNode. Hence the error.
childNodeWithName
返回类型为 的对象SKNode*
,而不是 SKLabelNode。因此错误。
If you know that the returned node is a SKLabelNode (or nil) you can cast it. I would also recommend to avoid nesting too many function calls as that'll make your code harder to read. Plus you can use the search featureto your advantage here:
如果您知道返回的节点是 SKLabelNode(或 nil),则可以对其进行转换。我还建议避免嵌套过多的函数调用,因为这会使您的代码更难阅读。另外,您可以在这里使用搜索功能:
id label = [self childNodeWithName:@"//bar1-playPauseButtonLabel"];
((SKLabelNode*)label).text = @"whatever";
The //
prefix indicates that you want to search the node graph starting with self
recursively. This will return the first node found that matches the given name.
该//
前缀表明您要搜索开始节点图self
递归。这将返回找到的第一个与给定名称匹配的节点。