using the accelerometer for moving multiple UIImages at once?

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

  1. moomy macrumors newbie

    Joined:
    Apr 28, 2010
    #1
    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??
    thanks
    Moomy
     
  2. jnic macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
    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

    Joined:
    Jun 25, 2010
    #3
    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

    Joined:
    Apr 28, 2010
    #4
    Code:
    -(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)(ball.center.x +valueX);
    if (newX > 320-BALL_RADIUS)
    newX = 320-BALL_RADIUS;
    if (newX < 0+BALL_RADIUS)
    newX = 0+BALL_RADIUS;
    
    int newY = (int)(ball.center.y -valueY);
    if (newY > 460-BALL_RADIUS)
    newY = 460-BALL_RADIUS;
    if (newY < 0+BALL_RADIUS)
    newY = 0+BALL_RADIUS;
    
    CGPoint newCenter = CGPointMake(newX, newY);
    
    ball.center = newCenter; 
    [self checkCollision];
    }
    
    -(void)checkCollision{
    
    
    if(fabs(self.ball.center.x - self.plug.center.x)<self.plug.frame.size.width/2.0 && fabs(self.plug.center.y - self.ball.center.y)<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