简体   繁体   中英

How do I get a ball to fall randomly in iPhone

With full compliments, may I start by declaring that I am new to iPhone programming. Thus I will like to ask for help on the solution on how to implement an animation where "a" ball falls randomly from the top of the iPhone screen. The ball's attribute is such that it has different sizes, speed and can bounce too ie there is collision also involved.

I am trying to use just an image of the ball and I added the ball image through Interface Builder(IB) and connected the UIImageView to files owner in IB and I have been trying without success to access the image programatically by adding the IBOutlet keyword to the .h file(please see the .h and .m code below). When I run the code all I get is the image appearing and instantly disappearing. how do I get this ball to fall randomly with some bounce or collision.

To be precise I wish to have the kind of animation such as that obtained in the iPhone game falling balls...where the balls fall and still bounce off.

Thanks in advance for your help CHEERS!!

***The .h file***

@interface TestBallsViewController : UIViewController {

  IBOutlet UIImageView *ball;

 CGPoint ballMovement;

 double size;
 double speed;

}


@property(nonatomic, retain)IBOutlet UIImageView *ball;


- (void)initializeTimer;


- (void)animateBall:(NSTimer *)theTimer;



**The .m file**

@implementation TestBallsViewController

@synthesize ball;


- (void)dealloc {

 [ball release];

    [super dealloc];
}



- (void)viewDidLoad {
 [super viewDidLoad];
 ballMovement = CGPointMake(2,2);  
 [self initializeTimer];

}



- (void)initializeTimer
 { 
  float theInterval = 0.3;

  [NSTimer scheduledTimerWithTimeInterval:theInterval target:self

                   selector:@selector(animateBall:) userInfo:nil repeats:YES];
}



- (void)animateBall:(NSTimer *)theTimer

 {
  ball.center = CGPointMake(ball.center.x+ballMovement.x, 
                                                       ball.center.y+ballMovement.y);


     int startX =arc4random()%100;
                   int endX = arc4random()%320; 
     ball.frame= CGRectMake(startX, -100, 15.0 * size, 15 * size);
                   ball.frame = CGRectMake(endX, 480, 15.0 * size, 15.0 * size);



  if(ball.center.x > 300 || ball.center.x < 20) ballMovement.x = 
                                                                     - ballMovement.x;
  if(ball.center.y > 440 || ball.center.y < 40) ballMovement.y =
                                                                     - ballMovement.y;

 }
- (void)animateBall:(NSTimer *)theTimer

 {
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:1.0];
  ball.center = CGPointMake(ball.center.x+ballMovement.x, 
                                                       ball.center.y+ballMovement.y);


     int startX =arc4random()%100;
                   int endX = arc4random()%320; 
     ball.frame= CGRectMake(startX, -100, 15.0 * size, 15 * size);
                   ball.frame = CGRectMake(endX, 480, 15.0 * size, 15.0 * size);



  if(ball.center.x > 300 || ball.center.x < 20) ballMovement.x = 
                                                                     - ballMovement.x;
  if(ball.center.y > 440 || ball.center.y < 40) ballMovement.y =
                                                                     - ballMovement.y;
  [UIView commitAnimations];

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM