一、 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开发中这个属性一般在使用动画时才会使用到。
一、 锚点
锚点的取值范围是(0,0.5,1)试图的默认锚点是:(0.5,0.5),锚点我把它理解为试图在一个范围内重复执行一个相同动作行为的支撑点,下面我截图说东西。
这里就是一个视图上可以取的锚点值,本来想传一个录制的视频,形象的说明下这东西,但是tmd发现不能传视频,来个详细地址,要深入理解自己去看。
http://jingyan.baidu.com/album/9f63fb918fa9ddc8400f0ec5.html?picindex=4
三、滚动场景背景图
关于实现滚动场景的背景图滚动的方法很多,这里只是我自己的方式,你也许会有更好的,首先滚动的背景图要想无缝连接上,那必须要美工把图做好了,这里我是用的三个精灵节点作为背景,当每个精灵节点的postion y超出顶部屏幕时,就将最后增加的那个背景节点的postion赋值给它,下面贴代码。
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 }
时间: 2024-10-13 14:12:36