为什么我的精灵没有随着加速度计移动?



我已经尝试修复这个几个小时了,没有运气。我试图让我的CCSprite子类(thePlayer)沿着Y轴相对于设备的倾斜在屏幕上移动。我以前也这么做过,应该没问题,但不知道为什么。下面是代码:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
    CGSize WinSize = [[CCDirector sharedDirector] winSize];
#define kFilteringFactor 0.1
#define kRestAccelX -0.6
#define kShipMaxPointsPerSec (WinSize.height*0.5)        
#define kMaxDiffX 0.2
    UIAccelerationValue rollingX, rollingY, rollingZ;
    rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
    float accelX = acceleration.x - rollingX;
    float accelDiff = accelX - kRestAccelX;
    float accelFraction = accelDiff / kMaxDiffX;
    float pointsPerSec = kShipMaxPointsPerSec * accelFraction;
    _shipPointsPerSecY = pointsPerSec;
    //CCLOG(@"PointsPerSec: %f", _shipPointsPerSecY);
    CGPoint pos = thePlayer.position;
    pos.y += _shipPointsPerSecY;
    CCLOG(@"Pos.y: %f", pos.y);
    thePlayer.position = pos;
}
- (void)update:(ccTime)dt
{
    CGSize WinSize = [[CCDirector sharedDirector] winSize];
    float maxY = WinSize.height - thePlayer.contentSize.height / 2;
    float minY = thePlayer.contentSize.height/2;
    float derp = _shipPointsPerSecY;
    //CCLOG(@"Derp: %f", derp);
    float newY = thePlayer.position.y + (_shipPointsPerSecY * dt);
    //CCLOG(@"NewY: %f", newY);
    newY = MIN(MAX(newY, minY), maxY);
    thePlayer.position = ccp(thePlayer.position.x, newY);
    //CCLOG(@"Player position Y: %f", thePlayer.position.y);
}

这可能是我遇到的第二个最烦人的问题,所以任何帮助都很感激,谢谢。

看起来你的pos.y最终是根据acceleration.x调整的;只是想确保你没有混淆你的X和Y值。

还要注意,来自加速度计的X和Y通常与屏幕的相同对齐有关,无论其方向如何。因此,在景观中,加速。x实际上是Y的值,反之亦然。如果这里也适用

最新更新