如何使用Accelerometer移动CCSprite

时间:2012-07-09 12:07:41

标签: iphone xcode cocos2d-iphone

嗨,我正在开发一个cocos2d游戏。我想用加速度计值更改来移动CCSprite。我已经为加速度计实现了这个代码。但在横向位置倾斜iphone精灵正在上下移动。但我想向前和向后移动精灵。

[[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];

[[UIAccelerometer sharedAccelerometer] setDelegate:self];

[self scheduleUpdate];


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

        // because we are in landscape mode where x is always +90 or -90
        // float acely = acceleration.x;
        float acelx = acceleration.y; 
        movement = movement * deceleration + acelx * sinsitivity;


        float angleRadians = atanf((float)acceleration.y / (float)acceleration.x);
        float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
        // float cocosAngle = angleDegrees;

        if(angleDegrees<85 && angleDegrees >-85 )
        {
            _player.rotation =  angleDegrees;
        }

        //NSLog(@" angle = %f",angleDegrees);



        if([ShieldCover visible]==TRUE)
        {
             ShieldCover.rotation= angleDegrees;
        }


        //  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 );

        //  ShieldCover.rotation =(float) CC_RADIANS_TO_DEGREES( atan2f( converted.x, converted.y) + M_PI );

    }

    - (void)update:(ccTime)delta {


        //assuming that you have a sprite called _fireman with 100 px width

        if ( _player.position.y > 0 && _player.position.y < 320) {
            //_fireman is at neither edge of the screen so move the paddle!
            _player.position = ccp(_player.position.x, _player.position.y + movement);
        }

        if ( _player.position.y < 0 ) {
            //_fireman hit the left edge of the screen, set the left bound position with no movement.
            _player.position = ccp( _player.position.x, 319);
        }

        if ( _player.position.y > 320 ) {
            //_fireman hit the right edge of the screen, set the right bound position with no movement.
            _player.position = ccp(_player.position.x, 1 );
        }

        if ( _player.position.y < 1 && movement > 1 ) {
            //_fireman is at the left edge of the screen and the device is tiled right. Move the _fireman!
            _player.position = ccp(_player.position.x , _player.position.y+ movement);
        }

        if ( _player.position.y > 319 && movement < 0) {
            //_fireman is at the right edge of the screen and the device is tiled left. Move the _fireman!
            _player.position = ccp(_player.position.x , _player.position.y+ movement);
        }

        barracuda.position = ccp(barracuda.position.x,_player.position.y);
        particles.position = ccp(_player.position.x-30, _player.position.y-10);
        parteMagnet.position = ccp(_player.position.x, _player.position.y);


    }

1 个答案:

答案 0 :(得分:3)

// Try this

-(id) init
{
   if (self == [super init])
   {
     [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1/60];
     [[UIAccelerometer sharedAccelerometer] setDelegate:self];
     [self scheduleUpdate];
   }
   return self;
}

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{ 
    float deceleration = 0.4f;
    float sinsitivity = 6.0f;
    float acelx = acceleration.y; 
    movement = movement * deceleration + acelx * sinsitivity;

    float angleRadians = atanf((float)acceleration.y / (float)acceleration.x);
    float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
    if(angleDegrees<85 && angleDegrees >-85 )
    {
        _player.rotation =  angleDegrees;
    }
    if([ShieldCover visible]==TRUE)
    {
         ShieldCover.rotation= angleDegrees;
    }
}

- (void)update:(ccTime)delta
{
    if ( _player.position.y > 0 && _player.position.y < 320)
    {
        _player.position = ccp(_player.position.x, _player.position.y + movement);
    }
    if ( _player.position.y < 0 )
    {
        _player.position = ccp( _player.position.x, 319);
    }
    if ( _player.position.y > 320 )
    {
        _player.position = ccp(_player.position.x, 1 );
    }
    if ( _player.position.y < 1 && movement > 1 )
    {
        _player.position = ccp(_player.position.x , _player.position.y+ movement);
    }
    if ( _player.position.y > 319 && movement < 0)
    {
        _player.position = ccp(_player.position.x , _player.position.y+ movement);
    }
    barracuda.position = ccp(barracuda.position.x,_player.position.y);
    particles.position = ccp(_player.position.x-30, _player.position.y-10);
    parteMagnet.position = ccp(_player.position.x, _player.position.y);
}