SKPhysicsbody

Hi,


I have a SKSpriteNode player and a SKSpriteNode sign. I want to drag the player to position above the sign but I can’t because I have given them both physics bodies. I have given them physics bodies because there is a score += 1 when they touch.


How do I adjust the physics codes so that I can drag and position the player to sit above the sign?


Thank you in advance.


Here are my codes:


import SpriteKit

import GameplayKit


struct PhysicsCategory {

static let none : UInt32 = 0

static let player : UInt32 = 0b1

static let sign : UInt32 = 0b10

}


class GameScene: SKScene {


let touchLocation = CGPoint()

let player = SKSpriteNode(imageNamed: "star")

let sign = SKSpriteNode(imageNamed: “Sign")

var playerContactsSign = 0

let scoreLabel = SKLabelNode(fontNamed: "Rockwell-Bold")

override func didMove(to view: SKView) {


scoreLabel.text = "Correct Answers: X"

scoreLabel.fontColor = SKColor.black

scoreLabel.fontSize = 50

scoreLabel.zPosition = 10

scoreLabel.alpha = 0.7

scoreLabel.horizontalAlignmentMode = .right

scoreLabel.verticalAlignmentMode = .top

scoreLabel.position = CGPoint(x: size.width/2 - 300, y: size.height/2 + 650)

self.addChild(scoreLabel)

spawnPlayer()

spawnSign()

physicsWorld.gravity = .zero

physicsWorld.contactDelegate = self

}



func spawnPlayer() {

player.name = "Che"

player.anchorPoint = CGPoint(x: 0.5, y: 0.5) //default

player.position = CGPoint(x: size.width/2 - 500, y: size.height/2 + 300)

player.zPosition = 10

player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width/2)

player.physicsBody?.isDynamic = true

player.physicsBody?.categoryBitMask = PhysicsCategory.player

player.physicsBody?.contactTestBitMask = PhysicsCategory.sign

player.physicsBody?.affectedByGravity = false

self.addChild(player)

player.setScale(0.5)

}



func spawnSign() {

sign.anchorPoint = CGPoint(x: 0.5, y: 0.5) //default

sign.position = CGPoint(x: size.width/2 - 500, y: size.height/2 - 300)

sign.zPosition = 3

sign.physicsBody = SKPhysicsBody(circleOfRadius: sign.size.width/2)

sign.physicsBody?.isDynamic = false

sign.physicsBody?.categoryBitMask = PhysicsCategory.sign

sign.physicsBody?.contactTestBitMask = PhysicsCategory.player

sign.physicsBody?.affectedByGravity = false

self.addChild(sign)

sign.setScale(3.5)

}



override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

for touch in touches {

let touchLocation = touch.location(in: self)

let nodeTouched = atPoint(touchLocation)

if nodeTouched.name == "Che" {

player.position.x = touchLocation.x

player.position.y = touchLocation.y


}

}

}


override func update(_ currentTime: CFTimeInterval) {

//Printing time since last call to update

if lastUpdateTime > 0 {

dt = currentTime - lastUpdateTime

} else {

dt = 0

}

lastUpdateTime = currentTime

print("\(dt*1000) milliseconds since last update")

scoreLabel.text = "Correct Answers: \(playerContactsSign)"

}


func playerTouchesSign(player: SKSpriteNode, sign: SKSpriteNode) {

print("Touch")

playerContactsSign += 1


}

}


extension GameScene: SKPhysicsContactDelegate {



func didBegin(_ contact: SKPhysicsContact) {

// 1

var firstBody: SKPhysicsBody

var secondBody: SKPhysicsBody

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {

firstBody = contact.bodyA

secondBody = contact.bodyB

} else {

firstBody = contact.bodyB

secondBody = contact.bodyA

}

// 2

if ((firstBody.categoryBitMask & PhysicsCategory.player != 0) &&

(secondBody.categoryBitMask & PhysicsCategory.sign != 0)) {

if let player = firstBody.node as? SKSpriteNode,

let sign = secondBody.node as? SKSpriteNode {

playerTouchesSign(player: player, sign: sign)

}

}

}


}

Replies

Correction: 'Above' meaning 'over' the sign.