Problem with UIImageView array and loops

Discussion in 'iOS Programming' started by orchiss, Mar 19, 2009.

  1. orchiss macrumors newbie

    Joined:
    Mar 19, 2009
    #1
    Hello everyone,

    I've been working on my game for the last week and all was well until I ran it on the iphone at which point it came to a grinding halt.

    I narrowed the issue down to the following:

    What I'm doing is creating an 8x8 array of UIImageView's and outputting them in a game loop that is fired every 1/60 of second via a timer in viewDidLoad.

    My definition in the interface is:
    UIImageView *_piece[8][8];
    UIImage *piece;

    viewDidLoad: piece = [UIImage imageNamed:mad:"white1.png"];

    In the loop I have the following code:
    Code:
    float startx = 15.0;
      float starty = 30.0;
       for(int i = 0;i < 8; i++) {     
        for(int j = 0; j < 8; j++) { 
    	  // remove previous view
          [_piece[i][j] removeFromSuperview];	 	 
    	  
    	  // assign image - same image for simplicity
          _piece[i][j] = [[UIImageView alloc] initWithImage: piece];
    	  
    	  // x and y of where to place the image	 
          _piece[i][j].center = CGPointMake(startx,starty);   
    	   	 	 
          // add it to the view
          [self.view addSubview:_piece[i][j]];
          
    	  // release it
          _piece[i][j].release;     	  	    
    	 	 
    	  startx += 36.0;
         }
    	 startx = 15.0; 
    	 starty += 36.0;
       }
    
    
    From what I understand, I should be removing the view, assigning an image, positioning it, adding it to the view, and releasing it. In essence, completely removing all UIImageviews and then adding them back in. It works great in the simulator, but comes to a grinding halt on the actual device. Any help would be greatly appreciated.

    -M
     
  2. detz macrumors 65816

    Joined:
    Jun 29, 2007
    #2
    That's way overkill, you don't need to recreate the uiviews every loop iteration, in fact you need to only create them once when you init the app. Move all the creation and initialization out of the for look and do it once. Your loop should only be for checking and changing things.

    Do the images change location or images? If not then you can set that outside the loop too.

    Also, there is no need to update 60 times a second, I don't know what you're doing so I can't say for sure but at the very least you can bring that down to 20 times.
     
  3. orchiss thread starter macrumors newbie

    Joined:
    Mar 19, 2009
    #3
    Thanks for the reply.

    I'm working on a variation of a match 3 game, so the pieces do move quite a bit - swapping,removing,adding,spinning, etc. I tried bringing the frame rate down to 20 with my current code and it still was unresponsive.

    I understand that it's a bit overkill the way I did it, but I kind of was thinking along the lines of draw a scene, remove the scene, draw the scene, etc...

    Is there anything wrong with my create/release code?

    -M
     
  4. detz macrumors 65816

    Joined:
    Jun 29, 2007
    #4
    Besides that you shouldn't do it? Creating objects are expensive and you know that you have 64 so you should just create 64 and then move and change them instead of removing them and re-creating them. The core animation stuff will handle how to draw everything for you.
     

Share This Page