Iphone 为什么不是';我的精灵不会随着加速度计移动吗?
我已经试着解决这个问题好几个小时了,运气不好。我试图让我的CCSprite子类(thePlayer)沿着Y轴在屏幕上相对于设备的倾斜移动。我以前做过,一切都应该正常,但由于某种原因它不是。代码如下:Iphone 为什么不是';我的精灵不会随着加速度计移动吗?,iphone,objective-c,cocos2d-iphone,Iphone,Objective C,Cocos2d Iphone,我已经试着解决这个问题好几个小时了,运气不好。我试图让我的CCSprite子类(thePlayer)沿着Y轴在屏幕上相对于设备的倾斜移动。我以前做过,一切都应该正常,但由于某种原因它不是。代码如下: - (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration { CGSize WinSize = [[CCDirector sharedDirector
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
CGSize WinSize = [[CCDirector sharedDirector] winSize];
#define kFilteringFactor 0.1
#define kRestAccelX -0.6
#define kShipMaxPointsPerSec (WinSize.height*0.5)
#define kMaxDiffX 0.2
UIAccelerationValue rollingX, rollingY, rollingZ;
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
float accelX = acceleration.x - rollingX;
float accelDiff = accelX - kRestAccelX;
float accelFraction = accelDiff / kMaxDiffX;
float pointsPerSec = kShipMaxPointsPerSec * accelFraction;
_shipPointsPerSecY = pointsPerSec;
//CCLOG(@"PointsPerSec: %f", _shipPointsPerSecY);
CGPoint pos = thePlayer.position;
pos.y += _shipPointsPerSecY;
CCLOG(@"Pos.y: %f", pos.y);
thePlayer.position = pos;
}
- (void)update:(ccTime)dt
{
CGSize WinSize = [[CCDirector sharedDirector] winSize];
float maxY = WinSize.height - thePlayer.contentSize.height / 2;
float minY = thePlayer.contentSize.height/2;
float derp = _shipPointsPerSecY;
//CCLOG(@"Derp: %f", derp);
float newY = thePlayer.position.y + (_shipPointsPerSecY * dt);
//CCLOG(@"NewY: %f", newY);
newY = MIN(MAX(newY, minY), maxY);
thePlayer.position = ccp(thePlayer.position.x, newY);
//CCLOG(@"Player position Y: %f", thePlayer.position.y);
}
这可能是我遇到的第二个最烦人的问题,因此非常感谢您的帮助。看起来您的
pos.y
最终将根据加速度.x
进行调整;只是想确保你没有混淆你的X和Y值
还请注意,加速度计的X和Y通常与屏幕的相同对齐有关,而与屏幕的方向无关。因此,在横向中,加速度.x实际上是你的Y值,反之亦然。如果这也适用于这里。您是如何以及在哪里将
加速计:didAccelerate:
选择器连接到加速计的?就在我的游戏世界层中,这是在cocos2d中创建新场景的标准CCScene。我确实收到了加速计的数据,但是。。。我真的不知道到底发生了什么…也许你的过滤过滤掉了