Help Optimizing Some Code

Discussion in 'iOS Development' started by JoshuaCaputo, Jun 1, 2009.

  1. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    I know these can be better optimized but how?

    Walking Animation
    Code:
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	
    	location = [touch locationInView:cameraView];
    	
    		if (location.x > goodGuy.center.x){
    			moving = TRUE;
    			direction = 0;
    			walkTimer = [NSTimer scheduledTimerWithTimeInterval:0.14 target:self selector:@selector(walk) userInfo:nil repeats:YES];
    		}
    		else if (location.x < goodGuy.center.x){
    			moving = TRUE;
    			direction = 1;
    			walkTimer = [NSTimer scheduledTimerWithTimeInterval:0.14 target:self selector:@selector(walk) userInfo:nil repeats:YES];
    		}
    }
    
    - (void)walk{
    
    
    	if (direction == 0){
    		moveFrame = moveFrame + 1;
    		if (moveFrame == 0){
    			goodGuy.image = [UIImage imageNamed:@"1new.png"];
    		}
    		else if (moveFrame == 1){
    			goodGuy.image = [UIImage imageNamed:@"2new.png"];
    		}
    		else if (moveFrame == 2){
    			goodGuy.image = [UIImage imageNamed:@"3new.png"];
    		}
    		else if (moveFrame == 3){
    			goodGuy.image = [UIImage imageNamed:@"4new.png"];
    		}
    		else if (moveFrame == 3){
    			goodGuy.image = [UIImage imageNamed:@"5new.png"];
    		}
    		else{
    			moveFrame = 0;
    			goodGuy.image = [UIImage imageNamed:@"5new.png"];
    		}
    	}
    	else {
    		moveFrame = moveFrame + 1;
    		if (moveFrame == 0){
    			goodGuy.image = [UIImage imageNamed:@"1newl.png"];
    		}
    		else if (moveFrame == 1){
    			goodGuy.image = [UIImage imageNamed:@"2newl.png"];
    		}
    		else if (moveFrame == 2){
    			goodGuy.image = [UIImage imageNamed:@"3newl.png"];
    		}
    		else if (moveFrame == 3){
    			goodGuy.image = [UIImage imageNamed:@"4newl.png"];
    		}
    		else if (moveFrame == 3){
    			goodGuy.image = [UIImage imageNamed:@"5newl.png"];
    		}
    		else{
    			moveFrame = 0;
    			goodGuy.image = [UIImage imageNamed:@"5newl.png"];
    		}
    	}
    }
    
    I don't know why but I can't quite figure it out. I know theres a better way.

    Grenade Arc
    I would really like to know how to make something arc in a parabola, I have tried a lot of things and its still not perfect so if you know you should help

    Please Register or Log in to view images

    .


    Any helps appreciated, thanks
  2. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    I'll try and look at this when I get home, but let me first tell you to use switch statements rather than if statements, as they are faster.
  3. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Also, instead of:
    moveFrame = moveFrame + 1;
    use:
    moveFrame++;

    As for the parabola, you could make it that every frame, the speed in Y is reduced by a constant. You can do the same for X, so it looks like the friction with the wind is also affecting it.
  4. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    If you want to do a perfect parabola, you could break out a math book (Algebra 2\ Pre-calc books should have plenty on the conic section that is a parabola) and do the math in your code.

    Generate an X/Y table and set the center of the view to that point.
  5. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    yeah i know the quadratic formula but i don't know what numbers go where.

    like where the x and y of the object go
  6. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    The starting point of the grenade is one x-intercept, the end-point is the other x-intercept.

    You can find the max value from there and then you know where to animate it during the rise of the grenade and where it will come from for the falling of the grenade.
  7. JoshuaCaputo

    JoshuaCaputo New Member

    Joined:
    Aug 2, 2008
    Messages:
    605
    Likes Received:
    0
    Device:
    iPod touch
    got the walk function down to this:

    Code:
    - (void)walk{
    	if (moving == TRUE){
    		moveFrame++;
    		if (moveFrame >= 5){moveFrame = 1;}
    		if (left){
    			NSString *imageName = [[NSString alloc] initWithFormat:@"%i.png", moveFrame];
    			player.image = [UIImage imageNamed:imageName];
    			[imageName release];
    		}
    		else {
    			NSString *imageName = [[NSString alloc] initWithFormat:@"%iL.png", moveFrame];
    			player.image = [UIImage imageNamed:imageName];
    			[imageName release];
    		}
    	}
    }
    
  8. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Code:
    - (void)walk{
    	if (moving){
    		moveFrame++;
    		if (moveFrame >= 5){moveFrame = 1;}
    		if (left) { player.image = [UIImage imageNamed:[NSString stringWithFormat:@"%i.png", moveFrame]];}
    		if (!left) { player.image = [UIImage imageNamed:[NSString stringWithFormat:@"%iL.png", moveFrame]];}
    		[imageName release];
    	}
    }
    
  9. gojohnnyboi

    gojohnnyboi Well-Known Member

    Joined:
    Jan 25, 2008
    Messages:
    3,339
    Likes Received:
    55
    This is also minor, but you don't really need (boolean == TRUE), you could just say if(boolean), as 'TRUE' is just a #define that is 1

    Code:
    #define TRUE 1
    #define FALSE 0
    and for checking false you could do if(!boolean).
  10. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I was going to post that, then i notice more things that could be reduced

    Please Register or Log in to view images

    .

Share This Page