0

UIAccelerometerベースのアプリケーションに統合するためのヘルプを探していcocos2dます。私の現在のデザインはおおよそ次のとおりです。

1 --私のHelloWorldLayerレイヤーにはプライベート変数 (という名前のクラス)Managerがあり、これをデータ マネージャーと呼びます。UI に情報を提供する主要なデータ管理クラスです。

2 --HelloWorldLayerupdate メソッドが、データ マネージャの update メソッドを呼び出します。

3 --accelerationMeterデータ マネージャーは、加速度計データを提供し、デバイスの現在の加速度を追跡するために使用しているカスタム データ オブジェクトを更新する新しいオブジェクト (という名前のクラス) を割り当てます。

4 --accelerationMeterクラスは加速度計を次のように設定します。

-(id) init
{
    if (self == [super init])
    {
    // the below call is only used if this object extends CCLayer. Right now it extends NSObject
        // self.isAccelerometerEnabled = YES;
        [[UIAccelerometer sharedAccelerometer] setUpdateInterval: 1/60];
        [[UIAccelerometer sharedAccelerometer] setDelegate: self];    

    // my data type to store acceleration information
        self.currVector = [[vector alloc] init];
    }
    return self;
}

// method called when the UIAccelerometer updates
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
    [self currVector].x = acceleration.x;
    [self currVector].y = acceleration.y;
    NSLog([NSString stringWithFormat:@"accelerometer: x = %f, y = %f",self.currVector.x,self.currVector.y]);
}

5 -- (一見正確に) からのデータ収集が 1 秒間成功した後、「加速度計」メソッドUIAccelerometerNSLog行がログ記録を停止します。したがって、委任されたメソッドは呼び出されなくなります。

6 --要求に応じて、加速度計を統合するために使用するコードを次に示します。

// source: HelloWorldLayer.mm
// purpose: update scene
-(void) update: (ccTime) dt
{
<snip>
    world->Step(dt, velocityIterations, positionIterations);
    [self updateData];
<snip>
}

// source: HelloWorldLayer.mm
// purpose: update data and use that for the accelerometer display
-(void) updateData
{
    if(dataManager)
    {
        [dataManager update]
        <snip>
        float accelX = [dataManager currentAcceleration].x;
        float accelY = [dataManager currentAcceleration].y;
        <snip>
    }
}


// source: Manager.m (the data manager class)
// called by updater method copied above this
-(void) update
{
    // replace outdated current accleration with the updated current accel
    // right now, the accelMonitor class just returns the value of the current acceleration so I will omit the monitor class
    currentAcceleration = [accelMonitor getCurrent];
    NSLog(@"Manger: Update called");
}

// source accelerationMeter.m
// called by (through an intermediate class) Manager
// I have verified that this method continues to be called after the accelerometer stops working
-(vector*) getCurrentAcceleration
{
    NSLog(@"accelerationMeter: getCurrentAcceleration called");
    return self.currVector;
}

私は IOS シミュレーターを使用していないことに注意してください。デバイスのコンソールからログを読んでいます。

主な質問は次のとおりです。
A -- 加速度計の委譲メソッドの実行が停止するのはなぜですか?
B -- 加速度計のデータを 1 秒以上利用できるようにするには、どうすればこれを解決できますか?

4

1 に答える 1

0

解決しました!

最終的に、accelerationMeter をノードとして HelloWorldLayer レイヤーに追加しました。これは、実行ループが範囲外になったり、モードが切り替わったりすることと関係があると思います。そのため、加速度計がデータを収集できなくなりますが、よくわかりません。

于 2013-08-12T14:45:35.853 に答える