gpt4 book ai didi

ios - SKLabelNode 触摸更新

转载 作者:行者123 更新时间:2023-11-29 12:39:39 26 4
gpt4 key购买 nike

我正在尝试找出异常行为的原因。我的 SpriteKit 场景有一个 SKLabelNode 作为属性。 SKLabelNode 在名为 levelSelected 的方法中初始化:

 -(void)levelSelected{
SKSpriteNode* bar = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:0.566f saturation:0.787f brightness:0.792f alpha:1] size:CGSizeMake(self.frame.size.width,60)];
bar.anchorPoint = CGPointMake(0, 0);
bar.position = CGPointMake(0, 50);
float space = (self.size.width - 45*6) / 7;
SKSpriteNode* letterBox = [SKSpriteNode spriteNodeWithImageNamed:@"letter_box_def"];
letterBox.anchorPoint = CGPointMake(0.5, 0.5);
letterBox.position = CGPointMake((space+((space + 45)*2)+22.5), 30);
[bar addChild:letterBox];
self.gl2 = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
self.gl2.fontColor = [SKColor blackColor];
self.gl2.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
self.gl2.fontSize = 30;
self.gl2.text = @"";
NSLog(@"%@", self.gl2);
[letterBox addChild:self.gl2];
[self addChild:bar];
self.gl2.text = @"D";
}

正如预期的那样,在运行时,节点显示“D”。我的触摸方法如下:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Called touchesBegan");
NSLog(@"Node : %@", self.gl2);
NSLog(@"Text : %@", self.gl2.text);
self.gl2.text = @"k";
NSLog(@"Node : %@", self.gl2);
NSLog(@"Text : %@", self.gl2.text);
}

现在,只需轻轻一按,标签就会从屏幕上消失。日志显示:

2014-08-14 06:15:34.841 Guess[3125:607363] Called touchesBegan
2014-08-14 06:15:34.842 Guess[3125:607363] Node : <SKLabelNode> name:'(null)' text:'D' fontName:'Arial' position:{0, 0}
2014-08-14 06:15:34.843 Guess[3125:607363] Text : D
2014-08-14 06:15:34.846 Guess[3125:607363] Node : <SKLabelNode> name:'(null)' text:'k' fontName:'Arial' position:{0, 0}
2014-08-14 06:15:34.847 Guess[3125:607363] Text : k

我试过的是使用另一种方法,从 levelSelected 方法调用:

-(void)levelSelected{
SKSpriteNode* bar = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithHue:0.566f saturation:0.787f brightness:0.792f alpha:1] size:CGSizeMake(self.frame.size.width,60)];
bar.anchorPoint = CGPointMake(0, 0);
bar.position = CGPointMake(0, 50);
float space = (self.size.width - 45*6) / 7;
SKSpriteNode* letterBox = [SKSpriteNode spriteNodeWithImageNamed:@"letter_box_def"];
letterBox.anchorPoint = CGPointMake(0.5, 0.5);
letterBox.position = CGPointMake((space+((space + 45)*2)+22.5), 30);
[bar addChild:letterBox];
self.gl2 = [SKLabelNode labelNodeWithFontNamed:@"Arial"];
self.gl2.fontColor = [SKColor blackColor];
self.gl2.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter;
self.gl2.fontSize = 30;
self.gl2.text = @"";
NSLog(@"%@", self.gl2);
[letterBox addChild:self.gl2];
[self addChild:bar];
self.gl2.text = @"D";
[self touchedSomething];
}
-(void)touchedSomething {
NSLog(@"Called touchedSomething");
NSLog(@"Node : %@", self.gl2);
NSLog(@"Text : %@", self.gl2.text);
self.gl2.text = @"k";
NSLog(@"Node : %@", self.gl2);
NSLog(@"Text : %@", self.gl2.text);
}

这非常有效,D 被 k 替换,生成日志:

2014-08-14 06:20:09.934 Guess[3132:608100] Called touchedSomething
2014-08-14 06:20:09.935 Guess[3132:608100] Node : <SKLabelNode> name:'(null)' text:'D' fontName:'Arial' position:{0, 0}
2014-08-14 06:20:09.935 Guess[3132:608100] Text : D
2014-08-14 06:20:09.938 Guess[3132:608100] Node : <SKLabelNode> name:'(null)' text:'k' fontName:'Arial' position:{0, 0}
2014-08-14 06:20:09.939 Guess[3132:608100] Text : k

但是,如果我从 levelSelected 中删除 [self touchedSomething] 调用并将其添加到 touchesBegan,它将不起作用,标签将再次从屏幕上消失。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(@"Called touchesBegan");
[self touchedSomething];
}

-(void)touchedSomething {
NSLog(@"Called touchedSomething");
NSLog(@"Node : %@", self.gl2);
NSLog(@"Text : %@", self.gl2.text);
self.gl2.text = @"k";
NSLog(@"Node : %@", self.gl2);
NSLog(@"Text : %@", self.gl2.text);
}

2014-08-14 06:24:49.492 Guess[3145:609098] Called touchesBegan
2014-08-14 06:24:49.493 Guess[3145:609098] Called touchedSomething
2014-08-14 06:24:49.494 Guess[3145:609098] Node : <SKLabelNode> name:'(null)' text:'D' fontName:'Arial' position:{0, 0}
2014-08-14 06:24:49.494 Guess[3145:609098] Text : D
2014-08-14 06:24:49.498 Guess[3145:609098] Node : <SKLabelNode> name:'(null)' text:'k' fontName:'Arial' position:{0, 0}
2014-08-14 06:24:49.498 Guess[3145:609098] Text : k

感谢您的帮助!

最佳答案

嗯,我能够(某种程度上)解决它。为标签定义一个 zPosition 修复它。为什么它在某些情况下可以正常工作,而在其他情况下却不能正常工作,这仍然是个谜。

关于ios - SKLabelNode 触摸更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25305844/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com