精灵套件 - 使场景向上延伸,而不是向下,或将初始节点位置设置到左下角

时间:2014-11-16 15:40:45

标签: ios sprite-kit skscene

我将场景设置为我的屏幕尺寸乘以三,我的场景位置设置为CGPointZero。当场景加载时,它似乎向下延伸到屏幕边缘以下,或者更确切地说屏幕显示场景的中间部分(很难解释。在击中物理体之前,我的角色将会出现在视线之外的好方法。场景的底部。如何让场景仅向上延伸,或让屏幕显示场景的底部?

- (void)viewWillLayoutSubviews
{
    // Configure the view.
    SKView * skView = (SKView *)self.view;
    if (!skView.scene) {
        skView.showsFPS = YES;
        skView.showsNodeCount = YES;
        skView.ignoresSiblingOrder = YES;
        skView.showsPhysics = YES;
        // Create and configure the scene.
        CGSize screenSize = CGSizeMake(skView.bounds.size.width, skView.bounds.size.height * 3);
        scene = [GameScene sceneWithSize:screenSize];//skView.bounds.size
        scene.gvc = self;
        scene.position = CGPointZero;
        scene.scaleMode = SKSceneScaleModeAspectFill;

        // Present the scene.
        [skView presentScene:scene];
    }
}

在GameScene中添加worldNode:

worldNode = [[SKNode alloc]init];
    [self addChild:worldNode];

所有内容都会添加到worldNode中,并且应该按照我在其他问题here中的字符进行操作。

编辑:我想出了我的问题,但没有找到解决方案。当我将场景大小设置为height * 3时,它设置的是正确的大小,但视图控制器的(0,0)点是左上角。当场景加载时,它显示我的场景的顶部,并且场景延伸到它下面。如何将我的(0,0)场景定位到左下角(所以(0,self.frame.size.height))?

1 个答案:

答案 0 :(得分:1)

创建背景合成

您可以使用下一个功能创建背景合成:

func backgroundNode() -> SKSpriteNode {
  // Create a container node for your background
  let backgroundNode = SKSpriteNode()
  backgroundNode.anchorPoint = CGPointZero
  backgroundNode.name = "background"

  // Start the creation of each part of the background and add it to the container node
  let background1 = SKSpriteNode(imageNamed: "background1")
  background1.anchorPoint = CGPointZero
  background1.position = CGPoint(x: 0, y: 0)
  backgroundNode.addChild(background1)

  let background2 = SKSpriteNode(imageNamed: "background2")
  background2.anchorPoint = CGPointZero
  background2.position = CGPoint(x: 0, y: background1.size.height)
  backgroundNode.addChild(background2)

  let background3 = SKSpriteNode(imageNamed: "background3")
  background3.anchorPoint = CGPointZero
  background3.position = CGPoint(x: 0, y: background1.size.height * 2)
  backgroundNode.addChild(background3)

  // The use of (background1.size.height * 3) depends if your background images have the same
  // height, otherwise just sum al the heights instead of multiply them
  backgroundNode.size = CGSize(width: background1.size.width, height: background1.size.height * 3)
  return backgroundNode
}

现在您可以使用该功能创建和放置您的背景节点:

let background = backgroundNode()
background.anchorPoint = CGPointZero
background.position = CGPointZero
background.name = "background"
addChild(background)

滚动或移动背景

移动背景是一项相当复杂的任务,并且过分依赖于您在游戏中尝试完成的任务。

以下是Ray Wenderlich的书iOS Games by Tutorials Third Edition中的一段代码,用于通过节点和手指的移动水平移动背景,但您可以尝试修改它以满足您的目标。

let backgroundMovePointsPerSec: CGFloat = 200.0

func moveBackground() {
  enumerateChildNodesWithName("background") { node, _ in
    let background = node as SKSpriteNode
    let backgroundVelocity = CGPoint(x: -self.backgroundMovePointsPerSec, y: 0)
    // dt(NSTimeInterval) is a property defined to keep track of the last time Sprite Kit called
    // update() and the delta time since the last update. update() is the Game Loop.
    let amountToMove = backgroundVelocity * CGFloat(self.dt) background.position += amountToMove
  }
}

此代码也使用了来自 Ray Wenderlich 的强大SKUtils Swift+SpriteKit扩展程序。