0

In my game, my main character is a CCSprite. That CCSprite is controller by the UIAccelerometer, I enabled the accelerometer method by doing self.isAccelerometerEnabled = YES; And then setting the updateInterval to: 1/30.

The problem is not my acceleration values not being actual good values but it has to do with my CCSprite not getting redrawn fast enough. So pretty much if my CCSprite moves many pixels per .1 second, the sprite starts to flicker and then the faster it gets the more it flickers.

I declared my acceleration values in my .h and set them in the Accelerometer delegate method then I set the position of the CCSprite in my game loop.

Any ideas why this might be happening?

Thanks!

Edit: Accelerometer code:

float accelX = (acceleration.x - [[NSUserDefaults standardUserDefaults] floatForKey:@"X-Calibrate"]);
rollingX = (accelX * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
AccelPoint.x += (rollingX*50);
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

2 Answers2

2

Flickering sprite indicates a bug somewhere else. Unless you mean that the sprite has some "jittery" motion to it. If you haven't already, you should add accelerometer filtering to smooth the accelerometer input values.

Btw, Kobold2D has the most commonly used accelerometer filtering (high and low pass) already built-in.

Community
  • 1
  • 1
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • I already did implement a high pass filter! Look in my edit1. This cannot be a bug elsewhere but I am not sure what could make my sprite even do that! my filter value is 0.2 btw. – SimplyKiwi Dec 03 '11 at 00:37
  • It's a low pass filter and the correct filter type to smooth out sudden movements. However, you kind of defeat the purpose by multiplying the resulting value with 50. This will scale up any remaining variance 50-fold. With acceleration values ranging from -1 to +1 and filtering not changing the value range, it means that your accelerometer controlled sprite can move up to 50 pixels in one frame under the most extreme circumstances. Another clue in the next comment…. – CodeSmile Dec 06 '11 at 21:19
  • Your accelerometer updateInterval is 30 frames. Unless you also lock the game at 30 fps, it means that every second frame you get new acceleration values. On every other frame, I could imagine you're running some kind of code that changes the sprite's position or changes the rollingX. For example, accelerometer tells sprite to move +25 pixels to right, next frame some other code repositions the sprite to -20 pixels to the left, and repeat that every frame and you get yourself a flickering sprite that flickers more the faster acceleration values are. – CodeSmile Dec 06 '11 at 21:21
  • Ok so what should I do instead like code-wise with these comments you made? – SimplyKiwi Dec 06 '11 at 22:22
  • try changing the CCDirector animationInterval to 30, or better simply set the UIAccelerometer update interval to 1/60. – CodeSmile Dec 07 '11 at 10:46
  • Unrelated to this thread but in your Pixel Perfect collision tutorial, it crashes without modifying any of your code when doing the sprite collision detection with this as the reason for the crash: 'other pixelMask index out of bounds for x,y: 113, 194' – SimplyKiwi Jan 16 '12 at 21:21
1

You should update the sprites position in the update method, not in the accelerometer method. (If you have not already)

HarryUK
  • 11
  • 1