こんにちは私は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);
}