【问题标题】:Moving an SKSpritenode towards another SKSpriteNode (Magnet)将 SKSpritenode 移向另一个 SKSpriteNode(磁铁)
【发布时间】:2016-11-17 23:59:53
【问题描述】:

我的目标:让 SKSpriteNode 移动到另一个 SKSpriteNodes 位置。 (为我的精灵套件游戏创建磁铁)

嗨,我正在尝试让多个具有相同名称的 SKSpriteNode 移动并与我将要称为磁铁的精灵节点碰撞我只是不明白我将在哪里或如何去做,我已经尝试过使用此代码,但我不知道该怎么做:

func update(_ currentTime: TimeInterval) {

   let node = childNode(withName: "specialObstacle")

        let magnetLocation = magnet.position
        let specialObjectLocation = node?.position

        let x = node?.position.x - magnetLocation.x
        let y = node?.position.y - magnetLocation.y

}

注意:我的磁铁位置会发生变化,因为用户将在屏幕上移动磁铁并希望它继续朝着磁铁移动,但我就是不知道该怎么做。

编辑 1

我尝试使用你有旋风的代码,它似乎适用于 x 轴,但它似乎不适用于 y 轴,由于某种原因,它看起来像是试图下降但失败并且只是在一个振动点。我在下面添加了我的代码和一张图片

这里是我正在使用的代码:

 let playerLocation:CGPoint = self.convert(thePlayer.position, from: worldNode)

        let location = playerLocation
        let nodeSpeed:CGFloat = 3.5

        worldNode.enumerateChildNodes(withName: "levelUnit"){
            node, stop in

            let levelUnit:LevelUnit = node as! LevelUnit  //cast as an actual LevelUnit class


            levelUnit.enumerateChildNodes(withName: "specialObstacle"){
                node, stop in

                //Aim
                let dx = location.x - (node.position.x)
                let dy = location.y - (node.position.y)
                let angle = atan2(dy, dx)

                node.zRotation = angle

                //Seek
                let vx = cos(angle) * nodeSpeed
                let vy = sin(angle) * nodeSpeed

                node.position.x += vx
                node.position.y += vy

            }
        }

【问题讨论】:

  • SKFieldNode 有磁场
  • @Knight0fDragon 您能否提供一个有关如何使用 SKFieldNode 的示例,因为这是我第一次听说它并且找不到任何深入的教程来告诉我它们如何工作或如何实施它们
  • 没用过,只知道它有你想要的。从文档开始。developer.apple.com/reference/spritekit/skfieldnode
  • @Knight0fDragon 在不使用 SKFieldNode 的情况下还有其他方法吗?
  • 当然可以,但是您为什么要这样做?你有工具学习如何使用它,猜测,犯错误

标签: swift sprite-kit position skspritenode


【解决方案1】:

像这样?

代码

首先是代码

import SpriteKit

class GameScene: SKScene {

    let magnet = SKFieldNode.linearGravityField(withVector: vector_float3(0, 9.8 * 2, 0))
    let circle = SKShapeNode(circleOfRadius: 30)
    override func didMove(to view: SKView) {
        circle.fillColor = .white
        circle.position.x = 0
        circle.position.y = frame.maxY
        addChild(circle)

        magnet.region = SKRegion(size: self.frame.size)
        magnet.isEnabled = false
        magnet.categoryBitMask = 0x1            
        circle.addChild(magnet)

        self.physicsBody = SKPhysicsBody(edgeLoopFrom: frame)

        for i in 0..<20 {
            let ball = SKShapeNode(circleOfRadius: 30)
            ball.fillColor = .yellow
            ball.physicsBody = SKPhysicsBody(circleOfRadius: 30)
            ball.position.x = CGFloat(i) * 10
            ball.physicsBody!.fieldBitMask = 0x1
            addChild(ball)
        }

        let ball = SKShapeNode(circleOfRadius: 30)
        ball.fillColor = .green
        ball.physicsBody = SKPhysicsBody(circleOfRadius: 30)
        ball.position.y = 40
        ball.physicsBody!.fieldBitMask = 0x2
        addChild(ball)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        circle.fillColor = .red
        magnet.isEnabled = true
    }

    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        circle.fillColor = .white
        magnet.isEnabled = false
    }
}

它是如何工作的?

这里有核心概念

  1. 屏幕顶部有一个SKFieldNode,其categoryBitMask为0x1
  2. 黄色球有一个物理体,带有fieldBitMask = 0x1(这将与 FieldNode 进行交互)
  3. 绿球有一个带有fieldBitMask = 0x2 的物理体(这使它不受SKFieldNode 的影响)

最后,我只是在您触摸/取消触摸屏幕时打开/关闭 SKFieldNode。

哦,在 SKFieldNode 的同一位置有一个红色精灵,但这只是出于 UI 原因。

【讨论】:

  • 这不能按我想要的方式工作,因为它只会在按下时进入屏幕顶部一直在移动磁铁,并且当我将圆圈的位置更改为屏幕中间时,它不会转到圆圈位置,而是一直越过圆圈到屏幕顶部。我希望它像真空一样吸走我在游戏中遇到的所有硬币和宝石,当它进入磁铁的某个半径范围内时。
  • @Astrum 您始终可以将磁铁矢量的 x 和 y 值设为变量。这些变量可以通过使用移动磁体和被磁化的节点的 x 和 y 值来确定。您还必须进行一些计算。免责声明:这纯粹是假设,未经测试
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-22
相关资源
最近更新 更多