精灵套件 - 当精灵离开屏幕时,用精灵移动场景和摄像机

时间:2014-11-12 19:04:57

标签: ios sprite-kit skscene

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

我创建了这样的场景大小:

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,如This问题。

我将其修改为:

- (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;
    }

}

好的,我几乎要工作了。我似乎无法阻止世界在场景的最顶端,当角色低于边缘时,角色不会下降,因为世界一直跟着他超越世界的初始起点。如果角色位于边距之上,如何让屏幕上下移动,如果角色低于边距,屏幕不会移动?

2 个答案:

答案 0 :(得分:1)

您希望将您的世界节点向下移动一个等于猫高于您想要的距离的数量。您的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分量。

答案 1 :(得分:-1)

- (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);
}