0

我想在 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);
}

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

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

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

4

2 回答 2

0

尝试使用 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]; 
 }
于 2012-06-05T11:01:38.947 回答
0

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

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

于 2012-06-05T11:07:52.590 回答