How to show iAd on a certain SKScene and hide it on the other one

爷,独闯天下 提交于 2020-01-02 18:37:11

问题


I'm making a simple game and I would like to add an iAd at the top of the Game Over Screen. I could add it on to the UIViewController, but then it would show up while playing, which is something I don't want.

Is there a way to make the iAd appear only on a certain SKScene? Thanks for all your help!


回答1:


The most clean solution is to declare and implement a protocol to let the UIViewController know that it should hide the ad.

@protocol MySceneDelegate <NSObject>
- (void)hideAd;
- (void)showAd;
@end

@interface MyScene : SKScene
@property (weak) id <MySceneDelegate> delegate;
@end

View controller that shows the scene should implement a hideAd method and set itself as a delegate of the scene. Example:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view, etc.
    ...

    // Set the delegate
    [scene setDelegate:self];

    // Present the scene.
    [skView presentScene:scene];
}

Then in the scene you don't want to show an ad, you can call the hideAd method of the view controller which was set as a delegate:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    /* Called when a touch begins */

    if ([_delegate respondsToSelector:@selector(hideAd)])
    {
        [_delegate performSelector:@selector(hideAd)];
    }
}

You can tell the view controller to show the ad in the same way. Hope it helps.



来源:https://stackoverflow.com/questions/22154972/how-to-show-iad-on-a-certain-skscene-and-hide-it-on-the-other-one

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