using the accelerometer for moving multiple UIImages at once?

Discussion in 'iOS Programming' started by moomy, Jun 28, 2010.

  1. moomy macrumors newbie

    Apr 28, 2010
    Hi everyone.

    I am trying to move 10 balls at once using the accelerometer. They would all start in different places, set in interface builder.

    I can move one easily but run into trouble when I try and make more than one move at the same time.

    I am assuming it is possible as I think I have seen this sort of think in apps (not sure though.)

    anyone know if it is possible and the best way to go about achieving this??
  2. jnic macrumors 6502a

    Oct 24, 2008
    What code are using to animate them currently? If it's standard UIView animations, you can wrap multiple transforms in a single animation block.
  3. bweberapps macrumors member

    Jun 25, 2010
    Do you need to use UIImages and UIViews? I would suggest using cocos2d for graphics and animations. My game uses the accelerometer to move a ball, and when I was testing the limitations of cocos2d and the box2d physics engine I was able to have 50 balls moving at once on the screen. Since cocos2d uses OpenGL it was more efficient and powerful, plus you can always add standard UI object to a cocos2d layer.
  4. moomy thread starter macrumors newbie

    Apr 28, 2010
    -(void)awakeFromNib {
    [[UIAccelerometer sharedAccelerometer] setUpdateInterval:1.0/30.0];
    [[UIAccelerometer sharedAccelerometer] setDelegate:self];
    - (void)accelerometerUIAccelerometer *)accelerometer didAccelerateUIAcceleration *)acceleration {
    valueX = acceleration.x*30.0;
    valueY = acceleration.y*30.0;
    int newX = (int)( +valueX);
    if (newX > 320-BALL_RADIUS)
    newX = 320-BALL_RADIUS;
    if (newX < 0+BALL_RADIUS)
    newX = 0+BALL_RADIUS;
    int newY = (int)( -valueY);
    if (newY > 460-BALL_RADIUS)
    newY = 460-BALL_RADIUS;
    if (newY < 0+BALL_RADIUS)
    newY = 0+BALL_RADIUS;
    CGPoint newCenter = CGPointMake(newX, newY); = newCenter; 
    [self checkCollision];
    if(fabs( -<self.plug.frame.size.width/2.0 && fabs( -<10.0) {
    [ball removeFromSuperview];	
    this is my code to make one ball move. It's a very simple way to do it. I think I may have to look into more complex ways to do things to get more balls moving. Will have a look at cocoa2d.

Share This Page