This includes the following Spritekit I will be around a case, this case is a simple 2d aircraft war game, today I say the creation of hostile elves, is hostile aircraft, hostile aircraft is constantly refreshed to the screen .... Of course, the class involved in fact is still, elves, and materials two classes, the two classes in the first two cases have appeared, the use of the same method, mainly look at the logic, I here is mainly to achieve a period of time every interval screen to refresh a hostile aircraft. The plane moves from the screen height to the starting point, while the potion moves to 0 o'clock to move the enemy aircraft out.
1 /*This method is Spritekit with the scene, and is called every second .*/2-(void) Update: (cftimeinterval) currenttime {3[Self Backmove:1];4 5[Self initenemysprite];//This increase in the method of creating hostile aircraft6 7 8 }9 /*creating hostile aircraft*/Ten-(void) Initenemysprite One { A /*This method is placed in the update, so is executed once per second, the following three variables is to control the speed of the aircraft refresh, if not control, the screen will be refreshed every second plane out, then the screen will be full, here every 35 seconds refresh a small address every 400 seconds to refresh a medium-sized aircraft, Brush a big plane every 700 seconds.*/ -_smallplanetime++; -_mediumplanetime++; the_bigplanetime++; - - - //int radomnumber= (arc4random ()%) + 0; + intspritex=Device_width; - /*The position of the random Elf on the x-axis*/ + intx = (Arc4random ()% (spritex- -)) + $; A at intSpeed =0; - - - if(_smallplanetime> *) { -UIImage *fartextureimagethree=[uiimage imagenamed:@"Membertwo"]; -Sktexture *fartexturethree =[Sktexture Texturewithimage:fartextureimagethree]; in - toSkspritenode *foeplane = [Skspritenode spritenodewithtexture:fartexturethree size:cgsizemake ( fartexturethree.size.width/2.5, fartexturethree.size.height/2.5)]; + //increase the range of hostile aircraft subjected to power sensing -Foeplane.physicsbody =[Skphysicsbody bodyWithRectangleOfSize:foePlane.size]; the /*The speed at which the plane moves down*/ *Speed= (Arc4random ()%5) +2; $ Panax NotoginsengFoeplane.position =cgpointmake (x, self.size.height); -foeplane.zposition=1; the /*The following three properties are used to set the gravity detection properties of hostile aircraft, after which the user-operated aircraft launches bullets on top of them, and the following properties work.*/ +FoePlane.physicsBody.categoryBitMask =Skrolecategoryfoeplane; AFoePlane.physicsBody.collisionBitMask =Skrolecategorybullet; theFoePlane.physicsBody.contactTestBitMask =Skrolecategorybullet; + [self addchild:foeplane]; - /*moves the sprite from the parent node when the wizard's Y coordinate is 0 o'clock*/ $[Foeplane runaction:[skaction sequence:@[[skaction Movetoy:0Duration:speed],[skaction Removefromparent]] completion:^{ $ [Foeplane removefromparent]; - }]; -_smallplanetime=0; the - }Wuyi the if(_mediumplanetime> -) { - Wu - AboutUIImage *fartextureimagethree=[uiimage imagenamed:@"Teamer"]; $Sktexture *fartexturethree =[Sktexture Texturewithimage:fartextureimagethree]; -Skspritenode *foeplane = [Skspritenode spritenodewithtexture:fartexturethree size:cgsizemake ( fartexturethree.size.width/2.5, fartexturethree.size.height/2.5)]; - //increase the range of hostile aircraft subjected to power sensing -Foeplane.physicsbody =[Skphysicsbody bodyWithRectangleOfSize:foePlane.size]; A /*The speed at which the plane moves down*/ +Speed = (arc4random ()%2) +2; the -Foeplane.position =cgpointmake (x, self.size.height); $ thefoeplane.zposition=1; the /*The following three properties are used to set the gravity detection properties of hostile aircraft, after which the user-operated aircraft launches bullets on top of them, and the following properties work.*/ theFoePlane.physicsBody.categoryBitMask =Skrolecategoryfoeplane; theFoePlane.physicsBody.collisionBitMask =Skrolecategorybullet; -FoePlane.physicsBody.contactTestBitMask =Skrolecategorybullet; in [self addchild:foeplane]; the /*moves the sprite from the parent node when the wizard's Y coordinate is 0 o'clock*/ the[Foeplane runaction:[skaction sequence:@[[skaction Movetoy:0Duration:speed],[skaction Removefromparent]] completion:^{ About [Foeplane removefromparent]; the }]; the_mediumplanetime=0; the + } - the Bayi if(_bigplanetime> the) { the the - - the the theUIImage *fartextureimagethree=[uiimage imagenamed:@"Unknown"]; theSktexture *fartexturethree =[Sktexture Texturewithimage:fartextureimagethree]; - the theSkspritenode *foeplane = [Skspritenode spritenodewithtexture:fartexturethree size:cgsizemake ( fartexturethree.size.width/2.5, fartexturethree.size.height/2.5)]; the 94 the //increase the range of hostile aircraft subjected to power sensing theFoeplane.physicsbody =[Skphysicsbody bodyWithRectangleOfSize:foePlane.size]; the 98 /*The speed at which the plane moves down*/ AboutSpeed = (arc4random ()%5) +3; -Foeplane.position =cgpointmake (x, self.size.height);101foeplane.zposition=1;102 /*The following three properties are used to set the gravity detection properties of hostile aircraft, after which the user-operated aircraft launches bullets on top of them, and the following properties work.*/103FoePlane.physicsBody.categoryBitMask =Skrolecategoryfoeplane;104FoePlane.physicsBody.collisionBitMask =Skrolecategorybullet; theFoePlane.physicsBody.contactTestBitMask =Skrolecategorybullet;106 [self addchild:foeplane];107 /*moves the sprite from the parent node when the wizard's Y coordinate is 0 o'clock*/108[Foeplane runaction:[skaction sequence:@[[skaction Movetoy:5Duration:speed],[skaction Removefromparent]] completion:^{109 [Foeplane removefromparent]; the }];111_bigplanetime=0; the 113 } the the the 117 118 119}
In fact, here the Elves and material Class I have encapsulated the class, here in order to display the code, so directly to the package class code together. This is not recommended for development.
Here are the following: http://download.csdn.net/detail/qq_35826634/9599204 interested friends can go down and see
IOS 2D Game Development Framework spritekit--> (create hostile sprites)