2010-12-09 114 views
0

我在阅读'学习iPhone和iPad Cocos2D游戏开发',并在第4章中介绍了一个使用加速度计的简单示例。如果我仅将x轴用作书籍,但使用y轴,则精灵的运动效果很好,如果它位于屏幕边缘,则精灵的运动不平滑。iPhone,cocos2d和加速度计

+ (id)scene 
{ 
    CCScene* scene = [CCScene node]; 
    CCLayer* layer = [GameScene node]; 
    [scene addChild:layer]; 
    return scene; 
} 

- (id)init 
{ 
    if ((self = [super init])) 
    { 
     CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 

     self.isAccelerometerEnabled = YES; 

     player = [CCSprite spriteWithFile:@"alien.png"]; 
     [self addChild:player z:0 tag:1]; 

     CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
     float imageHeight = [player texture].contentSize.height; 
     player.position = CGPointMake(screenSize.width/2, imageHeight/2); 

     [self scheduleUpdate]; 
    } 

    return self; 
} 

- (void)dealloc 
{ 
    CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self); 

    [super dealloc]; 
} 

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{ 
    float deceleration = 0.4f; 
    float sensitivity  = 6.0f; 
    float maxVelocity  = 100; 

    playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity; 
    playerVelocity.y = playerVelocity.y * deceleration + acceleration.y * sensitivity; 

    if (playerVelocity.x > maxVelocity) 
    { 
     playerVelocity.x = maxVelocity; 
    } 
    else if (playerVelocity.x < -maxVelocity) 
    { 
     playerVelocity.x = -maxVelocity; 
    } 

    if (playerVelocity.y > maxVelocity) 
    { 
     playerVelocity.y = maxVelocity; 
    } 
    else if (playerVelocity.y < -maxVelocity) 
    { 
     playerVelocity.y = -maxVelocity; 
    } 
} 

- (void)update:(ccTime)delta 
{ 
    CGPoint pos = player.position; 
    pos.x += playerVelocity.x; 
    pos.y += playerVelocity.y; 

    CGSize screenSize = [[CCDirector sharedDirector] winSize]; 
    float imageWidthHalved = [player texture].contentSize.width * 0.5f; 
    float leftBorderLimit  = imageWidthHalved; 
    float rightBorderLimit = screenSize.width - imageWidthHalved; 

    if (pos.x < leftBorderLimit) 
    { 
     pos.x = leftBorderLimit; 
     playerVelocity = CGPointZero; 
    } 
    else if (pos.x > rightBorderLimit) 
    { 
     pos.x = rightBorderLimit; 
     playerVelocity = CGPointZero; 
    } 

    float imageHeightHalved = [player texture].contentSize.height * 0.5f; 
    float topBorderLimit  = screenSize.height - imageHeightHalved; 
    float bottomBorderLimit = imageHeightHalved; 

    if (pos.y < bottomBorderLimit) 
    { 
     pos.y = bottomBorderLimit; 
     playerVelocity = CGPointZero; 
    } 
    else if (pos.y > topBorderLimit) 
    { 
     pos.y = topBorderLimit; 
     playerVelocity = CGPointZero; 
    } 

    player.position = pos; 
} 

什么问题?

+0

Huhh〜,如果我删除playerVelocity = CGPointZero;行(4行)更新方法,这个工作很好。什么...... :( – 2010-12-14 05:07:45

回答

2

如果你愿意,你可以尝试我在我的游戏做的:

#define SIGN(x) ((x < 0.0f) ? -1.0f : 1.0f) 


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

    float kFilteringFactor = 0.01; 

    accels[0] = acceleration.x * kFilteringFactor + accels[0] * (1.0f - kFilteringFactor); 
    accels[1] = acceleration.y * kFilteringFactor + accels[1] * (1.0f - kFilteringFactor); 
    accels[2] = acceleration.z * kFilteringFactor + accels[2] * (1.0f - kFilteringFactor); 

    float xx; 
    float yy; 

    // extract the acceleration components 
    xx = -[acceleration x]; 
    yy = [acceleration y]; 

    // Has the direction changed? 
    float accelDirX = SIGN(xvelocity) * -1.0f; 
    float newDirX = SIGN(xx); 
    float accelDirY = SIGN(yvelocity) * -1.0f; 
    float newDirY = SIGN(yy); 

    // Accelerate. To increase viscosity, lower the values below 1.0f 
    if (accelDirX == newDirX) 
     xaccel = (abs(xaccel) + 0.99f) * SIGN(xaccel); 
    if (accelDirY == newDirY) 
     yaccel = (abs(yaccel) + 0.99f) * SIGN(yaccel); 

    // Apply acceleration changes to the current velocity 
    xvelocity = -xaccel * xx; 
    yvelocity = -yaccel * yy; 

    [sprite moveByAccelerometerX:yvelocity Y:xvelocity];   
} 
0

一切,感谢首先阅读我的书! :)

你可以找到一个改进的过滤方法(易于指数),I'm using in this article,它还包含加速度计如何工作的基本信息。

您可以了解更多关于加速度计输入from the Tilt to Live developers及其方法,附带示例项目。

+0

加上我的评论,这4条线的目的是什么? – 2010-12-14 05:20:28

0

我买了这本书,发现屏幕碰撞检测的边缘不能正常工作,除非您有与示例中相同的尺寸精灵。要么让你的精灵具有相同的尺寸,要么让精灵环绕屏幕而不是停止,这就是我所做的。