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

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

问题描述

我正在使用Spritekit和Swift开发一个sidescroller。我不明白如何定义比屏幕更大的可播放区域并将相机放在播放器上。如何做到这一点?

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其位置预先确定。这会导致你的anchorPoint设置出现问题(正在创建world& physicsBody,其左下角从Scene的锚点开始)。

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).

这可以通过以下方法修复:在不使用位置设置的Rect的情况下创建World和Player。 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天全站免登陆