A better solution to drag a SKPhysicsBody around in Spritekit

Introduction

When asking to drag a SKPhysicsBody around in Spritekit, we may come up with an idea immediately that just set the touch position as the position of the node. However, this simple method has a couple of problems actually.[1]

Problems

  • While we are dragging the dynmaic body directly, gravity is still generating effect on it and is going to be dragging the body down. This will have the effect of making the body's position flicker noticeably as it's move around.

  • If you set the body's position directly, it is a possibility that this body will move through wall or through other objects骂维,which may not be what you want.

Better Solution

There is a better solution to drag a SKPhysicsBody around in Spritekit; that is adding a pin joint at the point player touched and jointing with the body; then moving the joint around.
Here is example:

override func touchesBegan(touches: Set<NSObject>,
    withEvent event: UIEvent) {

    // We only care about one touch at a time
    if let touch = touches.first as? UITouch {
    // Work out what node got touched
        let touchPosition = touch.locationInNode(self)
        let touchedNode = self.nodeAtPoint(touchPosition)

        // Make sure that we're touching something that _can_ be dragged
        if touchedNode == dragNode || touchedNode.physicsBody == nil {
            return
        }

        // Create the invisible drag node, with a small static body
        let newDragNode = SKNode()
        newDragNode.position = touchPosition
        newDragNode.physicsBody =
            SKPhysicsBody(rectangleOfSize:CGSize(width: 10,
                                                 height: 10))
        newDragNode.physicsBody?.dynamic = false

        self.addChild(newDragNode)

        // Link this new node to the object that got touched
        let newDragJoint = SKPhysicsJointPin.jointWithBodyA(
            touchedNode.physicsBody,
            bodyB:newDragNode.physicsBody,
            anchor:touchPosition)

        self.physicsWorld.addJoint(newDragJoint)

        // Store the reference to the joint and the node
        self.dragNode = newDragNode
        self.dragJoint = newDragJoint

    }
}

override func touchesMoved(touches: Set<NSObject>,
    withEvent event: UIEvent) {

    if let touch = touches.first as? UITouch {
        // When the touch moves, move the static drag node.
        // The joint will drag node.
        // The joint will drag the connected
        // object with it.
        let touchPosition = touch.locationInNode(self)

        dragNode?.position = touchPosition
    }
}

override func touchesEnded(touches: Set<NSObject>,
    withEvent event: UIEvent) {

    stopDragging()
}

override func touchesCancelled(touches: Set<NSObject>,
    withEvent event: UIEvent) {

    stopDragging()
}

func stopDragging() {
    // Remove the joint and the drag node.
    self.physicsWorld.removeJoint(dragJoint!)
    dragJoint = nil

    dragNode?.removeFromParent()
    dragNode = nil
}

  1. this article is an summary of a chapter of iOS Swift Game Development Cookbook ?

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吨掌,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子劳淆,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,496評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件端仰,死亡現(xiàn)場離奇詭異,居然都是意外死亡田藐,警方通過查閱死者的電腦和手機(jī)荔烧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,187評論 3 385
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來汽久,“玉大人鹤竭,你說我怎么就攤上這事【按迹” “怎么了诺擅?”我有些...
    開封第一講書人閱讀 157,091評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長啡直。 經(jīng)常有香客問我烁涌,道長,這世上最難降的妖魔是什么酒觅? 我笑而不...
    開封第一講書人閱讀 56,458評論 1 283
  • 正文 為了忘掉前任撮执,我火速辦了婚禮,結(jié)果婚禮上舷丹,老公的妹妹穿的比我還像新娘抒钱。我一直安慰自己,他們只是感情好颜凯,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,542評論 6 385
  • 文/花漫 我一把揭開白布谋币。 她就那樣靜靜地躺著,像睡著了一般症概。 火紅的嫁衣襯著肌膚如雪蕾额。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,802評論 1 290
  • 那天彼城,我揣著相機(jī)與錄音诅蝶,去河邊找鬼退个。 笑死,一個胖子當(dāng)著我的面吹牛调炬,可吹牛的內(nèi)容都是我干的语盈。 我是一名探鬼主播,決...
    沈念sama閱讀 38,945評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼缰泡,長吁一口氣:“原來是場噩夢啊……” “哼刀荒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起棘钞,我...
    開封第一講書人閱讀 37,709評論 0 266
  • 序言:老撾萬榮一對情侶失蹤缠借,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后武翎,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體烈炭,經(jīng)...
    沈念sama閱讀 44,158評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,502評論 2 327
  • 正文 我和宋清朗相戀三年宝恶,在試婚紗的時候發(fā)現(xiàn)自己被綠了符隙。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,637評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡垫毙,死狀恐怖霹疫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情综芥,我是刑警寧澤丽蝎,帶...
    沈念sama閱讀 34,300評論 4 329
  • 正文 年R本政府宣布,位于F島的核電站膀藐,受9級特大地震影響屠阻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜额各,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,911評論 3 313
  • 文/蒙蒙 一国觉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧虾啦,春花似錦麻诀、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,744評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至硬毕,卻和暖如春呻引,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背昭殉。 一陣腳步聲響...
    開封第一講書人閱讀 31,982評論 1 266
  • 我被黑心中介騙來泰國打工苞七, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留藐守,地道東北人挪丢。 一個月前我還...
    沈念sama閱讀 46,344評論 2 360
  • 正文 我出身青樓蹂风,卻偏偏與公主長得像,于是被迫代替她去往敵國和親乾蓬。 傳聞我的和親對象是個殘疾皇子惠啄,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,500評論 2 348

推薦閱讀更多精彩內(nèi)容