Wilhelm Michaelsen
Wilhelm Michaelsen

Reputation: 663

Use UIAccelerometer to move CCSprite – cocos2D

I want to be able to move a CCSprite using the UIAccelerometer. It should only be moved up and down. The code I use is this:

- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
valueX = acceleration.y * 45.0;

int newPos = (int)(valueX + _rocket.position.x); 

CGPoint newCenter = ccp(100, newPos);
_rocket.position = newCenter;
}

_rocket is the sprite.

When I run it on my iphone it's just vibrating on the same place, and I'm not able to move it to the top or the bottom. What's wrong?

Upvotes: 0

Views: 61

Answers (1)

K1laba
K1laba

Reputation: 154

try int newPos = (int)(valueX + _rocket.position.y);
instead int newPos = (int)(valueX + _rocket.position.x);

according to your code position.x is always 100. That's why int newPos = (int)(valueX + _rocket.position.x); not working.

Upvotes: 1

Related Questions