A little game about ball collisions in iOS

Source: Internet
Author: User

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

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.