2010-12-09 6 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

허허 ~, 내가 제거하면 playerVelocity = CGPointZero; 라인 (4 라인) 업데이트 방법,이 잘 작동합니다. 무엇 : ... ( –

답변

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 줄의 목적은 무엇입니까? –

0

나는이 책을 구입했으며이 예제에서와 같은 크기의 스프라이트를 사용하지 않으면 화면 충돌 감지의 가장자리가 제대로 작동하지 않는다는 것을 알아 냈습니다. 스프라이트를 같은 크기로 만들거나 멈추지 않고 스프라이트를 화면 주위로 감싸 주면된다.