在玩家节点上定义地图边界和中心? [英] Define map bounds and center on player node?

查看:13
本文介绍了在玩家节点上定义地图边界和中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Spritekit 和 Swift 开发横向卷轴.我不明白如何定义一个比屏幕更大的可玩区域并将相机放在播放器的中心.如何做到这一点?

I'm working on a sidescroller with Spritekit and Swift. I don't understand how to define a playable area bigger than the screen and center the camera on the player. How can this be done?

这是我当前的代码,我试图创建一个世界节点",我可以移动它来模拟相机,但是它与它的形状不相关,我无法让玩家进入其中.

This is my current code, I tried to create a "world node" which I could move around to simulate the camera, however it's somehow disassociated from it's shape and I haven't been able to get the player inside it.

override func didMoveToView(view: SKView) {
    self.anchorPoint = CGPointMake(0.5, 0.5)
    self.physicsWorld.contactDelegate = self
    self.size = CGSizeMake(view.bounds.size.width, view.bounds.size.height)

    // Add world
    let r : CGRect = CGRectMake(0, 0, 500, 500)
    world = SKShapeNode(rectOfSize: r.size)
    world.physicsBody = SKPhysicsBody(edgeLoopFromRect: r)
    world.strokeColor = SKColor.blackColor()
    world.position = CGPointMake(0, 0)
    self.addChild(world)

    // Add player
    player = SKShapeNode(rectOfSize: CGSize(width: 50, height: 50))
    player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 50))
    player.fillColor = SKColor.blackColor()
    player.position = CGPointMake(0, 0)
    world.addChild(player)

    // Accelerometer updates
    motionManager.startAccelerometerUpdates()
}

推荐答案

Apple 文档中的示例在 高级场景处理部分.Apple 建议将世界"SKNode 作为场景的子节点,将相机"SKNode 作为世界的子节点.

The example in Apple's documentation is in the Advanced Scene Processing section. Apple suggests making a "World" SKNode as a child of the Scene, and a "Camera" SKNode as a child of the world.

他们建议在 didSimulatePhysics 步骤中不断移动世界,使其以相机为中心.如果您愿意,此方法允许您在相机本身上执行操作或模拟物理.如果在此步骤之前将相机居中,您将无法使用物理来影响相机节点.

They suggest constantly moving the world so that it centers on the Camera during the didSimulatePhysics step. This method allows you to perform actions or simulate physics on the Camera itself, if you so choose. If you center the camera prior to this step, you won't be able to use physics to affect the Camera Node.

如果您只想左右滚动,只需将移动限制在 X 轴上即可.

If you specifically only want left and right scrolling, simply restrict the movement to the X-axis.

当前的问题是因为您从 Rect 中创建了世界和physicsBody,该 Rect 的位置已预先确定.这会导致您的 anchorPoint 设置出现问题(正在创建世界和物理实体,它们的左下角从场景的锚点开始).

The current problem is because of your creation of the world and the physicsBody from a Rect that has its position predetermined. This is causing trouble with your anchorPoint setting (the world & physicsBody are being created with their lower left corners starting at the Scene's anchor point).

这可以通过创建 World 和 Player 来解决,而不使用设置了位置的 Rect.SKShapeNode 的 shapeNodeWithRectOfSize 有效,SKSpriteNode 的 spriteNodeWithColor:size: PhysicsBody 有点棘手,应该使用 bodyWithEdgeLoopF​​romPath: world.path

This can be fixed by creating the World and Player without using a Rect with position set. SKShapeNode's shapeNodeWithRectOfSize works, as does SKSpriteNode's spriteNodeWithColor:size: PhysicsBody is a bit trickier, and should likely use bodyWithEdgeLoopFromPath: world.path

对于未来有兴趣创建带有始终专注于播放器的相机的横向卷轴的人来说,这可能是最简单的工作方式:

For future persons interested in creating a side-scroller with a camera always focused on the player, this is probably the simplest way to get one to work:

var player = SKShapeNode()
var world = SKShapeNode()

override func didMoveToView(view: SKView) {
    self.anchorPoint = CGPointMake(0.5, 0.5)
    self.size = CGSizeMake(view.bounds.size.width, view.bounds.size.height)

    // Add world
    world = SKShapeNode(rectOfSize: CGSizeMake(300, 300))
    world.physicsBody = SKPhysicsBody(edgeLoopFromPath: world.path)
    world.fillColor = SKColor.blackColor()
    self.addChild(world)

    // Add player
    player = SKShapeNode(rectOfSize: CGSize(width: 50, height: 50))
    player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize(width: 50, height: 50))
    player.fillColor = SKColor.blackColor()
    world.addChild(player)
}

override func update(currentTime: CFTimeInterval) {
    world.position.x = -player.position.x
    world.position.y = -player.position.y
}

这篇关于在玩家节点上定义地图边界和中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆