Reputation: 1377
I've been trying to fix this for hours now, with no luck. I'm trying to have my CCSprite subclass (thePlayer) move across the screen along the Y axis in relation to the tilt of the device. I've done it before, and everything should work, but for some reason it isn't. Here is the code:
- (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);
}
This is probably the second most annoying problem I have ever had, so any help is appreciated, thanks.
Upvotes: 0
Views: 215
Reputation: 24771
It looks like your pos.y
is ultimately being adjusted based on acceleration.x
; just want to make sure you are not confusing your X and Y values.
Also note that X and Y from an accelerometer usually relate to the same alignment of the screen, regardless of its orientation. Thus in landscape, acceleration.x is actually your Y value, and vice-versa. In case that applies here as well.
Upvotes: 1