嗨,我正在开发一个 cocos2d 游戏。我想CCSprite
随着加速度计值的变化而移动。我已经为加速度计实现了这个代码。但在横向位置倾斜 iphone sprite 上下移动。但我想前后移动精灵。
[[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);
}