A little game about ball collisions in iOS

Source: Internet
Author: User

This game is about a ball randomly moving on the screen, can be manipulated with a finger to make a ball, if two balls collide together, it means a lost, very simple game



Define two Uiimageview and a Start button in storyboard



and link the two Uiimageview with the code.

@property (weak, nonatomic)iboutlet uiimageview *player;

@property (weak, nonatomic)iboutlet uiimageview *enemy;

@property (weak, nonatomic)iboutlet UIButton *start;


Define the rate of ball movement in Viewdidload

pos =cgpointmake(5.0,4.0);

If you click the Start button

-(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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.