在SpriteKit中移动摄像头

/ /更新已更新的代码已被添加,按我的预期工作。 请参阅下面更新的代码中的didSimulatePhysics方法。 在我的情况下,我只关心在x轴上向左或向右移动字符,其中x轴上的0是绝对的左侧和右侧上的x轴是一个可configuration的值。 苹果冒险游戏真的帮了很多。

/ /原来的职位以下

我正在与苹果SpriteKit工作,我正在努力实现一个相机,因为我希望它的行为。 我在代码中所做的是在开始时加载一个精灵字符,两个button和一个在视图外右侧的红色框。 我想要做的就是用button移动angular色,一旦玩家到达屏幕的中间或末端,摄像机就会重新调整,以揭示视图中看不到的东西。 因此,一旦玩家到达那里,最终应该显示最初在视野外的红框。 但是,在下面使用的代码中,我无法让摄像机跟随并根据主angular调整坐标。 我已经看过苹果的高级场景处理文档以及其他一些堆栈溢出post,但似乎无法做到。 如果任何人可以提供一些build议,将不胜感激。

在这里输入图像说明

#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 totalScreenSize = CGRectMake(0, 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:totalScreenSize]; 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)创build一个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创build的地图,则可以使用免费的SKAToolKit框架 。 function包括玩家相机自动跟踪,testing播放器,testing平视显示器和精灵button。