Sprite Kit – 当精灵离开屏幕时,移动场景和相机与精灵

我正在尝试创build一个从屏幕上延伸一定距离的场景,并且我希望相机保持以播放器节点为中心。 当玩家到达屏幕的顶部时,我希望底部节点消失在屏幕边界之下并且场景的新的屏幕外部分变得可见。 像马里奥!

我创build这样的场景大小:

CGSize screenSize = CGSizeMake(skView.bounds.size.width, skView.bounds.size.height + (skView.bounds.size.height/3));//+ (skView.bounds.size.height/3) scene = [GameScene sceneWithSize:screenSize]; 

然后,将所有的节点添加到WorldNode中,就像这个问题一样。

我已经修改它是:

 - (void)didSimulatePhysics { CGFloat margin = self.size.height/3;// previously defined // make sure the cat's position is defined in scene coordinates CGPoint catPosition = [self convertPoint:cat.position fromNode:cat.parent]; if (catPosition.y > (self.size.height - margin)) { CGPoint worldPosition = worldNode.position; worldPosition.y -= catPosition.y - (self.size.height - margin); worldNode.position = worldPosition; } else if (catPosition.y < (self.size.height - margin)) { CGFloat maxWorldHeight = self.size.height; CGPoint worldPosition = worldNode.position; // this keeps the cat on the margin line if (worldPosition.y != worldStartPosition.y) { worldPosition.y += (self.size.height - margin) - catPosition.y; } if (worldPosition.y > maxWorldHeight) // assume maxWorldHeight is defined { worldPosition.y = maxWorldHeight; } worldNode.position = worldPosition; } } 

好吧,我已经几乎工作了。 我似乎无法把世界阻挡在这个场景的最高边界,而当他到达边缘以下时,angular色并没有掉下来,因为在这个世界上,他一直跟着他走过世界最初的起点。 如果angular色在边距以上,我怎么才能让屏幕上下移动,然后如果angular色在边距以下,屏幕不移动?

你想把你的世界节点向下移动的数量等于猫在你想要的位置之上。 你的didSimulatePhysics方法最终会看起来像这样:

 - (void)didSimulatePhysics { CGFloat margin; // set this to be whatever you want it to be // make sure the cat's position is defined in scene coordinates CGPoint catPosition = [self convertPoint:cat.position fromNode:cat.parent]; if (catPosition.y > (self.size.height - margin)) { // the cat is above the margin, we need to move the world node down until the // cat is at the margin // pull world position into local variable so you can modify the y component CGPoint worldPosition = worldNode.position; // move the world down so the cat is on the margin worldPosition.y -= catPosition.y - (self.size.height - margin); worldNode.position = worldPosition; } else { // the cat is below the margin, we need to move the world node up, but only // until the world node is at its starting position CGPoint worldPosition = worldNode.position; // move the cat up to the margin line worldPosition.y += (self.size.height - margin) - catPosition.y; // if this caused the world to be too high, move it back down if (worldPosition.y > maxWorldHeight) { worldPosition.y = maxWorldHeight; } worldNode.position = worldPosition; } } 

有一点需要注意的是, maxWorldHeight并不是世界的高度,而是高于屏幕底部允许的高度。 当你第一次把它放在场景中时,你应该把它设置成单词节点位置的y分量。

 - (void)didSimulatePhysics { CGPoint target = [self pointToCenterViewOn:cat.position]; CGPoint newPosition = _worldNode.position; newPosition.x += (target.x - worldNode.position.x) * 0.1f; newPosition.y += (target.y - worldNode.position.y) * 0.1f; worldNode.position = newPosition; } - (CGPoint)pointToCenterViewOn:(CGPoint)centerOn { CGFloat x = Clamp(centerOn.x, self.size.width / 2, worldNode.size.width - self.size.width / 2); CGFloat y = Clamp(centerOn.y, self.size.height / 2, worldNode.size.height - self.size.height/2); return CGPointMake(-x, -y); }