0
votes

I have a problem in my code.

I'm creating a Game and I need to have a sprite that can appear multiple times at the same time, to do so I created a class so I can do addChild(obstacle) multiple times and it will spawn one SKSpriteNode exactly similar to another.

My problem is that I want to check collision between my player and the obstacle but because it's from the same SKSpriteNode the computer can't know of which obstacle I'm talking about.

Here's how I created the player and the obstacle:

import SpriteKit

class Obstacle: SKSpriteNode {

    init() {
        let obstacleTexture = SKTexture(imageNamed: "obstacle")
        super.init(texture: obstacleTexture, color: UIColor.clearColor(), size: CGSize(width: 80, height: 80))
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

class GameScene: SKScene {

    var player:SKSpriteNode!

    override func didMoveToView(view: SKView) {

        //player setup
        let playerTexture = SKTexture(imageNamed: "player")
        player = SKSpriteNode(texture: playerTexture)
        player.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.2)

    }

    //how I spawn an obstacle
    func spawnObstacle() {

        let obstacle = Obstacle()

        //obstacle position setup
        obstacle.position.x = CGFloat(arc4random()) % self.frame.size.width
        obstacle.position.y = self.frame.size.height + 200

        //random spin action setup
        var rotateObstacle = SKAction.rotateByAngle(CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))

        if random() % 2 == 0 {
            rotateObstacle = SKAction.rotateByAngle(CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
        }else{
            rotateObstacle = SKAction.rotateByAngle(-CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
        }

        let rotateObstacleForever = SKAction.repeatActionForever(rotateObstacle)

        //random move action setup
        let moveObstacle = SKAction.moveTo(CGPointMake(CGFloat(arc4random()) % self.frame.size.width, -200), duration: Double((drand48() + 1) * 1.5))

        //running the actions
        obstacle.runAction(rotateObstacleForever)
        obstacle.runAction(moveObstacle)

        addChild(obstacle)

    }

}

}

How to detect when the player collide with any obstacle?

1
You have not specified any physicsBody yet, you need to change set, you should basically read up on how the physics simulation in SpriteKit works. - luk2302
I tried to setup a physicsBody but it didn't work. the player just pass through the obstacle and nothing happen - BenTrd
Read a tutorial, it is not as simple as just setting some physics body. - luk2302
I read like 5 tutorials and copied the code from it but nothing happen. I didn't saw any other efficient ways to spawn the same sprite multiple times. - BenTrd
A fundamental concept to understand is the difference between collision and contacts in spriteKit. A collision occurs when 2 physicsBodies hit and bounce off each other. Whether or not 2 bodies collide is controlled by the categoryBitMask, collisionBitMask and dynamic properties in their physicsBody. Collisions are handled by the game engine; nothing in your code is called to handle a collision. - Steve Ives

1 Answers

0
votes

To detect collisions you could use the SpriteKit physics.

Here you have 3 elements involved to hypothetical collisions:

  • The boundaries (or the field where live your player and where you have your obstacles)
  • The player
  • The obstacles

Advice

Set this parameter to your debug phases to see physics objects boundaries :

skView.showsPhysics = true

An example of code (warning:- This code would be only a point to start to realize your physics, I don't know the rest of your project so your job will be to correct as you believe it's better for your objects):

enum CollisionTypes: UInt32 {
    case Field = 1
    case Player = 2
    case Obstacle = 4
}

class GameScene: SKScene, SKPhysicsContactDelegate {
    override func didMoveToView(view: SKView) {
        self.physicsWorld.gravity = CGVectorMake(0, 0) // set your gravity value
        self.physicsWorld.contactDelegate = self
        let fieldBody = SKPhysicsBody.init(edgeLoopFromRect: self.frame)
        self.physicsBody = fieldBody
        self.physicsBody!.affectedByGravity = false
        self.physicsBody!.usesPreciseCollisionDetection = true
        self.physicsBody!.dynamic = true
        self.physicsBody!.mass = 0.8
        self.physicsBody!.friction = 0
        self.physicsBody!.linearDamping = 0
        self.physicsBody!.angularDamping = 0
        self.physicsBody!.restitution = 0
        self.physicsBody!.categoryBitMask = CollisionTypes.Field.rawValue
        self.physicsBody!.contactTestBitMask = CollisionTypes.Player.rawValue

        // Prepare the player 
        player.physicsBody = SKPhysicsBody(circleOfRadius: player.frame.width/2)
        player.physicsBody!.affectedByGravity = false
        player.physicsBody!.restitution = 0.0
        player.physicsBody!.linearDamping = 0
        player.physicsBody!.friction = 0.3
        player.physicsBody!.dynamic = true
        player.physicsBody!.mass = 0.2
        player.physicsBody!.allowsRotation = false
        player.physicsBody!.categoryBitMask = CollisionTypes.Player.rawValue
        player.physicsBody!.contactTestBitMask = CollisionTypes.Field.rawValue | CollisionTypes.Obstacles.rawValue
        player.physicsBody!.collisionBitMask = CollisionTypes.Field.rawValue | CollisionTypes.Obstacles.rawValue

        //Prepare the obstacles (you must do it in your obstacle class)
        obstacle.physicsBody = SKPhysicsBody(circleOfRadius: obstacle.frame.width/2)
        obstacle.physicsBody!.affectedByGravity = false
        obstacle.physicsBody!.restitution = 0.0
        obstacle.physicsBody!.linearDamping = 0
        obstacle.physicsBody!.friction = 0.3
        obstacle.physicsBody!.dynamic = true
        obstacle.physicsBody!.mass = 0.8
        obstacle.physicsBody!.allowsRotation = true
        obstacle.physicsBody!.categoryBitMask = CollisionTypes.Obstacle.rawValue
        obstacle.physicsBody!.contactTestBitMask = CollisionTypes.Player.rawValue
        obstacle.physicsBody!.collisionBitMask = CollisionTypes.Player.rawValue
    }
    func didBeginContact(contact: SKPhysicsContact) {
       if (contact.bodyA.categoryBitMask == CollisionTypes.Player.rawValue &&
        contact.bodyB.categoryBitMask == CollisionTypes.Obstacle.rawValue) {
        print("contact between Player and Obstacle")
       }
       if (contact.bodyA.categoryBitMask == CollisionTypes.Player.rawValue &&
        contact.bodyB.categoryBitMask == CollisionTypes.Field.rawValue) {
        print("contact between Player and Field")
       }
    }
 }