This game is about a ball randomly moving on the screen, able to manipulate the finger to make a ball, assuming that two balls collide together, it means losing, very easy to a game
Define two Uiimageview and one Startbutton in storyboard.
and link the two Uiimageview with the code.
@property (weak, nonatomic)iboutletuiimageview *player;
@property (weak, nonatomic)iboutletuiimageview *enemy;
@property (weak, nonatomic)iboutletUIButton *start;
Define the rate of ball movement in Viewdidload
pos =cgpointmake(5.0,4.0);
Suppose you clicked Startbutton.
-(Ibaction) Start: (ID) Sender {[Self.start sethidden:yes]; Start a timer and let player this ball move randomly randommain = [Nstimer scheduledtimerwithtimeinterval:0.03 target:self selector: @selector ( OnTimer) Userinfo:nil Repeats:yes]; }-(void) ontimer{[self checkcollision]; Self.enemy.center = Cgpointmake (self.enemy.center.x+pos.x, SELF.ENEMY.CENTER.Y+POS.Y); if (Self.enemy.center.x > | | self.enemy.center.x < 0) {pos.x =-pos.x; } if (Self.enemy.center.y > 480 | | self.enemy.center.y < 0) {pos.y =-pos.y; }}-(void) checkcollision{//detects if two balls have collided together if (Cgrectintersectsrect (Self.player.frame, Self.enemy.frame)) {[R Andommain invalidate]; [Self.start Sethidden:no]; CGRect frame = [Self.player frame]; frame.origin.x = 137.0f; FRAME.ORIGIN.Y = 326.0f; [Self.player Setframe:frame]; CGRect frame2 = [Self.enemy frame]; frame2.origin.x = 137.0f; Frame2.origin.y = 20.0f; [Self.enemy setframe:frame2]; Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Ball was catched" message:@ "you had to play again" delegate:self cancelbuttontitle:@ "Cancel" otherbuttontitles:nil]; [Alert show]; }}
The code can be downloaded in http://download.csdn.net/detail/baidu_nod/7556833
A little game about ball collisions in iOS