[英]How to cause a screen transition when an SKSpriteNode is tapped
我正在使用spritekit框架構建一個iOS應用程序,並且當用戶點擊“開始”按鈕時,我試圖使屏幕過渡到下一個場景。 我可以切換屏幕並加載下一個場景,但是當用戶點擊屏幕上的“ ANYWHERE”時會發生這種情況。我只希望在用戶點擊“開始” spritenode時發生這種情況。 我使用SKSpriteNode創建了開始按鈕,並使用name屬性將其命名為startButton。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *doNothing = [SKAction moveByX:0 y:0 duration:0];
if (startButton != nil)
{
[startButton runAction:doNothing completion:^{
SKScene *gamescene = [[GameScene alloc] initWithSize:self.size];
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:0.0];
[self.view presentScene:gamescene transition:transition];
}];
}
}
謝謝!
代替
if(startButton != nil){ }
嘗試在touchesBegan方法中使用它
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if([node.name isEqualTo:@"startButton"]){
//start scene
}
確保像這樣設置開始按鈕(標簽或精靈)的名稱
start.name = @"startButton";
編輯:
不要使用
[startButton runAction:doNothing completion:^{
....
}
而是立即過渡
if([node.name isEqualTo:@"startButton"]){
SKScene *gamescene = [[GameScene alloc] initWithSize:self.size];
SKTransition *transition = [SKTransition doorsCloseHorizontalWithDuration:0.0];
[self.view presentScene:gamescene transition:transition];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.