我知道之前已经问过这个问题,但我认为答案可能需要更新,因为它目前在使用Xcode 8.3.3 for Mac OS的SpriteKit中无效。在阅读了这个post之后,我意识到在AppDelegate中曾经可以完成的任务现在应该在ViewController中完成,但是它不起作用。任何人都有任何建议,为什么这不起作用?这是我的各种类的样子:
ViewController.m
#import "ViewController.h"
#import "GameScene.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Load the SKScene from 'GameScene.sks'
GameScene *scene = (GameScene *)[SKScene nodeWithFileNamed:@"GameScene"];
// Set the scale mode to scale to fit the window
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene
[self.skView presentScene:scene];
self.skView.showsFPS = YES;
self.skView.showsNodeCount = YES;
//added in hopes that mouse moved events would be captured
[self.skView.window setAcceptsMouseMovedEvents:YES];
[self.skView.window setInitialFirstResponder:self.skView];
}
@end
GameScene.m
#import "GameScene.h"
@implementation GameScene
- (void)didMoveToView:(SKView *)view {
}
-(void) mouseMoved:(NSEvent *)event {
NSLog(@"blah");
}
-(void)update:(CFTimeInterval)currentTime {
// Called before each frame is rendered
}
@end
答案 0 :(得分:4)
您要将第一响应者设置为skView
,您需要将其设置为skView.scene
,以便鼠标响应您的场景实例,而不是您的视图实例。
第1步:将您的窗口代码移至viewDidAppear
第2步:使用[self.skView.window makeFirstResponder:self.skView.scene];
您需要在viewDidAppear
事件中执行此操作的原因是因为window
SKView
nil
期间viewDidLoad
self.skView.window!.setsAcceptedMouseMovements = true
#import "ViewController.h"
#import "GameScene.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Load the SKScene from 'GameScene.sks'
GameScene *scene = (GameScene *)[SKScene nodeWithFileNamed:@"GameScene"];
// Set the scale mode to scale to fit the window
scene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene
[self.skView presentScene:scene];
self.skView.showsFPS = YES;
self.skView.showsNodeCount = YES;
}
- (void) viewDidAppear {
//added in hopes that mouse moved events would be captured
[self.skView.window setAcceptsMouseMovedEvents:YES];
[self.skView.window setInitialFirstResponder:self.skView];
[self.skView.window makeFirstResponder:self.skView.scene];
}
@end
{{1}}如果你在Swift中做到这一点,那么{{1}}就会失败(Swift是一种优秀的语言,我建议使用它)
{{1}}