0

motionManager.gravity を使用してノードを移動するアプリがあります。

これは私のコードです

self.motionManager = [[CMMotionManager alloc] init];
    self.motionManager.deviceMotionUpdateInterval = 0.005f;

    self.motionQueue = [[NSOperationQueue alloc] init];
    self.motionQueue.name = [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingString:@".motion"];



    self.updatePosition = NO;

    [self.motionManager startDeviceMotionUpdatesToQueue:self.motionQueue withHandler:^(CMDeviceMotion *motion, NSError *error) {


        @synchronized(self) {
            NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

            [formatter setNumberStyle:NSNumberFormatterDecimalStyle];

            [formatter setMaximumFractionDigits:2];

            [formatter setRoundingMode: NSNumberFormatterRoundUp];


            {


                numberString = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.x / 20.0 *200]];
                numberStringy = [formatter stringFromNumber:[NSNumber numberWithFloat:motion.gravity.y / 20.0 *200]];
                n = [numberString intValue];

                /*NSLog(@"ball.center x:%d",n);*/
                y = [numberStringy intValue];

                /*NSLog(@"ball.center y:%f",balla.center.y);*/

            }
            self.gravity = motion.gravity;
            self.updatePosition = YES;
        }
    }];

次に、displayLink を使用してスプライト ノードを更新および移動します。すべてがうまく機能しており、画面の端や特定の場所でノードを停止させることができます。私が知りたいのは、ノードが動いているか停止しているかを確認する方法があるかどうかです: 1. エッジで 2. コードで特定の場所で。3. デバイスは水平に保たれているため、加速度計は 0 より大きい n または y の数値を生成していません。

スプライト ノードが動いているときは音を鳴らしたいが、スプライト ノードが動いていないときは音を止めたい。
何か案は?

4

1 に答える 1

0

SKPhysicsBodyには というプロパティがありますresting。それを使って動きをテストします。

if(myNode.physicsBody.resting) {
    // node is not moving
} else {
    // node is moving
}

velocity特定の軸だけの動きをチェックしたい場合にも、このプロパティを使用できます。

于 2015-06-29T21:07:03.537 に答える