How to display alert view in SpriteKit

↘锁芯ラ 提交于 2019-12-11 11:43:12

问题


I wish to display UIAlertView in my SpriteKit game (saying there is not enough coins to select an item). I have set UIAlertView *alertView; property in my (only) ViewController and initialized it.

However I can't access this from my Scene (tried to call a public method but it didn't work).

How can I access my ViewController and its properties form the scene?


回答1:


As to how to access the ViewController (and its properties) from your SKScene or any SKNode, I would save a pointer inside those SKNodes after creation.

@interface YourScene : SKScene
  @property (weak,nonatomic) UIViewController * presentingViewController;
@end

// inside the ViewController
YourScene * scene = [YourScene new];
scene.presentingViewController = self;    
[skView presentScene:scene];

An answer to your title question: Displaying alertViews in SpriteKit

You don't have to put the alertView property in your ViewController; you can just put it in your SKScene, or wherever you like. You don't even have to set a delegate, but if you want to just have your SKScene subclass conform to the UIAlertViewDelegate protocol.

@interface YourNode : SKNode<UIAlertViewDelegate>
  - (void) displayAlert;
@end

// ...

@implementation YourNode
  - (void) displayAlert {
    UIAlertView * alertView = [UIAlertView alloc] initWithTitle:@"Your title" message:@"Your message is this message" delegate:self cancelButtonTitle:@"Cancel me" otherButtonTitles:@"Whatever", nil];
    [alertView show];
  }
@end


来源:https://stackoverflow.com/questions/24077426/how-to-display-alert-view-in-spritekit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!