IOS 2D遊戲開發架構 SpriteKit-->續(建立敵對精靈),iosspritekit--

來源:互聯網
上載者:User

IOS 2D遊戲開發架構 SpriteKit-->續(建立敵對精靈),iosspritekit--

  這次包括之後講的spritekit 我都會圍繞一個案例來說,這個案例就是一個簡單的2d飛機大戰遊戲,今天這裡我講建立敵對精靈,就是敵對飛機,敵對飛機不停的被重新整理到螢幕上.....當然這裡涉及到的類其實還是,精靈,和材質兩個類,這兩個類前兩篇的案例中已經出現過,使用方法都一樣,主要看邏輯,我這裡主要是實現每間隔一段時間螢幕就重新整理一個敵對飛機。飛機從螢幕高度為起點往下移動,當potion移動到0時將敵對飛機移出。

  1 /*這個方法是spritekit 的情境內建的,每過一秒就會被調用*/  2 -(void)update:(CFTimeInterval)currentTime {  3     [self BackMove:1];  4       5     [self initEnemySprite];//本次增加的建立敵對飛機的方法  6       7       8 }  9 /*建立敵對飛機*/ 10 -(void)initEnemySprite 11 { 12     /*此方法是放在update裡面的所以是每秒執行一次,下面三個變數就是控制飛機重新整理速度的,如果不控制,螢幕就會每秒都重新整理一個飛機出來,那麼不一會螢幕就會爆滿, 這裡每35秒重新整理一架小型地址每400秒重新整理一架中型飛機, 每700秒刷行一架大飛機*/ 13     _smallPlaneTime++; 14     _mediumPlaneTime++; 15     _bigPlaneTime++; 16      17      18     //int RadomNumber=   (arc4random() % 100) + 0; 19     int SpriteX=DEVICE_Width; 20     /*隨機精靈在x軸的位置*/ 21       int x = (arc4random() % (SpriteX-90)) + 45; 22      23     int speed = 0; 24    25      26     if (_smallPlaneTime>35) { 27         UIImage  *farTextureImageThree=[UIImage imageNamed:@"MemberTwo"]; 28         SKTexture *farTextureThree = [SKTexture  textureWithImage:farTextureImageThree]; 29          30          31         SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)]; 32         //增加敵對飛機受動力感應的範圍 33         foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size]; 34           /*飛機向下移動速度*/ 35          speed= (arc4random() % 5) + 2; 36          37          foePlane.position = CGPointMake(x, self.size.height); 38          foePlane.zPosition=1; 39         /*下面三個屬性是設定敵對飛機的重力檢測屬性,之後它們都會用到,比如之後使用者操作的飛機發射子彈打在它們上面,下面這屬性就起作用了*/ 40         foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane; 41         foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet; 42         foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet; 43         [self addChild:foePlane]; 44         /*當精靈的y座標為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  *farTextureImageThree=[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         //增加敵對飛機受動力感應的範圍 60          foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size]; 61           /*飛機向下移動速度*/ 62           speed = (arc4random() % 2) + 2; 63   64         foePlane.position = CGPointMake(x, self.size.height); 65          66         foePlane.zPosition=1; 67         /*下面三個屬性是設定敵對飛機的重力檢測屬性,之後它們都會用到,比如之後使用者操作的飛機發射子彈打在它們上面,下面這屬性就起作用了*/ 68         foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane; 69         foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet; 70         foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet; 71         [self addChild:foePlane]; 72         /*當精靈的y座標為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:farTextureImageThree]; 90             91             92            SKSpriteNode *foePlane = [SKSpriteNode spriteNodeWithTexture: farTextureThree size:CGSizeMake(farTextureThree.size.width/2.5, farTextureThree.size.height/2.5)]; 93             94             95            //增加敵對飛機受動力感應的範圍 96            foePlane.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:foePlane.size]; 97             98            /*飛機向下移動速度*/ 99             speed = (arc4random() % 5) + 3;100            foePlane.position = CGPointMake(x, self.size.height);101            foePlane.zPosition=1;102            /*下面三個屬性是設定敵對飛機的重力檢測屬性,之後它們都會用到,比如之後使用者操作的飛機發射子彈打在它們上面,下面這屬性就起作用了*/103            foePlane.physicsBody.categoryBitMask = SKRoleCategoryFoePlane;104            foePlane.physicsBody.collisionBitMask = SKRoleCategoryBullet;105            foePlane.physicsBody.contactTestBitMask = SKRoleCategoryBullet;106            [self addChild:foePlane];107            /*當精靈的y座標為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 }

 

其實這裡的精靈和材質類我都是封裝了類的,這裡為了顯示代碼,所以直接把封裝類的代碼寫一起了。開發時不建議這樣做。

 

下面是:http://download.csdn.net/detail/qq_35826634/9599204  有興趣的朋友可以去下來看看

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.