I made contact recogniser using physics body, category bit mask and SKPhysics contact. Unfortunately it do not work. Could You help me somehow to find mistake?
This code do not print statement Touched.
Is it because one body is SKSpriteNode and second SKShapeNode? Could we recognise contact between SpriteNode and ShapeNode?
I made all improvements like #giorashc said with SKPhysics body, but in not worked.
import Foundation
import SpriteKit
import GameplayKit
import UIKit
var TouchLevel3BadgeChecker = 0
enum BodyType:UInt32{
case TheCircle = 1
case TheEightBack = 2
}
class TouchLevel3SceneClass: SKScene, SKPhysicsContactDelegate{
var Eight = SKSpriteNode(imageNamed: "8.png")
var EightBack = SKSpriteNode(imageNamed: "8Back.png")
var counter : Int = 0;
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
Eight.position = CGPoint(x: 0, y: -50)
Eight.name = "Eight"
addChild(Eight)
EightBack.position = CGPoint(x: 0, y: -50)
EightBack.name = "EightBack"
EightBack.zPosition = 2
EightBack.physicsBody? = SKPhysicsBody(texture: EightBack.texture!,
size: EightBack.texture!.size())
EightBack.physicsBody?.usesPreciseCollisionDetection = true
EightBack.physicsBody?.isDynamic = true
EightBack.physicsBody?.affectedByGravity = false
EightBack.physicsBody?.categoryBitMask = BodyType.TheEightBack.rawValue
addChild(EightBack)
Circle.position = CGPoint(x: 0, y: -25)
Circle.zPosition = 2;
Circle.fillColor = SKColor.black;
Circle.name = "Circle"
Circle.physicsBody? = SKPhysicsBody(circleOfRadius: 30)
Circle.physicsBody?.usesPreciseCollisionDetection = true
Circle.physicsBody?.isDynamic = true
Circle.physicsBody?.affectedByGravity = false
Circle.physicsBody?.categoryBitMask = BodyType.TheCircle.rawValue
Circle.physicsBody?.collisionBitMask = BodyType.TheEightBack.rawValue
Circle.physicsBody?.contactTestBitMask = BodyType.TheEightBack.rawValue
addChild(Circle)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches{
let location = t.location(in: self);
if atPoint(location).name == "Circle"{
Circle.position.x = location.x
Circle.position.y = location.y
}
}
}
func didBegin(_ contact: SKPhysicsContact) {
if contact.bodyA.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyB.categoryBitMask == BodyType.TheEightBack.rawValue{
print("Touched")
} else if contact.bodyB.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyA.categoryBitMask == BodyType.TheEightBack.rawValue{
print("Touched")
}
}
}
You are not creating the physicsBody property. Since its optional and nil setting its properties won't do anything.
create the physicsBody property with:
EightBack.physicsBody = SKPhysicsBody(...);
Circle.physicsBody = SKPhysicsBody(...);
check the example in this link: https://developer.apple.com/documentation/spritekit/skphysicsbody
Related
I have 2 categories
enum ColliderType:UInt32 {
case bulletCategory = 1
case bossCategory = 2
}
I made my class use the SKPhysicsContactDelegate and created a node
class PlayScene: SKScene, SKPhysicsContactDelegate {
var charBullets :[SKSpriteNode] = [SKSpriteNode]()
var charBulletTexture = SKTexture(imageNamed: "Bullet1.png")
var boss = SKSpriteNode()
var bossTexture = SKTexture(imageNamed: "BossImage.png")
And set the physicsWorld.contactDelegate and created a node in the didMove(to view: SKView) function
override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
boss.name = "boss"
boss = SKSpriteNode(texture: bossTexture)
boss.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 300, height: 300))
boss.physicsBody?.usesPreciseCollisionDetection = true
boss.physicsBody!.isDynamic = true
boss.physicsBody!.affectedByGravity = false
boss.size = CGSize(width: 300, height: 300)
boss.physicsBody!.categoryBitMask = ColliderType.bossCategory.rawValue
boss.physicsBody!.collisionBitMask = 0
boss.physicsBody!.contactTestBitMask = ColliderType.bulletCategory.rawValue
boss.position = CGPoint(x: 0, y: self.size.height/4)
addChild(boss)
}
I created another node in the touchesBegan function
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
charBullet.name = "bullet"
charBullet.physicsBody = SKPhysicsBody(circleOfRadius: charBullet.size.width/64)
charBullet.physicsBody!.isDynamic = true
charBullet.physicsBody!.usesPreciseCollisionDetection = true
charBullet.physicsBody!.affectedByGravity = false
charBullet.physicsBody!.velocity = CGVector.init(dx: 0, dy: 450)
charBullet.physicsBody!.categoryBitMask = ColliderType.bulletCategory.rawValue
charBullet.physicsBody!.collisionBitMask = 0
charBullet.physicsBody!.contactTestBitMask = ColliderType.bossCategory.rawValue
charBullet.position = CGPoint(x: character.position.x, y: character.position.y + 100)
addChild(charBullet)
charBullets.append(charBullet)
}
Then I have a didBegin(_ contact: SKPhysicsContact) but it isn't called when the bullet and boss collide
func didBegin(_ contact: SKPhysicsContact) {
//not printed
print("contact!")
if(charBullets.count > 0) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()
if(contact.bodyA.node?.name == "bullet") {
print("test1")
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
print("test2")
firstBody = contact.bodyB
secondBody = contact.bodyA
print(secondBody)
}
if(firstBody.node?.name == "bullet") {
print("bulletname")
}
if(firstBody.node?.name == "boss") {
print("bossname")
}
if(secondBody.node?.name == "bullet") {
print("bulletname2")
}
if(secondBody.node?.name == "boss") {
print("bossname2")
}
if(firstBody.node?.name == "bullet" && secondBody.node?.name == "boss") {
print("hit the boss!")
charBullets[i].removeFromParent()
bossHealth -= 1
bossHealthLabel.text = "Boss Health \(bossHealth)"
}
}
}
}
Update: I fixed it by changing their physicsBodys' to use the image texture size as opposed to a set value and by setting their names under the creation of the node by using
boss = SKSpriteNode(texture: bossTexture)
boss.name = "boss"
boss.physicsBody = SKPhysicsBody(texture: bossTexture, size: bossTexture.size())
and
let charBullet = SKSpriteNode(texture: charBulletTexture)
charBullet.name = "bullet"
charBullet.physicsBody = SKPhysicsBody(texture: charBulletTexture, size: charBulletTexture.size())
Create enemy
touchesBegan and didBegin contact function
My enemy node is not being removed from the scene every time my sword node touches it. I'm just wondering if anyone could explain to me what I'm doing wrong?
(UPDATE BELOW)
import SpriteKit
import GameplayKit
import AVFoundation
class LevelTwo: SKScene, SKPhysicsContactDelegate{
var levelBg = SKSpriteNode(imageNamed: "level2")
var hero = SKSpriteNode()
var enemy = SKSpriteNode()
var sword = SKSpriteNode()
var health1 = SKSpriteNode(imageNamed: "playerhplv2")
var health2 = SKSpriteNode(imageNamed: "playerhplv2")
var health3 = SKSpriteNode(imageNamed: "playerhplv2")
var musicPath = URL(fileURLWithPath: Bundle.main.path(forResource: "gameMusic", ofType: "mp3")!)
var musicGamePlayer = AVAudioPlayer()
var runMonster = SKAction()
var waitMonster = SKAction()
var sequenceMonster = SKAction()
var repeatMonster = SKAction()
enum CollisionNum: UInt32{
case swordNum = 1
case enemyNum = 2
case playerNum = 4
}
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
///music
do{
musicGamePlayer = try AVAudioPlayer(contentsOf: musicPath)
musicGamePlayer.prepareToPlay()
musicGamePlayer.numberOfLoops = -1
musicGamePlayer.play()
}
catch{
print(error)
}
//bg
levelBg.position = CGPoint(x: 0, y: 0)
levelBg.zPosition = 1
levelBg.size = levelBg.texture!.size()
levelBg.setScale(1.25)
self.addChild(levelBg)
//hero
let playerTexture = SKTexture(imageNamed: "main")
hero = SKSpriteNode(texture: playerTexture)
hero.position = CGPoint(x: 0, y: 0)
hero.zPosition = 2
hero.setScale(0.6)
hero.physicsBody = SKPhysicsBody(texture: playerTexture, size: CGSize(width: hero.size.width, height: hero.size.height))
hero.physicsBody!.categoryBitMask = CollisionNum.playerNum.rawValue
hero.physicsBody!.collisionBitMask = CollisionNum.enemyNum.rawValue //player is allowed to bump into rocks and skulls
hero.physicsBody!.contactTestBitMask = CollisionNum.enemyNum.rawValue // same as collisions
hero.physicsBody!.isDynamic = false
self.addChild(hero)
//health1
health1.position = CGPoint(x: 130, y: 150)
health1.zPosition = 3
health1.setScale(0.75)
self.addChild(health1)
//health2
health2.position = CGPoint(x: 230, y: 150)
health2.zPosition = 3
health2.setScale(0.75)
self.addChild(health2)
//health3
health3.position = CGPoint(x: 320, y: 150)
health3.zPosition = 3
health3.setScale(0.75)
self.addChild(health3)
runMonster = SKAction.run(addMonster)
waitMonster = SKAction.wait(forDuration: 0.3)
sequenceMonster = SKAction.sequence([runMonster,waitMonster])
repeatMonster = SKAction.repeatForever(sequenceMonster)
run(repeatMonster)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let locale = touch.location(in: self)
hero.position.x = locale.x
hero.position.y = locale.y
}
}
func addMonster(){
//random position based off the bg size
let monsterHigherX = Int(levelBg.size.width)
let monsterHigherY = Int(levelBg.size.height)
let monsterLowerX = monsterHigherX * -1
let monsterLowerY = monsterHigherY * -1
let randomLocaleX = Int(arc4random_uniform(UInt32(monsterHigherX - monsterLowerX))) + monsterLowerX
let randomLocaleY = Int(arc4random_uniform(UInt32(monsterHigherY - monsterLowerY))) + monsterLowerY
let movementEnemy = SKAction.moveBy(x: -5, y: -5, duration: 0.2)
let movementForever = SKAction.repeatForever(movementEnemy)
let enemyTexture = SKTexture(imageNamed: "boss0")
enemy = SKSpriteNode(texture: enemyTexture)
enemy.zPosition = 2
enemy.setScale(0.5)
enemy.position = CGPoint(x: randomLocaleX, y: randomLocaleY)
enemy.physicsBody = SKPhysicsBody(texture: enemyTexture, size: CGSize(width: enemy.size.width, height: enemy.size.height))
enemy.physicsBody!.isDynamic = true
enemy.physicsBody!.affectedByGravity = false
enemy.physicsBody!.categoryBitMask = CollisionNum.enemyNum.rawValue
enemy.physicsBody!.collisionBitMask = CollisionNum.swordNum.rawValue
enemy.physicsBody!.contactTestBitMask = CollisionNum.swordNum.rawValue
enemy.run(movementForever)
self.addChild(enemy)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let swordTexture = SKTexture(imageNamed: "blade-0")
sword = SKSpriteNode(texture: swordTexture)
sword.setScale(0.50)
sword.zPosition = 2
sword.position = hero.position
sword.physicsBody = SKPhysicsBody(texture: swordTexture, size: CGSize(width: sword.size.width, height: sword.size.height))
sword.physicsBody!.velocity = CGVector(dx: 1200, dy:0)
sword.physicsBody!.isDynamic = true
sword.physicsBody!.affectedByGravity = true
sword.physicsBody!.usesPreciseCollisionDetection = true
sword.physicsBody!.categoryBitMask = CollisionNum.swordNum.rawValue
sword.physicsBody!.collisionBitMask = CollisionNum.enemyNum.rawValue
sword.physicsBody!.contactTestBitMask = CollisionNum.enemyNum.rawValue
self.addChild(sword)
}
func didBegin(_ contact: SKPhysicsContact) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == CollisionNum.swordNum.rawValue | CollisionNum.enemyNum.rawValue {
enemy.removeFromParent()
}
}
override func update(_ currentTime: TimeInterval) {
}
}
(Ive attached my whole level2 class) Thank you so much for the suggestion; however, when I tried implementing this I still run into the same problem (im running this on the iphone simulator) Im wondering whether the error is with my enum or my implementation of my physics with my nodes
You do not want to remove "enemy" because "enemy" is always the last monster you added. You need to check which contactBody is the enemy so you can remove it. You can do that by guaranteeing which node you want to associate as A, and which you want to associate as B by looking at the categoryBitMask value:
func didBegin(_ contact: SKPhysicsContact) {
//This guarantees the lower categoryBitMask (Providing you are only using one) is in A
let bodyA = contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask ? contact.bodyA : contact.bodyB
let bodyB = contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask ? contact.bodyB : contact.bodyA
if bodyA.categoryBitMask == CollisionNum.swordNum.rawValue && bodyB.categoryBitMask == CollisionNum.enemyNum.rawValue {
bodyB.node.removeFromParent()
}
}
Of course this will lead to problems with multiple collisions, so instead you may want to do:
var removeNodes = SKNode()
func didBegin(_ contact: SKPhysicsContact) {
//This guarantees the lower categoryBitMask (Providing you are only using one) is in A
let bodyA = contact.bodyA.categoryBitMask <= contact.bodyB.categoryBitMask ? contact.bodyA : contact.bodyB
let bodyB = contact.bodyA.categoryBitMask > contact.bodyB.categoryBitMask ? contact.bodyB : contact.bodyA
if bodyA.categoryBitMask == CollisionNum.swordNum.rawValue && bodyB.categoryBitMask == CollisionNum.enemyNum.rawValue {
bodyB.node.moveToParent(removeNodes)
}
}
func didFinishUpdate(){
removeNodes.removeAllChildren()
}
Try this:
func didBegin(_ contact: SKPhysicsContact) {
let collision: UInt32 = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
if collision == CollisionNum.swordNum.rawValue | CollisionNum.enemyNum.rawValue {
enemy.removeFromParent()
}
}
You were only testing if bodyA is equal to the enemy, however, bodyA may be equal to the sword instead.
This is a game I have been working on. In summary there is a moving block named enemy and I want it to collide with an invisible static block called invisibleGround2. I have it printing hit when they supposedly collide but they are not colliding. Ive read every swift collision documentation by apply and others out there and I dont know whats wrong. Any help would be much appreciated!
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var orb = SKSpriteNode(imageNamed: "orb")
var scrollingG:scrollingGround?
var invisibleGround = SKSpriteNode(imageNamed: "invisible")
var invisibleGround2 = SKSpriteNode(imageNamed: "invisible")
let enemies = [SKSpriteNode(imageNamed: "blueE.png"), SKSpriteNode(imageNamed: "redE.png")]
let enemyCategory : UInt32 = 1
let jumperCategory : UInt32 = 1
let rotateDuration = 2
let enemyMoveSpeed = 5
let groundScrollingSpeed = 5
func createOrb(){
let orbConst = frame.size.width/2
let xConstraint = SKConstraint.positionX(SKRange(constantValue: orbConst))
orb.position = CGPoint(x: frame.size.width/2, y: 480)
orb.physicsBody = SKPhysicsBody(texture: orb.texture!, size: orb.texture!.size())
orb.constraints = [xConstraint]
self.addChild(orb)
}
func createScrollingGround () {
scrollingG = scrollingGround.scrollingNodeWithImage(imageName: "ground", containerWidth: self.size.width)
scrollingG?.scrollingSpeed = CGFloat(groundScrollingSpeed)
scrollingG?.anchorPoint = .zero
self.addChild(scrollingG!)
}
func createGround(){
invisibleGround2.size.width = 1
invisibleGround2.size.height = 1
invisibleGround2.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width:1, height: 100))
invisibleGround2.physicsBody?.isDynamic = false
invisibleGround2.position = CGPoint(x: 530, y: 191)
invisibleGround2.physicsBody?.categoryBitMask = jumperCategory
invisibleGround2.physicsBody?.collisionBitMask = enemyCategory
invisibleGround2.physicsBody?.contactTestBitMask = enemyCategory
invisibleGround2.name = "jumper"
invisibleGround.position = CGPoint(x: 0, y: 190)
invisibleGround.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width * 3, height: 10))
invisibleGround.physicsBody?.isDynamic = false
self.addChild(invisibleGround2)
self.addChild(invisibleGround)
}
func getRandomEnemy(fromArray array:[SKSpriteNode])->SKSpriteNode{
return array[Int(arc4random_uniform(UInt32(array.count)))]
}
func spawnEnemy() {
if let enemy = getRandomEnemy(fromArray: enemies).copy() as? SKSpriteNode {
enemy.position = CGPoint(x: frame.size.width + frame.size.width/3, y: 440)
enemy.physicsBody = SKPhysicsBody(texture: enemy.texture!, size: enemy.texture!.size())
if enemy.size.width < 95 {
enemy.physicsBody?.categoryBitMask = enemyCategory
enemy.physicsBody?.collisionBitMask = jumperCategory
enemy.physicsBody?.contactTestBitMask = jumperCategory
}
enemy.name = "enemy"
self.addChild(enemy)
let moveLeft = SKAction.moveBy(x: -1500, y: 0, duration: TimeInterval(enemyMoveSpeed))
enemy.run(moveLeft)
}
}
func addEnemies () {
self.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
self.spawnEnemy()
}, SKAction.wait(forDuration: 4)])))
}
func jump() {
orb.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 335))
}
func rotate() {
let rotate = SKAction.rotate(byAngle: CGFloat(M_PI * -2.55), duration: TimeInterval(rotateDuration))
let repeatAction = SKAction.repeatForever(rotate)
orb.run(repeatAction)
}
override func didMove(to view: SKView) {
createScrollingGround()
createOrb()
createGround()
rotate()
addEnemies()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
jump()
}
override func update(_ currentTime: TimeInterval) {
if self.scrollingG != nil {
scrollingG?.update(currentTime: currentTime)
func didBegin(_ contact: SKPhysicsContact) {
let bodyA = contact.bodyA.categoryBitMask
let bodyB = contact.bodyB.categoryBitMask
if bodyA == jumperCategory && bodyB == enemyCategory {
print("hit")
} else if bodyA == enemyCategory && bodyB == jumperCategory {
print("hit 2")
}
}
}
}
}
Not an swer, but some things to check:
Have you set the scene’s physicsworld delegate property set to
self?
Move didBegin outside of update
Is the 'enemy' sprite's width < 95?
Add a print("Contact detected") as the first line of your relocated didBegin so you at least know that some contact has been detected.
See my answer here https://stackoverflow.com/a/43605825/1430420 for a simple SK collision demo which might help - I think it needs updates to Swift 4 which I'll try and do.
I have SpritNodes that are in my scene and I want a method to be called when I touch it. I have isUserInteractionEnabled set to true for my node, but touchesBegan still does not get called when I touch the nodes. (Note: I am using Swift 3.0)
Code:
import SpriteKit
class MainScene: SKScene, SKPhysicsContactDelegate {
var didStart = false
var background = SKSpriteNode(imageNamed: "background")
var backDrop = SKShapeNode()
var emailNodes: [SKSpriteNode] = []
let emailCatagory: UInt32 = 0x1 << 0
let dropCatagory: UInt32 = 0x1 << 1
override func sceneDidLoad() {
startCountDown()
}
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self
background.position = CGPoint(x: frame.midX, y:frame.midY)
}
public func startCountDown(){
var times = 4
let countdownTimer = SKLabelNode()
countdownTimer.text = "3"
countdownTimer.position = CGPoint(x: frame.midX, y: frame.midY)
countdownTimer.fontSize = 120.0
countdownTimer.fontName = "Lao MN"
countdownTimer.fontColor = UIColor.black()
backDrop = SKShapeNode()
backDrop = SKShapeNode(rectOf: CGSize(width: frame.width, height: 100))
backDrop.position = CGPoint(x: frame.midX, y: 10)
backDrop.physicsBody = SKPhysicsBody(rectangleOf: backDrop.frame.size)
//backDrop.size = CGSize(width: 1000, height: 2)
backDrop.physicsBody?.affectedByGravity = false
backDrop.physicsBody?.usesPreciseCollisionDetection = true
backDrop.name = "backDrop"
backDrop.physicsBody?.collisionBitMask = 0
backDrop.physicsBody?.categoryBitMask = dropCatagory
addChild(countdownTimer)
addChild(backDrop)
//addChild(background)
Timer.every(1.2.seconds) { (timer: Timer) in
if(times<=0){
timer.invalidate()
countdownTimer.removeFromParent()
self.didStart = true
self.startDropping()
}else{
print("\(times)")
times = times - 1
countdownTimer.text = "\(times)"
}
}
}
func startDropping(){
Timer.every(1.2.seconds) { (timer: Timer) in
let which = Int(arc4random_uniform(2) + 1)
let ee = self.getEmailNode(type: which)
self.addChild(ee)
ee.physicsBody?.applyImpulse(CGVector(dx: 0.0, dy: -5.0))
}
}
func getEmailNode(type: Int) -> SKSpriteNode{
var email = SKSpriteNode()
if(type == 1){
email = SKSpriteNode(imageNamed: "normal_email")
email.name = "normal_email"
}
if(type == 2){
email = SKSpriteNode(imageNamed: "classified_email")
email.name = "classified_email"
}
email.setScale(3)
email.position = CGPoint(x: getRandomColumn(), y: frame.height)
email.physicsBody = SKPhysicsBody(rectangleOf: email.frame.size)
email.physicsBody?.usesPreciseCollisionDetection = true
email.physicsBody?.categoryBitMask = emailCatagory
email.isUserInteractionEnabled = true
email.physicsBody?.affectedByGravity = false
email.physicsBody?.collisionBitMask = 0
email.physicsBody?.contactTestBitMask = emailCatagory | dropCatagory
emailNodes.append(email)
return email
}
func getRandomColumn() -> CGFloat{
let which = Int(arc4random_uniform(3) + 1)
let gg = frame.size.width/3
switch(which){
case 1:
return gg / 2
case 2:
return frame.midX
case 3:
return (gg * 3) - gg / 2
default:
return (gg * 3) + gg / 2
}
}
func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == dropCatagory) &&
(contact.bodyB.categoryBitMask == emailCatagory) {
let node = contact.bodyB.node as! SKSpriteNode
node.removeFromParent()
while emailNodes.contains(node) {
if let itemToRemoveIndex = emailNodes.index(of: node) {
emailNodes.remove(at: itemToRemoveIndex)
}
}
}
}
func doesContainNode(sk: SKSpriteNode) -> Bool {
for it in emailNodes{
if(it == sk){
return true
}
}
return false
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
enumerateChildNodes(withName: "//*", using:
{ (node, stop) -> Void in
print("\(node.name)")
print("\(node)")
if((node.name?.contains("email")) != nil){
print("Touched!")
}
})
}
}
You should try the following in order to be able to get the user's position and know when the sprite has been touched. Add the following inside the touchesEnabled function.
for touch in touches {
let userTouch = touch.locationInNode(self)
}
And then check if the sprite was touched by using:
node.containsPoint(userTouch)
See if that works. The way you have your code setup, you might need to nest the above function right after checking if it's nil. As for the userInteractionEnabled, I don't use it at all when using the above code.
You do not want userInteractionEnabled set on any of your nodes, you want that only on the scene. Use userInteractionEnabled only when you are subclassing your node, this way you can use touchesBegan inside your subclassed file. What is happening is your touch is going into your node and being absorbed, which does nothing, and is being ignored by the scene since the node absorbed it.
Edit: Sorry #MarkBrownsword I did not see your comment, if you post it as an answer, I will upvote and delete my answer.
I found a solution. I removed isUserInteractionEnabled and touchesBegan was still not being called. So I went through each of the properties of the "email" node and for some reason the following properties made it where touchesBegan would not be called.
email.physicsBody?.affectedByGravity = false
email.physicsBody?.collisionBitMask = 0
So I removed those and now touchesBegan is being properly called.
When I put a println() in my didBeginContact, nothing prints. Therefore, I assume that my didBeginContact doesn't work. Why? I also have an enum (Collider Type) in which rawValues of Cup and Star and listed.
This is my GameScene file.
import Foundation
import SpriteKit
class PlayScene: SKScene, SKPhysicsContactDelegate {
let cup = SKSpriteNode(imageNamed: "cup")
override func didMoveToView(view: SKView) {
/* Setup your scene here */
self.physicsWorld.contactDelegate = self
self.backgroundColor = UIColor(red: 192, green: 192, blue: 192, alpha: 1)
cup.setScale(0.5)
cup.position = CGPointMake(self.size.width + cup.size.width / 2, self.size.height/2)
self.addChild(cup)
let actualDuration = 2.0
let actualY = size.height / 2
let actionMove = SKAction.moveByX(-550, y: 0, duration: actualDuration)
let reverseActionMove = actionMove.reversedAction()
let sequence = SKAction.sequence([actionMove, reverseActionMove])
let endlessAction = SKAction.repeatActionForever(sequence)
self.cup.physicsBody = SKPhysicsBody(rectangleOfSize: self.cup.size)
self.cup.physicsBody?.dynamic = false
self.cup.physicsBody?.categoryBitMask = ColliderType.Cup.rawValue
self.cup.physicsBody?.collisionBitMask = ColliderType.Star.rawValue
//Code to keep stars in screen
//let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
//borderBody.friction = 0
//self.physicsBody = borderBody
cup.runAction(endlessAction)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
for touch: AnyObject in touches {
let star = StarNode.star(touch.locationInNode(self))
star.setScale(0.3)
self.addChild(star)
}
}
func didBeginContact(contact: SKPhysicsContact) {
println("test")
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case ColliderType.Star.rawValue | ColliderType.Cup.rawValue:
println("star with da cup")
default:
return
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
This is my StarNode file.
import Foundation
import UIKit
import SpriteKit
class StarNode: SKSpriteNode, SKPhysicsContactDelegate {
class func star(location: CGPoint) -> StarNode {
let sprite = StarNode(imageNamed:"star")
sprite.xScale = 0.075
sprite.yScale = 0.075
sprite.position = location
sprite.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "star"), size: sprite.size)
sprite.physicsBody?.dynamic = true
sprite.physicsBody?.categoryBitMask = ColliderType.Star.rawValue
sprite.physicsBody?.collisionBitMask = ColliderType.Cup.rawValue
if let physics = sprite.physicsBody {
physics.affectedByGravity = true
physics.friction = 0.12
physics.allowsRotation = true
physics.dynamic = true
physics.linearDamping = 0.75
physics.angularDamping = 0.75
sprite.name = "star"
}
return sprite
}
}
You need to set the contactTestBitMask to your SKSpriteNodes. The collisionBitMask says only which categories of bodies can collide with the node but doesn't call the didBeginContact method.
self.cup.physicsBody?.contactTestBitMask = ColliderType.Star.rawValue
//Star
sprite.physicsBody?.contactTestBitMask = ColliderType.Cup.rawValue
The contactTestBitMask documentation says it:
A mask that defines which categories of bodies cause intersection notifications with this physics body.