IOS 2D遊戲開發框架 SpriteKit-->續(postion,錨點,遊戲滾動場景)

本人開發的開發者技術變現資源彙集地,你們支持下,下面是網址html

https://www.baiydu.comandroid

 

 

1、 Postion算法

這裏的postion和app開發中的layer.postion是同樣的,postion的值表明該視圖在父節點中的相對位置, 好比一個試圖的父節點是self.view, 這裏咱們想讓這個試圖在self.view中居中顯示,那麼layer.postion=CGPointMake(self.view.frame.size.widht/2,self.view.frame.size.height/2)...座標就是標準的二維座標系,x減小,位置像左移,x增長像右移, y增長像上移,y減小像下移,中心點是座標原點。app開發中這個屬性通常在使用動畫時纔會使用到。  數組

1、 錨點網絡

  錨點的取值範圍是(0,0.5,1)試圖的默認錨點是:(0.5,0.5),錨點我把它理解爲試圖在一個範圍內重複執行一個相同動做行爲的支撐點,下面我截圖說東西。app

  這裏就是一個視圖上能夠取的錨點值,原本想傳一個錄製的視頻,形象的說明下這東西,可是tmd發現不能傳視頻,來個詳細地址,要深刻理解本身去看。post

http://jingyan.baidu.com/album/9f63fb918fa9ddc8400f0ec5.html?picindex=4動畫

 

3、滾動場景背景圖ui

  關於實現滾動場景的背景圖滾動的方法不少,這裏只是我本身的方式,你也許會有更好的,首先滾動的背景圖要想無縫鏈接上,那必需要美工把圖作好了,這裏我是用的三個精靈節點做爲背景,當每一個精靈節點的postion y超出頂部屏幕時,就將最後增長的那個背景節點的postion賦值給它,下面貼代碼。spa

  1 #import "GameScene.h"
  2 @implementation GameScene
  3 
  4 {
  5    //場景背景節點精靈存放數組
  6     NSMutableArray *NearbyArray;
  7     
  8 }
  9 
 10 /*初始化場景的方法,在uiviwecontroller 初始化場景時調用的方法*/
 11 - (instancetype)initWithSize:(CGSize)size
 12 {
 13      if (self = [super initWithSize:size]) {
 14         
 15         NearbyArray=[[NSMutableArray alloc]init];
 16           /*第一個場景背景節點*/
 17         UIImage  *farTextureImage=[UIImage imageNamed:@"planeBack"];
 18         SKTexture *farTexture = [SKTexture  textureWithImage:farTextureImage];
 19         
 20         SKSpriteNode  *farTextureSpriteOne = [SKSpriteNode spriteNodeWithTexture:farTexture size:self.size];
 21         // farTextureSpriteOne.anchorPoint=CGPointMake(DEVICE_Width/2, DEVICE_Height/2);
 22         farTextureSpriteOne.zPosition=0;
 23          farTextureSpriteOne.position=CGPointMake(self.frame.size.width/2, self.frame.size.height/2 );
 24         
 25         
 26         
 27         
 28             /*第二個場景背景節點*/
 29          UIImage  *farTextureImageTwo=[UIImage imageNamed:@"planeBack"];
 30          SKTexture *farTextureTwo = [SKTexture  textureWithImage:farTextureImageTwo];
 31             SKSpriteNode  *farTextureSpriteTwo = [SKSpriteNode spriteNodeWithTexture:farTextureTwo size:self.size];
 32         //farTextureSpriteTwo.anchorPoint=CGPointMake(0, 0);
 33         farTextureSpriteTwo.zPosition=0;
 34         farTextureSpriteTwo.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/2-10));
 35  
 36         
 37         
 38         /*第三個場景背景節點*/
 39          UIImage  *farTextureImageThree=[UIImage imageNamed:@"planeBack"];
 40         SKTexture *farTextureThree = [SKTexture  textureWithImage:farTextureImageThree];
 41 
 42         SKSpriteNode  *farTextureSpriteThree =[SKSpriteNode spriteNodeWithTexture:farTextureThree size:self.size];
 43 
 44          farTextureSpriteThree.zPosition=0;
 45         farTextureSpriteThree.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/2+self.frame.size.height-20));
 46         
 47         
 48         
 49         
 50         [self addChild:farTextureSpriteOne];
 51         [self addChild:farTextureSpriteTwo];
 52           [self addChild:farTextureSpriteThree];
 53         
 54         /*把三個場景背景節點加到一個數組中去,等會滾動以後,纔好快速獲取每一個節點,重置postion*/
 55         [NearbyArray addObject:farTextureSpriteOne];
 56          [NearbyArray addObject:farTextureSpriteTwo];
 57         [NearbyArray addObject:farTextureSpriteThree];
 58         
 59        
 60     }
 61     return self;
 62 
 63 }
 64 
 65 /*設置背景圖片滾動的方法*/
 66 -(void)BackMove:(CGFloat)moveSpeed
 67 {
 68     
 69     for (int i=0; i<NearbyArray.count; i++) {
 70         SKSpriteNode *TempSprite=[NearbyArray objectAtIndex:i];
 71    
 72     
 73         [TempSprite setPosition:CGPointMake(TempSprite.position.x,TempSprite.position.y+moveSpeed)];
 74     }
 75   
 76     //循環滾動算法
 77     SKSpriteNode *RollOneSprite=[NearbyArray objectAtIndex:0];
 78     SKSpriteNode *RollTwoSprite=[NearbyArray objectAtIndex:1];
 79      SKSpriteNode *ThreeBackSprit=[NearbyArray objectAtIndex:2];
 80     
 81     if (RollOneSprite.position.y>(self.frame.size.height/2+self.frame.size.height))
 82        {
 83          RollOneSprite.position=CGPointMake(RollOneSprite.position.x, -(self.frame.size.height/2+self.frame.size.height-30));
 84    
 85     }
 86       if (RollTwoSprite.position.y>(self.frame.size.height/2+self.frame.size.height)) {
 87             RollTwoSprite.position=CGPointMake(RollOneSprite.position.x, -(self.frame.size.height/2+self.frame.size.height-30));
 88          
 89     }
 90     if (ThreeBackSprit.position.y>(self.frame.size.height/2+self.frame.size.height)) {
 91           ThreeBackSprit.position=CGPointMake(RollOneSprite.position.x, -(self.frame.size.height/2+self.frame.size.height-30));
 92         
 93     }
 94 }
 95 
 96 /*這個方法是spritekit 的場景自帶的,每過一秒就會被調用*/
 97 -(void)update:(CFTimeInterval)currentTime {
 98     [self BackMove:2];
 99     
100     
101 }
102 
103 @end

UIViewController調用

 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4 
 5     // Configure the view.
 6     SKView * skView = (SKView *)self.view;
 7     skView.showsFPS = YES;
 8     skView.showsNodeCount = YES;
 9     /* Sprite Kit applies additional optimizations to improve rendering performance */
10     skView.ignoresSiblingOrder = YES;
11     
12     
13    GameScene *scene = [[GameScene alloc]initWithSize:skView.bounds.size];
14     
15     scene.scaleMode = SKSceneScaleModeAspectFill;
16     
17     [skView presentScene:scene];
18    
19 }

 

 

本人創業作的一款androidApp, 下載量已經有2000多萬,各類當前熱門的網絡手機獎勵紅包所有集成,另外還有熱門電影和淘寶高額優惠券!很適合各種型的用戶。

 

相關文章
相關標籤/搜索