Images not moving when called by NSTimer

Discussion in 'iOS Programming' started by noahgolm, Feb 22, 2011.

  1. noahgolm macrumors member

    Joined:
    Aug 18, 2010
    #1
    I have an app that randomly creates a UIImageView (enemy) at one of three "spawn points." To move them towards their destination (a player on the screen), I used CoreAnimation with things such [UIView beginAnimation...]. Now, though, I've begun to need to implement collision detection. Unfortunately, there is no way to do this between the animated enemy and the player using something like CGRectIntersectsRect(). To solve this, I merely decided to animate this using an NSTimer (calling a function every .01 seconds moving the image forward). Strangely, the image never moves, and the enemy images just pile on top of each other. I've tried numerous methods, but here's the most basic one (which also doesn't work):

    Code:
    
    -(void)animateEnemy{
    	CGPoint eCenter = enemy.center;
    	enemy.center = CGPointMake(eCenter.x, eCenter.y++);
    	
    	
    }
    I have an NSTimer in the viewDidLoad method, too:
    Code:
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animateEnemy) userInfo:nil repeats:YES];
    
    Any ideas as to what is causing the problem?
     
  2. MDiak47 macrumors newbie

    Joined:
    Dec 11, 2010
    #2
    Code:
    [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animateEnemy) userInfo:nil repeats:YES];
    Try this, if that doesn't fix it, it is the code within the function call.

    Code:
    [NSTimer scheduledTimerWithTimeInterval:0.1/60 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
     
  3. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #3
    What happens if you try this?

    Code:
    -(void)animateEnemy{
    	CGPoint eCenter = enemy.center;
    	eCenter.y++;
    	enemy.center = eCenter;
    }
     
  4. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #4
    The red-hilited code is using the original value of eCenter.y to make a CGPoint. The y value of eCenter is then incremented. Sadly, the post-incremented value isn't used for anything else.

    This happens because this is what a postfix ++ does: it evaluates to the current value, then increments the variable.

    This should work better:
    Code:
    enemy.center = CGPointMake(eCenter.x,eCenter.y+1);	
    
    Might want to review the ++ and -- operators again, and how postfix vs. prefix placement affects things.
     
  5. firewood macrumors 604

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #5
    Are you calling setNeedsDisplay so that a drawRect can redraw your moved image? Or moving a CALayer or something containing the image?
     
  6. noahgolm thread starter macrumors member

    Joined:
    Aug 18, 2010
    #6
    Combining the NSTimer solution with changing ++ to +1 seemed to work. Thanks, I'll definitely review my prefix vs. postfix stuff now, as that seemed to make a major difference in this event. Thank you all! I wish I knew other mac programmers I could consult with outside of the internet, as that would help me with problems upfront rather than asking everyone online.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    One of the most useful things you can do is learn how to use the debugger, and especially how to single-step through lines of code.

    If you know how to use the debugger, then many more things become possible. For example, instead of passively wondering why the enemies didn't move, or having to ask someone here, you could look at what's actually happening.

    If the enemies aren't animating, set a breakpoint on animateEnemy, then step through what actually happens. You'd be able to see that the enemy.center wasn't changing, even if it wasn't immediately clear why. Maybe you'd change the code to break it down more like PhoneyDeveloper showed. Or more like I showed. Or even if you didn't get an epiphany on y++, you'd still be able to come here and provide all that information as a starting point.

    All of this is only possible by learning to use the debugger. And the best part is that you'll then know how to use the debugger in all your other programs, to help solve all those other puzzling bugs you haven't written yet. It's a benefit that keeps on paying off.
     

Share This Page