【问题标题】:Move CCSprite(cocos2d) object in landscape mode according to acceleration values根据加速度值在横向模式下移动 CCSprite(cocos2d) 对象
【发布时间】:2011-07-15 14:48:40
【问题描述】:

我正在准备一个游戏,我想根据加速度值移动一个对象,游戏处于横向模式。

对于游戏,我使用的是 cocos2d 框架,我正在根据加速度值改变精灵位置,这是我的加速度计代码

- (void)accelerometer:(UIAccelerometer*)accelerometer didAccelerate:(UIAcceleration*)acceleration{
static float prevX=0, prevY=0, prevZ=0;
float accelX = acceleration.x * kFilterFactor + (1- kFilterFactor)*prevX;
float accelY = acceleration.y * kFilterFactor + (1- kFilterFactor)*prevY;
float accelZ = acceleration.z * kFilterFactor + (1- kFilterFactor)*prevZ;


prevX = accelX;
prevY = accelY;
prevZ = accelZ;

NSLog(@"x:%.2f,y:%.2f,z:%.2f",accelX, accelY, accelZ);

if ( ((player.position.x + (-accelY*kSpeed)) >0 && (player.position.x + (-accelY*kSpeed))<480)||
     ((player.position.y + (accelX*kSpeed)) >0 && (player.position.y + (accelX*kSpeed))<320)){

    player.position = ccp(player.position.x + (-accelY*kSpeed), player.position.y + (accelX*kSpeed));
}


CGPoint converted = ccp( (float)-acceleration.y, (float)acceleration.x);

// update the rotation based on the z-rotation
// the sprite will always be 'standing up'
player.rotation = (float) CC_RADIANS_TO_DEGREES( atan2f( converted.x, converted.y) + M_PI );
}

其中player 是CCSprite 对象,播放器根据设备方向旋转,但不会根据设备方向改变位置。我究竟做错了什么?是不是横向模式下x轴表现为y,y轴表现为x?

【问题讨论】:

  • 哦……对不起……dnt知道这让我忘记了。

标签: iphone cocos2d-iphone uiaccelerometer ccsprite


【解决方案1】:

加速度 x、y、z 值与 iPhone 的描绘模式一致。在其他模式下,您必须根据此代码重新映射轴:

-(void) setAccelerationWithX:(double)x y:(double)y z:(double)z
{
    rawZ = z;

    // transform X/Y to current device orientation
    switch ([CCDirector sharedDirector].deviceOrientation)
    {
        case kCCDeviceOrientationPortrait:
            rawX = x;
            rawY = y;
            break;
        case kCCDeviceOrientationPortraitUpsideDown:
            rawX = -x;
            rawY = -y;
            break;
        case kCCDeviceOrientationLandscapeLeft:
            rawX = -y;
            rawY = x;
            break;
        case kCCDeviceOrientationLandscapeRight:
            rawX = y;
            rawY = -x;
            break;
    }
}

Kobold2D acceleration values are already mapped to the current device orientation,这样你就不用考虑这个了。

【讨论】:

    猜你喜欢
    • 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
    相关资源
    最近更新 更多