2017-07-15 72 views
1

我知道这个问题之前已经被问过了,但我认为答案可能需要更新,因为它目前不适用于使用Xcode 8.3.3 for Mac OS的SpriteKit。看完这个post后,我意识到可以在AppDelegate中完成的事情现在应该在ViewController中完成,但它不起作用。任何人有任何建议,为什么这不起作用?下面是我的各种类的样子:SpriteKit MouseMoved不能正常工作

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

你没问究竟同样的问题在几个星期前? –

+0

是的。我完全忘记了这一点。无论哪种方式,没有人发布过答案。我将删除旧的问题。 – 02fentym

+0

如果你有兴趣,我可以告诉你如何在Swift中做到这一点。我记得你的旧主题,因为我在Swift中发布了代码,然后在意识到你在Objective-C中需要它之后将其删除。不管你是用Objective-C还是Swift编写它,这个概念都是一样的。 –

回答

3

你是你的第一个响应者设置为skView,你需要将其设置为skView.scene,以便鼠标响应您的场景实例,而不是您的视图实例。

步骤1:将你的窗口代码viewDidAppear

第2步:改变你的第一个响应者使用[self.skView.window makeFirstResponder:self.skView.scene];

你之所以需要做的是在viewDidAppear事件现场是因为windowSKViewviewDidLoad事件期间为nil。假如你在斯威夫特做到了这一点,self.skView.window!.setsAcceptedMouseMovements = true就失败了你(SWIFT是一个卓越的语言,我会建议使用它)

#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 
+0

像这样的东西? '[self.skView.window makeFirstResponder:self.skView.scene];'不起作用。我在那里写了ViewController.m btw。 – 02fentym

+1

不要在did load事件中做你的窗口代码,在'viewDidAppear'事件中执行它 – Knight0fDragon

+0

工作。你能详细说明为什么吗? – 02fentym