IOS 2D游戏开发框架 SpriteKit-->续(postion,锚点,游戏滚动场景)

时间:2022-03-12 18:58:43

本人开发的开发者技术变现资源聚集地,大家支持下,下面是网址

https://www.baiydu.com

一、 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),锚点我把它理解为试图在一个范围内重复执行一个相同动作行为的支撑点,下面我截图说东西。

IOS 2D游戏开发框架 SpriteKit-->续(postion,锚点,游戏滚动场景)这里就是一个视图上可以取的锚点值,本来想传一个录制的视频,形象的说明下这东西,但是tmd发现不能传视频,来个详细地址,要深入理解自己去看。

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

三、滚动场景背景图

关于实现滚动场景的背景图滚动的方法很多,这里只是我自己的方式,你也许会有更好的,首先滚动的背景图要想无缝连接上,那必须要美工把图做好了,这里我是用的三个精灵节点作为背景,当每个精灵节点的postion y超出顶部屏幕时,就将最后增加的那个背景节点的postion赋值给它,下面贴代码。

 #import "GameScene.h"
@implementation GameScene {
//场景背景节点精灵存放数组
NSMutableArray *NearbyArray; } /*初始化场景的方法,在uiviwecontroller 初始化场景时调用的方法*/
- (instancetype)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) { NearbyArray=[[NSMutableArray alloc]init];
/*第一个场景背景节点*/
UIImage *farTextureImage=[UIImage imageNamed:@"planeBack"];
SKTexture *farTexture = [SKTexture textureWithImage:farTextureImage]; SKSpriteNode *farTextureSpriteOne = [SKSpriteNode spriteNodeWithTexture:farTexture size:self.size];
// farTextureSpriteOne.anchorPoint=CGPointMake(DEVICE_Width/2, DEVICE_Height/2);
farTextureSpriteOne.zPosition=;
farTextureSpriteOne.position=CGPointMake(self.frame.size.width/, self.frame.size.height/ ); /*第二个场景背景节点*/
UIImage *farTextureImageTwo=[UIImage imageNamed:@"planeBack"];
SKTexture *farTextureTwo = [SKTexture textureWithImage:farTextureImageTwo];
SKSpriteNode *farTextureSpriteTwo = [SKSpriteNode spriteNodeWithTexture:farTextureTwo size:self.size];
//farTextureSpriteTwo.anchorPoint=CGPointMake(0, 0);
farTextureSpriteTwo.zPosition=;
farTextureSpriteTwo.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/-)); /*第三个场景背景节点*/
UIImage *farTextureImageThree=[UIImage imageNamed:@"planeBack"];
SKTexture *farTextureThree = [SKTexture textureWithImage:farTextureImageThree]; SKSpriteNode *farTextureSpriteThree =[SKSpriteNode spriteNodeWithTexture:farTextureThree size:self.size]; farTextureSpriteThree.zPosition=;
farTextureSpriteThree.position=CGPointMake(farTextureSpriteOne.position.x, -(self.frame.size.height/+self.frame.size.height-)); [self addChild:farTextureSpriteOne];
[self addChild:farTextureSpriteTwo];
[self addChild:farTextureSpriteThree]; /*把三个场景背景节点加到一个数组中去,等会滚动之后,才好快速获取每个节点,重置postion*/
[NearbyArray addObject:farTextureSpriteOne];
[NearbyArray addObject:farTextureSpriteTwo];
[NearbyArray addObject:farTextureSpriteThree]; }
return self; } /*设置背景图片滚动的方法*/
-(void)BackMove:(CGFloat)moveSpeed
{ for (int i=; i<NearbyArray.count; i++) {
SKSpriteNode *TempSprite=[NearbyArray objectAtIndex:i]; [TempSprite setPosition:CGPointMake(TempSprite.position.x,TempSprite.position.y+moveSpeed)];
} //循环滚动算法
SKSpriteNode *RollOneSprite=[NearbyArray objectAtIndex:];
SKSpriteNode *RollTwoSprite=[NearbyArray objectAtIndex:];
SKSpriteNode *ThreeBackSprit=[NearbyArray objectAtIndex:]; if (RollOneSprite.position.y>(self.frame.size.height/+self.frame.size.height))
{
RollOneSprite.position=CGPointMake(RollOneSprite.position.x, -(self.frame.size.height/+self.frame.size.height-)); }
if (RollTwoSprite.position.y>(self.frame.size.height/+self.frame.size.height)) {
RollTwoSprite.position=CGPointMake(RollOneSprite.position.x, -(self.frame.size.height/+self.frame.size.height-)); }
if (ThreeBackSprit.position.y>(self.frame.size.height/+self.frame.size.height)) {
ThreeBackSprit.position=CGPointMake(RollOneSprite.position.x, -(self.frame.size.height/+self.frame.size.height-)); }
} /*这个方法是spritekit 的场景自带的,每过一秒就会被调用*/
-(void)update:(CFTimeInterval)currentTime {
[self BackMove:]; } @end

UIViewController调用

 - (void)viewDidLoad
{
[super viewDidLoad]; // Configure the view.
SKView * skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = YES; GameScene *scene = [[GameScene alloc]initWithSize:skView.bounds.size]; scene.scaleMode = SKSceneScaleModeAspectFill; [skView presentScene:scene]; }

本人创业做的一款androidApp, 下载量已经有2000多万,各种当前热门的网络手机奖励红包全部集成,另外还有热门电影和淘宝高额优惠券!很适合各类型的用户。

 IOS 2D游戏开发框架 SpriteKit-->续(postion,锚点,游戏滚动场景)