IOS 2D game development framework SpriteKit -- & gt; continued (create enemy genie), iosspritekit --

Source: Internet
Author: User
Tags spritekit

IOS 2D game development framework SpriteKit --> continuous (create enemy genie), iosspritekit --

This includes the spritekit mentioned later. I will focus on a case. This case is a simple 2d airplane war game. Today, I am talking about creating a hostile genie, that is, a hostile plane, hostile planes are constantly refreshed to the screen ..... of course, the classes involved here are actually the genie and material classes. The first two cases of these classes have already appeared. They are used in the same way, mainly based on logic, I am here mainly to refresh a hostile plane every time. The plane moves down from the screen height to the starting point. When potion moves to 0, the opposite plane is removed.

1/* This method is inherent in the spritekit scenario, and is called every second */2-(void) update :( CFTimeInterval) currentTime {3 [self BackMove: 1]; 4 5 [self initEnemySprite]; // Method 6 7 8} 9/* create a hostile aircraft */10-(void) initEnemySprite 11 {12/* This method is placed in update, so it is executed once per second. The following three variables control the plane refresh speed. If not controlled, the screen will refresh an airplane every second, and the screen will fill up in a short time. Here, a small address is refreshed every 35 seconds and a medium-sized aircraft is refreshed every 400 seconds, 1 large plane */13 _ smallPlaneTime ++; 14 _ mediumPlaneTime ++; 15 _ bigPlaneTime ++ every 700 seconds; 16 17 18 // int RadomNumber = (arc4random () % 100) + 0; 19 int SpriteX = DEVICE_Width; 20/* random sprite position on the x axis */21 int x = (arc4random () % (SpriteX-90) + 45; 22 23 int speed = 0; 24 25 26 if (_ smallPlaneTime> 35) {27 UIImage * Representation = [UIImage imageNamed: @ "MemberTwo"]; 28 SKTexture * farTextureThree = [SKTexture textureWithImage: representation]; 29 30 31 SKSpriteNode * foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size: CGSizeMake (farTextureThree. size. width/2.5, farTextureThree. size. height/2.5)]; 32 // increase the range of power sensing of a hostile aircraft by 33 foePlane. physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: foePlane. size]; 34/* aircraft downward movement speed */35 speed = (arc4random () % 5) + 2; 36 37 foePlane. position = CGPointMake (x, self. size. height); 38 foePlane. zPosition = 1; 39/* the following three attributes are used to set the gravity detection attributes of the enemy aircraft, which will be used later. For example, the planes operated by the user then launch bullets on them, the following attribute takes effect */40 foePlane. physicsBody. categoryBitMask = SKRoleCategoryFoePlane; 41 foePlane. physicsBody. collisionBitMask = SKRoleCategoryBullet; 42 foePlane. physicsBody. contactTestBitMask = SKRoleCategoryBullet; 43 [self addChild: foePlane]; 44/* remove the genie from the parent node when the y coordinate of the genie is 0 */45 [foePlane runAction: [SKAction sequence: @ [SKAction moveToY: 0 duration: speed], [SKAction removeFromParent] completion: ^ {46 [foePlane removeFromParent]; 47}]; 48 _ smallPlaneTime = 0; 49 50} 51 52 if (_ mediumPlaneTime> 400) {53 54 55 56 UIImage * watermark = [UIImage imageNamed: @ "Teamer"]; 57 SKTexture * farTextureThree = [SKTexture textureWithImage: farTextureImageThree]; 58 SKSpriteNode * foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size: CGSizeMake (farTextureThree. size. width/2.5, farTextureThree. size. height/2.5)]; 59 // increase the range of power sensing for a hostile aircraft by 60 foePlane. physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: foePlane. size]; 61/* aircraft downward movement speed */62 speed = (arc4random () % 2) + 2; 63 64 foePlane. position = CGPointMake (x, self. size. height); 65 66 foePlane. zPosition = 1; 67/* the following three attributes are used to set the gravity detection attributes of a hostile aircraft, which will be used later. For example, the planes operated by the user then launch bullets on them, the following attribute takes effect */68 foePlane. physicsBody. categoryBitMask = SKRoleCategoryFoePlane; 69 foePlane. physicsBody. collisionBitMask = SKRoleCategoryBullet; 70 foePlane. physicsBody. contactTestBitMask = SKRoleCategoryBullet; 71 [self addChild: foePlane]; 72/* remove the genie from the parent node when the y coordinate of the genie is 0 */73 [foePlane runAction: [SKAction sequence: @ [SKAction moveToY: 0 duration: speed], [SKAction removeFromParent] completion: ^ {74 [foePlane removeFromParent]; 75}]; 76 _ mediumPlaneTime = 0; 77 78} 79 80 81 if (_ bigPlaneTime> 700) {82 83 84 85 86 87 88 UIImage * farTextureImageThree = [UIImage imageNamed: @ "Unknown"]; 89 SKTexture * farTextureThree = [SKTexture textureWithImage: plaintext]; 90 91 92 SKSpriteNode * foePlane = [SKSpriteNode plaintext: farTextureThree size: CGSizeMake (farTextureThree. size. width/2.5, farTextureThree. size. height/2.5)]; 93 94 95 // increase the range of power sensing of a hostile aircraft by 96 foePlane. physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: foePlane. size]; 97 98/* aircraft downward movement speed */99 speed = (arc4random () % 5) + 3; 100 foePlane. position = CGPointMake (x, self. size. (height); 101 foePlane. zPosition = 1; 102/* the following three attributes are used to set the gravity detection attributes of the enemy aircraft, which will be used later. For example, the planes operated by the user then launch bullets on them, the following attribute takes effect */103 foePlane. physicsBody. categoryBitMask = SKRoleCategoryFoePlane; 104 foePlane. physicsBody. collisionBitMask = skrolecategorybullet; 105 foePlane. physicsBody. contactTestBitMask = SKRoleCategoryBullet; 106 [self addChild: foePlane]; 107/* remove the genie from the parent node when the y coordinate of the genie is 0 */108 [foePlane runAction: [SKAction sequence: @ [SKAction moveToY: 5 duration: speed], [SKAction removeFromParent] completion: ^ {109 [foePlane removeFromParent]; 110}]; 111 _ bigPlaneTime = 0; 112 113} 114 115 116 117 118 119}

 

In fact, the genie and material classes here are encapsulated classes. In order to display the code, the encapsulated class code is directly written together. This is not recommended during development.

 

Below is: http://download.csdn.net/detail/qq_35826634/9599204 Interested friends can go down to see

 

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.