IOS 2D Game Development Framework spritekit--> (Create User Role Wizard-original)

Source: Internet
Author: User
Tags spritekit

First, the main realization
Today Spritekit implements the creation of the Player Character wizard (Skspritenode *), which increases the gesture action of the character Sprite, and the added gesture calculation method is not the same as in Objective-c. Because the coordinate system used by OBJECTIVE-C is not the same as the coordinate system used by Spritekit, the wizard's collision check code is added later.

Second, Skspritenode gestures

The Skspritenode class comes with 5 gesture monitoring methods,

       
1,-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) event

Call when the finger moves

2, -(VOID) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Uievent *) Event
    Call when the finger is lifted

3,-(void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *) event

      Cancel (abnormal leave screen, unexpected interrupt event)
         4,-(void) touchescancelled: (Nsset<uitouch *> *) touches withevent: (Uievent *) event
  3D Touch Correlation method, the current touch object estimates the touching characteristics, the return value is Uitouchpropertyie,
   5,-(void) touchesestimatedpropertiesupdated: (Nsset *) touches

6. The above 5 methods used most is 1, 2, 3, the following we want to manipulate the role of the wizard is also used to these three gestures, the realization of the idea → when the user clicks the screen to enter the 1 gesture, judge the point of the click of the coordinates is not on the Role wizard, if you can perform 2 gestures, The code with an int variable record, if clicked on the character Elf Int=1,int=1 2 gesture to execute, when the user hand lift, will be executed gesture 3, indicating the end of the gesture, we will set int=1 = 0.

Second, the Code

1. Add the code to create the role wizard in the scene layer initialization: Skspritenode * Firendplane

1UIImage *roleplaneimage=[uiimage imagenamed:@"Attackplane"];2Sktexture *roleplaneimagetextture =[Sktexture texturewithimage:roleplaneimage];3Firendplane=[skspritenode spritenodewithtexture:roleplaneimagetextture Size:cgsizemake (DEVICE_Width*0.25, device_width*0.25)];4Firendplane.physicsbody =[Skphysicsbody bodyWithRectangleOfSize:FirendPlane.size];5             /*Increased collision Monitoring Code*/6FirendPlane.physicsBody.categoryBitMask =Skrolecategoryfoeplane;7firendplane.zposition=1;8Firendplane.position=cgpointmake (self.frame.size.width/2, - );9[Self addchild:firendplane];

2. Add gestures

1-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event2 {3Uitouch *touctobj =[touches anyobject];4     5Cgpoint location=[Touctobj locationinnode:self];6     7     8CGFloat currenttagareax=firendplane.position.x-firendplane.size.width/2;9     TenCGFloat currenttagareay=firendplane.position.y-firendplane.size.height/2; One     if(Location.x>=currenttagareax &&location.x<=firendplane.position.x+ (FirendPlane.size.width/2) && ALocation.y>=currenttagareay &&location.y<=firendplane.position.y+ (FirendPlane.size.height/2)) { -          -Isornotachmyplane=1; the     } -      - } -  +- (void) touchesended: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event - { +Isornotachmyplane=0; A      at } -  --(void) touchesmoved: (Nsset<uitouch *> *) touches withevent: (Uievent *)Event - { -     if(isornotachmyplane>0) { -Uitouch *touctobj =[touches anyobject]; inCgpoint location=[Touctobj locationinnode:self]; -          to          +         if(Location.x >= self.size.width-(FirendPlane.size.width/2)) { -              thelocation.x = Self.size.width-(FirendPlane.size.width/2); *              $}Else if(location.x <= (FirendPlane.size.width/2)) {Panax Notoginseng              -Location.x =firendplane.position.x; the              +         } A          the          +         if(Location.y >= self.size.height-(FirendPlane.size.height/2)) { -              $LOCATION.Y = Self.size.height-(FirendPlane.size.height/2); $              -              -              the}Else if(LOCATION.Y <= (FirendPlane.size.height/2)) { -             WuyiLOCATION.Y =FIRENDPLANE.POSITION.Y; the              -         } Wu          -Skaction *action = [Skaction moveto:cgpointmake (Location.x, LOCATION.Y) Duration:0]; About          $ [Firendplane runaction:action]; -     } -     

Two

http://download.csdn.net/detail/liaohang1987x/9610880

IOS 2D Game Development Framework spritekit--> (Create User Role Wizard-original)

Related Article

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.