Lan Yi iOS uses the controls you learn to play games

Source: Internet
Author: User

Today, the overall harvest a lot of feeling, start learning time is very short, but today I can use the knowledge to do a simple like a pony across the river game.     By writing a game, you can put together the knowledge of the Uibutton,uilabel,uitextfield that you have learned over the past few days. The implementation of the small Game program, the main use of nstimer to achieve the image of their own movement, to determine whether two are exposed to the use of new knowledge cgrectintersectsrect1,UiButton
Drag the four buttons through storyboard, implement the button in the program to click on different buttons to move the picture up or down2, while loop to show how many apples are sold and how many apples are left. 3, through the learning of pride and knowledge points to write know the game, the function of the implementation includes, through the keys to control an object, from the bottom of the screen to the top of the screen, the middle base to the horizontal movement of obstacles after the failure. 1,UIButton Drag through the storyboard four buttons, in the program to achieve the click of different buttons button to move the picture up or down

-(Ibaction) Upaction: (UIButton *) Sender {

_ib.center=cgpointmake (_ib.center.x, _ib.center.y-10);

//}

-(Ibaction) Leftaction: (UIButton *) Sender {

_ib.center=cgpointmake (_ib.center.x-10, _IB.CENTER.Y);

//}

-(Ibaction) Downaction: (UIButton *) Sender {

_ib.center=cgpointmake (_ib.center.x, _ib.center.y+10);

//}

-(Ibaction) Rightaction: (UIButton *) Sender {

_ib.center=cgpointmake (_ib.center.x+10, _IB.CENTER.Y);

//}

//

-(void) Viewdidload {

[Super Viewdidload];

//

_ib=[[uiimageview Alloc]initwithframe:cgrectmake (10, 100, 100, 100)];

_ib.image=[uiimage imagenamed:@ "555.jpg"];

[Self.view Addsubview:_ib];

//   }

2,while loop to show how many apples are sold and how many apples are left.

-(void) Viewdidload {

[Super Viewdidload];

//

//

int apples=100;

int selleapples=0;

while (selleapples<100) {

Selleapples+1, the value of itself will not change.

NSLog (@ "sell to the fruit of the first%d", selleapples+1);

The meaning of selleapples++ and selleapple+ is different, the former is to add 1 of its own value, first take 0 and then add 1 to 1.

selleapples++;

Selleapples is the result of the above himself + + 1.

NSLog (@ "sold%d fruit, and%d", selleapples,apples-selleapples);

//

//    }

//

3, through the learning of pride and knowledge points to write know the game, the function of the implementation includes, through the keys to control an object, from the bottom of the screen to the top of the screen, the middle base to the horizontal movement of obstacles after the failure.

-(void) Viewdidload {

[Super Viewdidload];

_kk=10;

_kk2=15;

_kk3=20;

//

//

_iv=[[uiimageview Alloc]initwithframe:cgrectmake (330, 80, 100, 100)];

_iv.image=[uiimage imagenamed:@ "02.jpg"];

_iv.backgroundcolor=[uicolor Browncolor];

[Self.view Addsubview:_iv];

//

_iv2=[[uiimageview Alloc]initwithframe:cgrectmake (330, 240, 100, 100)];

_iv2.image=[uiimage imagenamed:@ "03.jpeg"];

[Self.view Addsubview:_iv2];


_iv3=[[uiimageview Alloc]initwithframe:cgrectmake (330, 390, 100, 100)];

_iv3.image=[uiimage imagenamed:@ "01.jpg"];

[Self.view Addsubview:_iv3];

//

_time1=[nstimer scheduledtimerwithtimeinterval:.1 target:self selector: @selector (move1) Userinfo:nil Repeats:yes] ;

//

//

//}

-(void) move1{

//

//

_iv.center=cgpointmake (_iv.center.x-_kk, _IV.CENTER.Y);

//

if (_iv.center.x>380| | _iv.center.x<0) {

_KK=-_KK;

//

//    }

//

_iv2.center=cgpointmake (_IV2.CENTER.X-_KK2, _IV2.CENTER.Y);

//

if (_iv2.center.x>380| | _iv2.center.x<0) {

_KK2=-_KK2;

//

//    }

//

_iv3.center=cgpointmake (_IV3.CENTER.X-_KK3, _IV3.CENTER.Y);

//

if (_iv3.center.x>380| | _iv3.center.x<0) {

_KK3=-_KK3;

//

//    }

//}

Share:

0

Like

Lan Yi iOS uses the controls you learn to play games

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.