我无法使用我的SKSpriteNode playAgainButton重启我的游戏,其名称属性为" play"。游戏结束方法工作正常,但我无法在触摸开始功能重启游戏。以下是各自的功能。任何帮助都会很棒,我已经在这个问题上停留了一段时间,并在doc中尝试了几种不同的方法,包括 - (CGPoint)convertPoint:(CGPoint)指向fromNode:(SKNode *)节点;和 - (NSArray *)nodesAtPoint:(CGPoint)p;
这是我在触摸开始功能中的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
// Jumping fucntionality
[mainChar.physicsBody applyForce:CGVectorMake(0, 75000)];
// Play Again Button functionality
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
SKNode *node;
NSArray *nodes = [node nodesAtPoint:location];
for (node in nodes) {
if ([node.name isEqual: @"play"]){
score = 0;
[node removeFromParent];
[self.yourScoreLabel removeFromParent];
[self.finalScoreLabel removeFromParent];
self.scene.view.paused = NO;
scoreLabel.text = [NSString stringWithFormat:@"Score %ld", (long)score];
[self startTimers];
}
}
}
gameOver功能:
-(void)gameOver{
self.scene.view.paused = YES;
[planetTimer invalidate];
[asteroidTimer invalidate];
self.yourScoreLabel = [SKLabelNode new];
self.yourScoreLabel.position = CGPointMake(0, 200);
self.yourScoreLabel.fontSize = 100;
self.yourScoreLabel.zPosition = 1;
self.yourScoreLabel.text = @"Your score:";
[self addChild:self.yourScoreLabel];
self.finalScoreLabel = [SKLabelNode new];
self.finalScoreLabel.position = CGPointMake(0, 0);
self.finalScoreLabel.fontSize = 200;
self.finalScoreLabel.zPosition =1;
self.finalScoreLabel.text = [NSString stringWithFormat:@"%ld", (long)score];
[self addChild:self.finalScoreLabel];
SKSpriteNode * playAgainButton = [SKSpriteNode spriteNodeWithImageNamed:@"play"];
playAgainButton.position = CGPointMake(0, -200);
playAgainButton.name = @"play";
playAgainButton.zPosition = 1;
[self addChild:playAgainButton];
}
谢谢。
答案 0 :(得分:1)
您正在使用[touch locationInView:view]
,它将返回视图坐标系中的位置。在SpriteKit中,您需要处理场景的坐标系,它可能处于不同的间隔。要获取场景中的位置,只需执行CGPoint location = [touch locationInNode:self];