views:

92

answers:

3

I have this IBAction that is suppose to make a character onscreen jump, however when its called it just moves the character up once, then each call after that the character just moves down more and more. This function should just be called then the character would 'jump' up and then fall straight down off the screen since i havent put in any collision with the ground. ANy suggestions why this is happening? tim is the name of my UIImageView that holds the chracater, btw.

-(IBAction)Jump:(id)sender

{ int jumpSpeed = JumpSpeedLimit; CGPoint newCenter = tim.center;

if(!mainJumping){
 //then start jumping
 mainJumping = TRUE;
 jumpSpeed = JumpSpeedLimit*-1;
 newCenter.x -= jumpSpeed;
 tim.center = newCenter;

} else {
 //then continue jumping if already in the air
 //crazy math that I won't explain
 if(jumpSpeed < 0){
  jumpSpeed *= 1 - JumpSpeedLimit/75;
  if(jumpSpeed > -JumpSpeedLimit/5){
   jumpSpeed *= -1;
  }
 }
 if(jumpSpeed > 0 && jumpSpeed <= JumpSpeedLimit){
  jumpSpeed *= 1 + JumpSpeedLimit/50;
 }
 newCenter = tim.center;
 newCenter.x -= jumpSpeed;
 tim.center = newCenter;
 /*
 //if hits the floor, then stop jumping

 if(tim.center.x >= 360 - tim.bounds.size.height){
  mainJumping = FALSE;   
  newCenter = tim.center;
  newCenter.x = 360 - tim.bounds.size.height;
  tim.center = newCenter;
 }*/

}

}

+1  A: 

You are fundamentally designing this wrong. You want it to be that pressing the "jump" button sets some sort of flag, and then in your game engine code, process that flag.

rlbond
A: 

Sounds like a job for CoreAnimation more than one for a repeated call to an IBAction. Create an animation path on which your view should move and add a timing function to give the gravity acceleration effect, something along the lines of the animation cookbook sample: http://developer.apple.com/documentation/GraphicsImaging/Conceptual/CoreAnimation_Cookbook/Articles/Timing.html#//apple_ref/doc/uid/TP40006077-SW1

Remus Rusanu
A: 

Hey, try this animation snippet to make your views jumping.

slatvick