检测精灵套件中的抖动 [英] Detecting shakes in sprite kit

查看:103
本文介绍了检测精灵套件中的抖动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现摇晃有问题.这是Sprit Kit中的一个场景,我定义了运动检测器,如下所示:

I have a problem with detecting a shake. It's a skscene in the Sprit Kit and I defined the motion detector like this:

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event{

}

-(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {

NSLog(@"test?");

}

我的错误在哪里?我是否必须像使用UIGestureRecognizer那样实现它?

Where is my mistake? Do I have to implement it like I had to do it with the UIGestureRecognizer?

在此先感谢您(对不起我的英语不好) 朱利安

Thanks in advance (and sorry for my bad english) Julian

推荐答案

显然,您无法检测到来自SKScene子类(例如GameScene)的震动事件.但是,您可以从诸如GameViewController之类的视图控制器中检测到它们.触发震动事件时,可以从视图控制器的GameScene中调用震动处理程序.

Apparently, you can't detect shake events from a SKScene subclass, such as GameScene. However, you can detect them from a view controller, such as GameViewController. When a shake event is triggered, you can call a shake handler in GameScene from the view controller.

在您的GameViewController.m中,添加它以检测震动事件

In your GameViewController.m, add this to detect shake events

- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    if (motion == UIEventSubtypeMotionShake) {
        SKView *skView = (SKView *)self.view;
        GameScene *scene = (GameScene *)skView.scene;
        // Call a function in the GameScene
        [scene shake];
    }
}

将此添加到GameScene.h中的@interface

Add this to the @interface in GameScene.h

- (void) shake;

将此添加到GameScene.m

Add this to GameScene.m

- (void) shake {
    NSLog(@"shake");
}

这篇关于检测精灵套件中的抖动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆