I'm trying to make a vertical shooter, using Cocos2D and a few tutorials. Unfortunately, most tutorials only show how to make an horizontal shooter.
Still, I tried to adapt the code from a horizontal shooter to a vertical one, but am having a few problems. Mainly, my ship keeps moving right, even when the iPhone is sitting perfectly still.
Seeing as I'm a beginner, I'm having trouble pinpointing the exact issue and I'd love if anyone could help me.
Here's my accelerometer method:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
define kFilteringFactor 0.1
define kRestAccelX -0.6
define kShipMaxPointsPerSec (winSize.width*0.5)
define kMaxDiffX 0.2
UIAccelerationValue rollingX;
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
float accelX = acceleration.x - rollingX;
CGSize winSize = [CCDirector sharedDirector].winSize;
float accelDiff = accelX - kRestAccelX;
float accelFraction = accelDiff / kMaxDiffX;
float pointsPerSec = kShipMaxPointsPerSec * accelFraction;
_shipPointsPerSecX = pointsPerSec;
}
And my update method:
-(void)update:(ccTime)dt {
CGSize winSize = [CCDirector sharedDirector].winSize;
float maxX = winSize.width - _ship.contentSize.width/2;
float minX = _ship.contentSize.width/2;
float newX = _ship.position.x + (_shipPointsPerSecX * dt);
newX = MIN(MAX(newX, minX), maxX);
_ship.position = ccp(newX, _ship.position.y);
}
What could be causing this?
rollingX
variable is read while not initialized? \$\endgroup\$