Hello all,
I have some Swift source code (mainly based on the MegaJump tutorial) where I want to object to bounce of eachother (see below extract). I can’t manage to get the player bounce of the background, while I did set the collisionBitMask correctly I think. If I add the player to the foreGroundNode iso to the general scene, they do bounce of eachother. But for various reasons I would like to keep the player in a separate SKNode. Is there something wrong with this piece of code, making that the physicsobjects do not react correctly? Or is it not possible to make 2 different SKNodes collide with eachother?
struct CollisionBitMask {
static let playerCategory:UInt32 = 0x1 << 0
static let foregroundCategory:UInt32 = 0x1 << 1
}
var foregroundNode: SKNode!
var foregroundNode2: SKNode!
var player: SKNode!
class GameScene: SKScene, SKPhysicsContactDelegate {
override init(size: CGSize) {
// Add some gravity
physicsWorld.gravity = CGVector(dx: 0.0, dy: -5.0)
// Set contact delegate
physicsWorld.contactDelegate = self
// Create foreground
var positionForeGroundNode = CGPoint(x: 0, y:0);
foregroundNode = createForegroundNode(position: positionForeGroundNode)
foregroundNode.zPosition = 2
addChild(foregroundNode)
// Create player
player = createPlayer()
player.zPosition = 2
addChild(player)
// Set properties of physicsBody
//player.physicsBody?.isDynamic = true
player.physicsBody?.applyImpulse(CGVector(dx: 0.0, dy: 0.0))
}
func createPlayer() -> SKNode {
let playerNode = SKNode()
playerNode.position = CGPoint(x: self.size.width * 0.5, y: self.size.height * 0.5)
let sprite = SKSpriteNode(imageNamed: "Run1.png")
sprite.setScale(scaleFactorPlayer / 10.0)
playerNode.addChild(sprite)
// 1
playerNode.physicsBody = SKPhysicsBody(circleOfRadius: sprite.size.width / 2)
// 2
playerNode.physicsBody?.isDynamic = true
// 3
playerNode.physicsBody?.allowsRotation = false
// 4
playerNode.physicsBody?.restitution = 1.0
playerNode.physicsBody?.friction = 0.0
playerNode.physicsBody?.angularDamping = 0.0
playerNode.physicsBody?.linearDamping = 0.0
playerNode.physicsBody?.categoryBitMask = CollisionBitMask.playerCategory
playerNode.physicsBody?.collisionBitMask = CollisionBitMask.foregroundCategory
playerNode.physicsBody?.contactTestBitMask = CollisionBitMask.foregroundCategory
return playerNode
}
func createForegroundNode(position : CGPoint) -> SKNode {
let foregroundNode = SKNode()
let xSpacing = 32.0 * scaleFactorForeground
for index in 0...19 {
// 3
let node = SKSpriteNode(imageNamed: "tile")
// 4
node.setScale(scaleFactorForeground / 4.0)
node.anchorPoint = position
node.position = CGPoint(x: xSpacing * CGFloat(index), y: 0)
//5
foregroundNode.addChild(node)
}
foregroundNode.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width, height: self.size.height / 20.0))
foregroundNode.physicsBody?.isDynamic = false
foregroundNode.physicsBody?.categoryBitMask = CollisionBitMask.foregroundCategory
foregroundNode.physicsBody?.collisionBitMask = CollisionBitMask.playerCategory
foregroundNode.physicsBody?.contactTestBitMask = CollisionBitMask.playerCategory
return foregroundNode
}
}