Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c SpriteKit鼠标移动不工作_Objective C_Macos_Sprite Kit_Mouseevent - Fatal编程技术网

Objective c SpriteKit鼠标移动不工作

Objective c SpriteKit鼠标移动不工作,objective-c,macos,sprite-kit,mouseevent,Objective C,Macos,Sprite Kit,Mouseevent,我知道以前有人问过这个问题,但我认为答案可能需要更新,因为它目前在使用Xcode 8.3.3 for Mac OS的SpriteKit中不起作用。读了这篇文章后,我意识到以前可以在AppDelegate中完成的事情现在应该在ViewController中完成,但它不起作用。有人对为什么这不起作用有什么建议吗?下面是我的各种课程的样子: ViewController.m #import "ViewController.h" #import "GameScene.h" @implementatio

我知道以前有人问过这个问题,但我认为答案可能需要更新,因为它目前在使用Xcode 8.3.3 for Mac OS的SpriteKit中不起作用。读了这篇文章后,我意识到以前可以在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
#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
游戏场景.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
#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

如果要将第一响应程序设置为
skView
,则需要将其设置为
skView.scene
,以便鼠标响应的是场景实例,而不是视图实例

步骤1:将窗口代码移动到
viewdide

步骤2:使用
[self.skView.window makeFirstResponder:self.skView.scene]将第一响应者更改为场景

之所以需要在
viewdideappear
事件中执行此操作,是因为
SKView
window
viewdideload
事件期间为
nil
。您是否在Swift,
self.skView.window!中完成了此操作!。SetAcceptedMouseMotations=true
对您可能会失败(Swift是一种高级语言,我建议您使用它)


几周前你不是问了同样的问题吗?是的。我完全忘记了。不管怎样,都没有人给出答案。我将删除旧问题。如果您感兴趣,我可以向您展示如何使用Swift。我记得你的老话题,因为我在Swift中发布了代码,然后在意识到你想在Objective-C中使用它后将其删除。我认为,无论你是用Objective-C还是Swift编写它,概念都是一样的。是的,那太好了。我可以把Swift翻译成Objective-C,像这样吗<代码>[self.skView.window makeFirstResponder:self.skView.scene]它不工作。我在ViewController.m btw.Don’不要在did load事件中执行窗口代码,在
viewdide出现
event中执行加载时SKView的窗口不是根视图控制器的窗口,在视图出现后,您知道您的窗口在根视图控制器上,因为您可以看到它。我理解。谢谢