从viewcontroller设置SKScene委托,找不到委托

时间:2014-08-25 19:49:05

标签: objective-c delegates sprite-kit header-files

尝试从SKScene打开视图控制器。

发现https://stackoverflow.com/a/20072795/1686319非常有帮助,但是在尝试从视图控制器设置委托时出现错误。

  

没有可见的@interface为' SKScene'声明选择器   ' setDelegate:'

EABMyScene.h

#import <SpriteKit/SpriteKit.h>

@protocol EABMySceneDelegate <NSObject>
-(void)doSomething;
@end

@interface EABMyScene : SKScene {

}
@property (nonatomic, weak) id <EABMySceneDelegate> delegate;
@end

有什么想法吗?

更新

EABViewController.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>

@protocol ViewControllerDelegate <NSObject>
-(void) openTweetSheet;
@end

@interface EABViewController : UIViewController <ViewControllerDelegate>

@end

EABViewController.m

#import "EABViewController.h"
#import "EABMyScene.h"

@implementation EABViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view.
    SKView * skView = (SKView *)self.view;
    skView.showsFPS = YES;
    skView.showsNodeCount = YES;

    // Create and configure the scene.
    SKScene * scene = [EABMyScene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;

    [scene setDelegate:self];

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

-(void)openTweetSheet
{
    NSLog(@"Open Tweet Delegate Method");
}

EABMyScene.h

#import <SpriteKit/SpriteKit.h>

@interface EABMyScene : SKScene {

}

@property (nonatomic, weak) id <ViewControllerDelegate> delegate;

@end

错误:没有可见的@interface为&#39; SKScene&#39;声明选择器&#39; setDelegate:&#39;

1 个答案:

答案 0 :(得分:2)

类SKScene没有名为delegate的属性,因此语句

[scene setDelegate:self];

产生了编译错误。要解决此问题,请将场景强制转换为SKScene子类EABMyScene:

[(EABMyScene *)scene setDelegate:self];