我正在sprite kit中创建一个标签并设置初始大小.由于应用程序要进行本地化,因此其他语言中的单词可能比英语版本更长.因此,如何调整标签的字体大小以适合某个宽度,在这种情况下是按钮.
myLabel = SKLabelNode(fontNamed: "Arial")
myLabel.text = "Drag this label"
myLabel.fontSize = 20
Run Code Online (Sandbox Code Playgroud) 目前我正在使用SpriteKit编写一个小应用程序,它工作得非常好,但唯一的问题是SKLabelNode,我使用以下正常代码初始化:
?self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Futura"];
self.scoreLabel.fontSize = 190.0f;
self.scoreLabel.color = [SKColor whiteColor];
self.scoreLabel.alpha = .2;
self.scoreLabel.text = @"00";
self.scoreLabel.position = CGPointMake(screenWidth/2,self.scoreLabel.frame.size.height/2);
[self addChild:self.scoreLabel];
Run Code Online (Sandbox Code Playgroud)
还有很多东西可以初始化,但它们不会影响任何东西.如果我注释掉上面的代码,应用程序会在平时加载.使用SKLabelNode,它可以将负载延迟几秒钟......
希望有人能帮助我.
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = NSTextAlignment.right
paragraph.tailIndent = -3
let shadow = NSShadow()
shadow.shadowBlurRadius = 5
shadow.shadowColor = UIColor.gray
shadow.shadowOffset = CGSize(width: 2, height: -2)
let text = NSMutableAttributedString(string: "MY LABEL", attributes:
[NSShadowAttributeName : shadow,
NSStrokeColorAttributeName : UIColor.black,
NSStrokeWidthAttributeName : -3,
NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName : UIFont(name: "MyFont", size: 24) as Any,
NSParagraphStyleAttributeName : paragraph])
let node = SKLabelNode()
addChild(node)
node.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
它有效,但没有阴影。我可以使用相同的NSMutableAttributedString上著名的ASAttributedLabel和它的作品不错,有一个影子,但我想使用SKLabelNode,以实现更好的性能IOS11。ASAttributedLabel可能会滞后于动态场景:(
nsattributedstring nsmutableattributedstring sprite-kit sklabelnode swift
真的没有办法设计SKLabelNode吗?我的意思是除了改变字体和颜色.我知道我可以做一些事情,比如通过在第一个后面创建第二个SKLabelNode来添加投影(已经排序很糟糕).为我的字体创建图像也会很糟糕,原因很明显.看起来奇怪的是你无法打破无聊的扁平文字.
我的意思是..即使spritekit节点计数有一个很酷的渐变风格......这是怎么做的?!

好吧所以我在这里尝试重新创建渐变效果.我已经尝试了混合模式,颜色和colorBlendFactor的多种组合的每种组合.
α

乘

加

这是代码
let testNode = SKLabelNode(text: "hey there")
testNode.fontSize = 30
testNode.color = SKColor.blueColor()
testNode.colorBlendFactor = 1
testNode.fontName = "Helvetica-Bold"
testNode.blendMode = SKBlendMode.Multiply
testNode.colorBlendFactor = 0.6
testNode.position = CGPoint(x: self.size.width/2, y: self.size.height/2)
self.addChild(testNode)
Run Code Online (Sandbox Code Playgroud) 我正在制作一个Sprite Kit应用程序,在我的场景中我添加了一个SKLabelNode.当我加载SKScene时,我注意到一个非常大的滞后尖峰.在分析应用程序后,我发现它来自创建一个带有纸莎草字体的SKLabelNode(尽管字体无关紧要).当我删除标签时,场景几乎立即启动,但使用标签需要额外的1-3秒.
我很确定这是从加载字体开始,当我回到主菜单并再次玩游戏时,它会立即重新开始.
现在是否有一种方法可以提前预加载字体,这样当玩家选择等级时就没有大的停顿?
我正在开发一款游戏,我需要实现顶级栏,持有得分和其他东西等细节.
我的顶栏应该有以下(从左到右): - 一个50x50的图标,根据级别改变,但它的大小相同 - 一个标签,其中一些文字根据级别而变化 - ...其他元素
问题是当标签文本发生变化时,标签有时离图标太远,而其他时间则超过图标(取决于文本长度)
我以为我理解定位,但显然我没有...
//create array for level text label
levelTextArray = [[NSArray alloc] initWithObjects:@"\"Uuu! Bubbles!\"",@"\"Noob\"",@"\"I’m getting it..\"",@"\"This is easy\"",@"\"Wha?\"",@"\"It’s ooon now!\"",@"\"Come on..\"",@"\"Dude…\"",@"\"You’re pushing it..\"",@"\"I’ll show you!!\"",@"\"AAAAAAA!!!\"",@"\"Holy Bubbles… \"",@"\"Ninja mode on!\"",@"\"I’m on fire!!\"",@"\"The wheel's spinning, but the hamsters dead. \"", nil];
//add level text label
levelTextLabel = [[SKLabelNode alloc] init];
levelTextLabel.text = levelTextArray[0];
levelTextLabel.position = CGPointMake(60, CGRectGetMidY(scoreImage.frame)+5);
levelTextLabel.fontColor = [UIColor colorWithRed:0/255.0f green:1/255.0f blue:0/255.0f alpha:1.0f];
levelTextLabel.fontName = @"Noteworthy-Light";
levelTextLabel.fontSize = 14.0;
[self addChild:levelTextLabel];
Run Code Online (Sandbox Code Playgroud)
scoreImage是这种情况下的图标.另外..为了使图像完全出现在视图中,我将它定位如下:
scoreImage …Run Code Online (Sandbox Code Playgroud) 我正在使用SKLabelNode.我正在创建它并将它作为一个孩子添加到我的场景中它显示没有问题,但当我尝试使用colorizeWithColor()方法更改它的颜色(而不是fontColor)时,标签淡出.
这是问题的界限:
myLabel.runAction(SKAction.colorizeWithColor(SKColor.blueColor(), colorBlendFactor: 1.0, duration: duration))
Run Code Online (Sandbox Code Playgroud)
完成此操作后,我在控制台上打印了myLabel.color属性,这是我得到的:
Optional(UIDeviceRGBColorSpace 0.99178 0.99178 1 0.00822043)
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,alpha值几乎为0,所以我猜这就是标签消失的原因,但我不明白为什么会发生这种情况.
提前致谢.
更新:好的,所以我发现实际上这是我的不好,我在询问前没有搜索过.以下是有关colorizeWithColor方法的文档:
此操作只能由SKSpriteNode对象执行.当动作执行时,精灵的color和colorBlendFactor属性将动画为其新值.
那么也许任何人都知道一个好的工作来着色一直在更新的SKLabelNode?
!!!最后更新!!!
我能够找到一个解决方案,但0x141E提出了一个更好的解决方案,我使用并创建了下一个方法,当你需要从颜色A转换到颜色B时,它很有效.在0x141E建议的解决方案中你曾经回到fontColor,当你改变颜色时它会闪烁.在我的情况下,它改变了fontColor,而不是color属性,这导致一个非常好的过渡(当然不是很好).
再次感谢0x141E真的很好的方法!
这是我的解决方案:
当你调用参数withDuration = 0.5的方法时,这种特殊情况很有效.但是如果你需要其他时间,你可以使用发送的withDuration参数或乘数来玩,在我的代码中是5.即使当然应该是更好的解决方案,所以如果你找到请分享.为了我的需要,这个很棒.
首先是一个视频,以便您了解它的工作原理:https://www.youtube.com/watch?v = Zzz8Bn0-hUA&feature = youroutu.be
func changeColorForLabelNode(labelNode: SKLabelNode, toColor: SKColor, withDuration: NSTimeInterval) {
labelNode.runAction(SKAction.customActionWithDuration(withDuration, actionBlock: {
node, elapsedTime in
let label = node as SKLabelNode
let toColorComponents = CGColorGetComponents(toColor.CGColor)
let fromColorComponents = CGColorGetComponents(label.fontColor.CGColor)
let finalRed = fromColorComponents[0] + (toColorComponents[0] - fromColorComponents[0])*CGFloat(elapsedTime / (CGFloat(withDuration)*5))
let finalGreen = fromColorComponents[1] + (toColorComponents[1] - fromColorComponents[1])*CGFloat(elapsedTime / …Run Code Online (Sandbox Code Playgroud) 我有一个SKLabelNode设置来显示分数变量后跟Highscore变量
scoreLabel.text = "\(score)/\(classicHScoreInt)"
现在,一切都很好,但我希望classicHScoreInt是一个较小的字体,也许是一个不同的颜色.这怎么可能?
classicHScoreInt 是(如上所述)一个整数,所以是 score
我正在做一个小的SpriteKit游戏。
我在主屏幕上有一个“提示”部分,我想输入和输出,每次显示不同的提示。
我写了一个自己写的方法,但是它很杂乱,我相信有更好的方法可以完成。我希望有人可以向我展示一种我可能错过的方式(或者在做某事时走了很长一段路)。
这是我目前的做法:
func createTipsLabels(){
//create SKLabelNodes
//add properties to Labels
//tip1Label... etc
//tip2Label... etc
//tip3Label... etc
//now animate (or pulse) in tips label, one at a time...
let tSeq = SKAction.sequence([
SKAction.runBlock(self.fadeTip1In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip1Out),
SKAction.waitForDuration(2),
SKAction.runBlock(self.fadeTip2In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip2Out),
SKAction.waitForDuration(2),
SKAction.runBlock(self.fadeTip3In),
SKAction.waitForDuration(5),
SKAction.runBlock(self.fadeTip3Out),
SKAction.waitForDuration(2),
])
runAction(SKAction.repeatActionForever(tSeq)) //...the repeat forever
}
//put in separate methods to allow to be called in runBlocks above
func fadeTip1In() { tip1Label.alpha = 0; tip1Label.runAction(SKAction.fadeInWithDuration(1)) ; print("1") }
func fadeTip1Out(){ tip1Label.alpha = 1; tip1Label.runAction(SKAction.fadeOutWithDuration(1)); print("2") …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个显示精灵套件游戏分数的 sklabelnode。文本最初显示 0,但在文本更改时消失。该节点在类声明之后与分数一起在全局范围内声明,这是一个整数。这是代码:
var scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
override func didMoveToView(view: SKView) {
scoreLabel.text = String(score)
scoreLabel.fontSize = 48
scoreLabel.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMaxY(self.frame)-50)
self.addChild(scoreLabel)
}
func updateScoreAndLabel()->(scoreint:Int,label:SKLabelNode){
score++
scoreLabel.text = String(score)
return (score, scoreLabel)
}
Run Code Online (Sandbox Code Playgroud)
updateScoreAndLabel() 是从另一个对这个问题不重要的函数调用的,所以我没有包括它。问题不在于我相信的函数调用。
我想制作像'乒乓球'这样的小游戏.一切都很好,但是现在,当我想添加一个分数时,游戏会冻结.我将此代码用于我的另一个项目,一切都很好.
以下是该score部分的代码:
-(void)scoreCount{
score ++;
if(scoreLabel == nil){
scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"ROTORcapExtendedBold"];
scoreLabel.fontSize = 40;
scoreLabel.position = CGPointMake(self.frame.size.width/2,self.frame.size.height/3);
scoreLabel.zPosition = 0;
}
[self addChild:scoreLabel];
scoreLabel.text = [NSString stringWithFormat:@"%ld",(long)score];
}
Run Code Online (Sandbox Code Playgroud)
在控制台我收到此消息:
终止应用程序由于未捕获的异常 'NSInvalidArgumentException',理由是: 'Attemped添加已经具有父SKNode:名称:'(null)的文字: '1' 的fontName: 'ROTORcapExtendedBold' 位置:{189.33333,106.66666}"
删除该行后scoreLabel == nil,应用程序不会冻结,但屏幕上的分数会复制旧分数并使分数无法读取.
我该如何解决?
sklabelnode ×11
sprite-kit ×11
swift ×6
ios ×5
objective-c ×5
cocoa ×1
ios8 ×1
nsstring ×1
optimization ×1
skaction ×1
sknode ×1