IOS 2D Game Development Framework spritekit--> (perfect role function)

Source: Internet
Author: User
Tags addchild spritekit

I. Description
Today, the character wizard adds a bullet launch function, which increases the collision detection between the bullet and the hostile elf, which disappears from the screen when the character sprite bullets collide with the opposing elves.

Second, the change of scene layer Skscene

1. Add the following code to the method of initializing the scene layer
Self.physicsWorld.contactDelegate = self;

self.physicsWorld.gravity = Cgvectormake (0,0);

2, the initial scene layer to increase the scene of dynamic detection agent

1-(Instancetype) Initwithsize: (cgsize) Size2 {3      if(self =[Super Initwithsize:size]) {4         5nearbyarray=[[Nsmutablearray alloc]init];6           /*first scene background node*/7UIImage *fartextureimage=[uiimage imagenamed:@"Planeback"];8Sktexture *fartexture =[Sktexture texturewithimage:fartextureimage];9         TenSkspritenode *fartexturespriteone =[Skspritenode spritenodewithtexture:fartexture size:self.size]; One         //Fartexturespriteone.anchorpoint=cgpointmake (DEVICE_WIDTH/2, DEVICE_HEIGHT/2); Afartexturespriteone.zposition=0; -Fartexturespriteone.position=cgpointmake (self.frame.size.width/2, self.frame.size.height/2 ); -          the          -          -          -             /*second scene background node*/ +UIImage *fartextureimagetwo=[uiimage imagenamed:@"Planeback"]; -Sktexture *fartexturetwo =[Sktexture texturewithimage:fartextureimagetwo]; +Skspritenode *fartexturespritetwo =[Skspritenode spritenodewithtexture:fartexturetwo size:self.size]; A         //fartexturespritetwo.anchorpoint=cgpointmake (0, 0); atfartexturespritetwo.zposition=0; -Fartexturespritetwo.position=cgpointmake (fartexturespriteone.position.x,-(self.frame.size.height/2-Ten)); -   -          -          -         /*A third scene background node*/ inUIImage *fartextureimagethree=[uiimage imagenamed:@"Planeback"]; -Sktexture *fartexturethree =[Sktexture Texturewithimage:fartextureimagethree]; to  +Skspritenode *fartexturespritethree =[Skspritenode spritenodewithtexture:fartexturethree size:self.size]; -  thefartexturespritethree.zposition=0; *Fartexturespritethree.position=cgpointmake (fartexturespriteone.position.x,-(self.frame.size.height/2+self.frame.size.height- -)); $         Panax Notoginseng          -          the          + [self addchild:fartexturespriteone]; A [self addchild:fartexturespritetwo]; the [self addchild:fartexturespritethree]; +          -         /*add three scene background nodes to an array, wait for it to scroll, then get each node quickly, reset postion*/ $ [Nearbyarray Addobject:fartexturespriteone]; $ [Nearbyarray addobject:fartexturespritetwo]; - [Nearbyarray Addobject:fartexturespritethree]; -          the           -           Wuyi   theFirendplane=[Skfoeplane Createplayerplane]; -firendplane.zposition=1; WuFirendplane.position=cgpointmake (self.frame.size.width/2, - ); - [self addchild:firendplane]; About          /*Add two wizard after generating the power of the agent*/ $Self.physicsWorld.contactDelegate =Self ; -Self.physicsWorld.gravity = Cgvectormake (0,0); -     } -     returnSelf ; A  +}

Third, initialize the role of the wizard's Bullets

1-(void) Playerbulletfire2 {3     4bullettime++;5    if(bullettime==Ten) {6UIImage *roleplaneimage=[uiimage imagenamed:@"Plus"];7         8Sktexture *roleplaneimagetextture =[Sktexture texturewithimage:roleplaneimage];9Firendplane.playerbulletsprite=[skspritenode spritenodewithtexture:roleplaneimagetextture Size:CGSizeMake (DEVICE _width*0.06, device_width*0.06)];Tenfirendplane.playerbulletsprite.zposition=1; OneFirendplane.playerbulletsprite.position=cgpointmake (firendplane.position.x, firendplane.position.y+Ten ); A [self addChild:FirendPlane.PlayerBulletSprite]; -FirendPlane.PlayerBulletSprite.physicsBody =[Skphysicsbody bodyWithRectangleOfSize:FirendPlane.PlayerBulletSprite.size]; -         theFirendPlane.PlayerBulletSprite.physicsBody.collisionBitMask =1; -         -[Firendplane.playerbulletsprite runaction:[skaction sequence:@[[skaction movetoy:device_height+ -Duration1.5],[skaction Removefromparent]]; -Bullettime=0; +     } -     +      A}

Iv. methods for adding bullet initialization in update

2 -(void) Update: (cftimeinterval) currenttime {3    7    [ Self Playerbulletfire]; 8 }

V. Realization of the scene dynamic detection agent

Here's what I'm saying: Since the code for the gravitational collision detection of bullets and hostile elves has been added to the previous code, the scene will automatically execute when the two units of this type collide with each other.

Below the agent, the following agent is to move two colliding sprites out of the scene layer.

1  -(void) Didbegincontact: (Skphysicscontact *)contact2  {3   [ Contact.bodyA.node Removefromparent]; 4    [Contact.bodyB.node removefromparent]; 5    6  }

Vi. Address of the case

Case http://download.csdn.net/detail/liaohang1987x/9612989

IOS 2D Game Development Framework spritekit--> (perfect role function)

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.