大佬教程收集整理的这篇文章主要介绍了ios – 在SpriteKit中移动相机,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
// ORIGINAL POST BELOW
我正在与苹果SpriteKit合作,我正在努力实现一个相机,就像我想要的那样.我在代码中所做的是在开始时加载一个精灵字符,两个按钮和一个红色框,该对话框在视图外部.我想要做的是用按钮移动角色,一旦播放器到达屏幕的中间或结尾,相机将重新调整,以发现视图中看不到的内容.所以向右移动应该最终显示一旦玩家到达那里,最初在视图之外的红色框.但是,使用下面的代码,我无法让相机跟随并将坐标调整到主角色.我看过苹果公司的高级场景处理文档以及一些其他的堆栈溢出帖子,但似乎无法正确.如果任何人可以提供一些建议,将不胜感激.
#define cameraEdge 150 -(id)initWithSize:(CGSizE)size { if (self = [super initWithSize:size]) { /* Setup your scene here */ //320 568 self.BACkgroundColor = [SKColor whiteColor]; myWorld = [[SKNode alloc] init]; [self addChild:myWorld]; mainCharacter = [SKSpriteNode spriteNodeWithImagenamed:@"0"]; mainCharacter.physicsBody.dynamic = YES; mainCharacter.name = @"player"; mainCharacter.position = CGPointMake(20,20); CGRect @R_86_10586@lScreenSize = CGRectMake(0,800,320); SKSpriteNode *Box = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(60,60)]; SKSpriteNode *BoxTwo = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(60,60)]; SKSpriteNode *BoxThree = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(60,60)]; BoxThree.position = CGPointMake(40,50); [myWorld addChild:BoxThree]; BoxTwo.position = CGPointMake(1100,50); Box.position = CGPointMake(650,50); [myWorld addChild:Box]; [myWorld addChild:BoxTwo]; self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:@R_86_10586@lScreenSize]; self.physicsWorld.gravity = CGVectorMake(0,-5); mainCharacter.name = @"mainCharacter"; mainCharacter.physicsBody.linearDamping = 0; mainCharacter.physicsBody.friction = 0; mainCharacter.physicsBody.restitution = 0; mainCharacter.physicsBody = [SKPhysicsBody bodyWithRectangLeofSize:mainCharacter.size]; [myWorld addChild:mainCharacter]; [self addChild:[self buildLeftButton]]; [self addChild:[self buildrightButton]]; } return self; } - (void)didSimulatePhysics { SKSpriteNode *hero = mainCharacter; if(hero) { CGPoint heroPosition = hero.position; CGPoint worldPosition = myWorld.position; NSLog(@"%f",heroPosition.X); CGFloat xCoordinate = worldPosition.x + heroPosition.x; if(xCoordinate < cameraEdge && heroPosition.x > 0) { worldPosition.x = worldPosition.x - xCoordinate + cameraEdge; self.worldMovedForupdate = YES; } else if(xCoordinate > (self.frame.size.width - cameraEdgE) && heroPosition.x < 2000) { worldPosition.x = worldPosition.x + (self.frame.size.width - xCoordinatE) - cameraEdge; self.worldMovedForupdate = YES; } myWorld.position = worldPosition; } } -(SKSpriteNode *)buildLeftButton { SKSpriteNode *leftButton = [SKSpriteNode spriteNodeWithImagenamed:@"left"]; leftButton.position = CGPointMake(20,20); leftButton.name = @"leftButton"; leftButton.zPosition = 1.0; return leftButton; } -(SKSpriteNode *)buildrightButton { SKSpriteNode *leftButton = [SKSpriteNode spriteNodeWithImagenamed:@"right"]; leftButton.position = CGPointMake(60,20); leftButton.name = @"rightButton"; leftButton.zPosition = 1.0; return leftButton; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UITouch *touch = [touches anyObject]; CGPoint LOCATIOn = [touch LOCATIOnInNode:self]; SKNode *node = [self nodeAtPoint:LOCATIOn]; if([node.name isEqualToString:@"leftButton"]) { [mainCharacter.physicsBody applyImpulse:CGVectorMake(-120,0)]; } else if([node.name isEqualToString:@"rightButton"]) { [mainCharacter.physicsBody applyImpulse:CGVectorMake(120,10)]; } }
1)创建SKNode并将其称为myWorld,worldNode或任何其他名称.
2)添加worldNode [self addChild:worldNode];
3)将所有其他节点添加到worldNode,包括您的播放器.
4)在didSimulatePhysics方法中,添加以下代码:
worldNode.position = CGPointMake(-(player.position.x-(self.size.width/2)),-(player.position.y-(self.size.height/2)));
您的观点现在将始终以玩家的位置为中心.
2015年5月更新:
如果您使用的是使用Tiled Map Editor创建的地图,则可以使用免费的SKAToolKit framework.功能包括播放器相机自动跟踪,测试播放器,测试HUD和精灵按钮.
以上是大佬教程为你收集整理的ios – 在SpriteKit中移动相机全部内容,希望文章能够帮你解决ios – 在SpriteKit中移动相机所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。