【问题标题】:object is moving in the accelerometer物体在加速度计中移动
【发布时间】:2012-06-05 10:55:38
【问题描述】:

我想在 cocos2d 中创建一个游戏,其中一个对象正在运行。

我想根据设备加速度计左右移动这个对象。 我正在获取加速度计的值并更新对象的位置。甚至我可以在 LOG 中看到新位置。但物体并没有从它的位置移动。这是我在应用程序中编写的代码。

- (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration1 {

acceleration = acceleration1;
CGPoint position = mainPlayer.position;

if(acceleration.y < -0.25) {  // tilting the device to the right
    position.y -= 0.25;
} else if (acceleration.y > 0.25) {  // tilting the device to the left
    position.y += 0.25;
} else {

} //    newPosition = position;

CCAction *action = [CCMoveTo actionWithDuration:0.01 position:position];
[mainPlayer runAction:action]; 
//    mainPlayer.position = ccp(position.x, position.y);
}

我已经通过直接设置位置和使用动作来尝试两种方式。

任何人都知道为什么会发生此问题或加速度计需要任何设置才能工作。

请提供此类示例的任何链接。

【问题讨论】:

    标签: cocos2d-iphone accelerometer ccsprite acceleration


    【解决方案1】:

    尝试使用 MoveBy 而不是 MoveTo:

     - (void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration1 {
    
        acceleration = acceleration1;
        CGPoint position = ccp(0,0);
    
        if(acceleration.y < -0.25) {  // tilting the device to the right
            position.y = 0.25;
        } else if (acceleration.y > 0.25) {  // tilting the device to the left
            position.y = 0.25;
        }
    
        CCAction *action = [CCMoveBy actionWithDuration:0.01 position:position];
        [mainPlayer runAction:action]; 
     }
    

    【讨论】:

      【解决方案2】:

      我建议不要从加速度计运行操作,因为此方法每秒调用 10 次(我不记得 cocos2d 是如何初始化加速度计的),这将导致 runAction 方法每秒调用 10 次并且可能会停止之前的操作,这就是为什么您看不到它移动的原因。

      显示有关如何创建主播放器以及如何将其添加到场景/图层的更多代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多