gpt4 book ai didi

objective-c - SpriteKit MouseMoved 不工作

转载 作者:行者123 更新时间:2023-12-02 00:43:40 25 4
gpt4 key购买 nike

我知道之前有人问过这个问题,但我认为答案可能需要更新,因为它目前在使用 Xcode 8.3.3 for Mac OS 的 SpriteKit 中不起作用。看完这篇post ,我意识到以前可以在 AppDelegate 中完成的工作现在应该在 ViewController 中完成,但它不起作用。有人对为什么这不起作用有任何建议吗?这是我的各种类(class)的样子:

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

最佳答案

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

第 1 步:将窗口代码移动到 viewDidAppear

第 2 步:使用 [self.skView.window makeFirstResponder:self.skView.scene];

将您的第一响应者更改为场景

之所以需要在 viewDidAppear 事件中执行此操作,是因为 SKViewwindownilviewDidLoad 事件期间。如果您在 Swift 中完成此操作,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

关于objective-c - SpriteKit MouseMoved 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45117487/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com