[英]Why isn't my sprite moving with accelerometer?
我已經嘗試解決這個問題了好幾個小時,沒有運氣。 我試圖讓我的CCSprite子類(thePlayer)相對於設備的傾斜度沿Y軸在屏幕上移動。 我之前已經做過了,一切都應該正常工作,但是由於某種原因卻沒有。 這是代碼:
- (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
最終將根據acceleration.x
x進行調整; 只是想確保您不會混淆X和Y值。
還要注意,來自加速度計的X和Y通常與屏幕的相同對齊方式有關,而與屏幕的方向無關。 因此,在橫向環境中,acceleration.x實際上是您的Y值,反之亦然。 如果在這里也適用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.