"play"という名前プロパティを持つSKSpriteNode playAgainButtonでゲームを再開するのに問題があります。ゲームオーバーメソッドは正常に動作していますが、タッチの開始機能でゲームを再開できません。ここにそれぞれの機能があります。どのような助けが素晴らしいだろう、私はこの問題でしばらくの間立ち往生しており、doc(CGPoint)convertPoint:(CGPoint)fromNode:(SKNode *)node;と - (NSArray *)nodesAtPoint:(CGPoint)p;SpriteKit/Obj-C - nodesatPointでタッチを開始する
- (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];
}
}
}
とゲームオーバー機能:
-(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];
}
ありがとうございました。ここ
は、私はタッチを持っているコードが機能を始めています。
1)touchedBeganイベントではなくtouchedEndedイベントで行います。 UX(ユーザーエクスペリエンス)の観点から、私は誤ってボタンに触れ、指を離す前に杖をスライドさせて動かしないでください。 2)あなたのゲームを「再開」することはできません。あなたがやっていることは、タイマーを再起動することです。あなたの主人公は、最後に画面に触れたときから加速しています。 – Knight0fDragon
ゲームを再起動する最も簡単で安全な方法は、新しいgamesceneインスタンスを作成することです – Knight0fDragon
ありがとうございました@ Knight0fDragon touchesEndedは理にかなっています。ええ、それは本当の "再起動"ではありませんが、スコア、タイマーなどをリセットし、そこから再び始まります。私の問題はnodesatPointメソッドです。私はそれを呼び出すために変更しました。つまり、SKSpriteNode * node = [self nodesatPoint:location]は配列を返していますが、その場所から私の "再生"ノードの再起動ボタンを認識しません。 – SalokinSwift