0

Basically the game consists of a basket that the player moves across the screen, the aim of the game is for the player to catch balls falling from the top of the screen. I am currently trying to add collision detection between the balls and the basket, but am facing difficulties namely, implementing this collision detection. I am new to swift, sprite kit and app development, so please help. Any help would be appreciated. Another problem I am facing is that all the balls are falling in the centre of the screen. A line of code is supposed to execute when, the ball hits the basket and following that the ball should disappear, please help as I am new to Spritekit.

import SpriteKit

class GameScene: SKScene {

var basket = SKSpriteNode()

let actionMoveRight = SKAction.moveByX(50, y: 0, duration: 0.2)
let actionMoveLeft = SKAction.moveByX(-50, y: 0, duration: 0.2)
//let physicsBody = SKPhysicsBody(texture: , size: 3500)

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    self.physicsWorld.gravity = CGVectorMake(0.0, -0.5)
    self.backgroundColor = SKColor.whiteColor()
    basket = SKSpriteNode(imageNamed: "basket")
    basket.setScale(0.5)
    basket.position = CGPointMake(self.size.width/2, self.size.height/8)
    basket.size.height = 50
    basket.size.width = 75
    self.addChild(basket)

    let updateAction = SKAction.runBlock {



        var choice = arc4random_uniform(3)

        switch choice {
        case 1 :
            var ball1 = SKSpriteNode(imageNamed: "redBall")
            ball1.position = CGPointMake(self.size.width/3, self.size.height)
            ball1.setScale(0.5)
            ball1.size.height = 20
            ball1.size.width = 30
            ball1.physicsBody = SKPhysicsBody(circleOfRadius: ball1.size.height / 2.75)
            ball1.physicsBody!.dynamic = true
            self.addChild(ball1)
            println("0")


        case 0 :
            var ball2 = SKSpriteNode(imageNamed: "redBall")
            ball2.position = CGPointMake(self.size.width/5, self.size.height)
            ball2.setScale(0.5)
            ball2.size.height = 20
            ball2.size.width = 30
            ball2.physicsBody = SKPhysicsBody(circleOfRadius: ball2.size.height / 2.75)
            ball2.physicsBody!.dynamic = true
            self.addChild(ball2)
            println("1")


        case 2 :
            var ball3 = SKSpriteNode(imageNamed: "redBall")
            ball3.position = CGPointMake(self.size.width*4/5, self.size.height)
            ball3.setScale(0.5)
            ball3.size.height = 20
            ball3.size.width = 30
            ball3.physicsBody = SKPhysicsBody(circleOfRadius: ball3.size.height / 2.75)
            ball3.physicsBody!.dynamic = true
            self.addChild(ball3)
            println("2")


        default :
            println("Problem")

        }



    }

    let waitDuration : NSTimeInterval = 1.0
    let updateAndWaitAction = SKAction.sequence([updateAction,SKAction.waitForDuration(waitDuration)])
    let repeatForeverAction = SKAction.repeatActionForever(updateAndWaitAction)
    self.runAction(repeatForeverAction)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        if location.x > basket.position.x {
            if basket.position.x < self.frame.maxX {
                basket.runAction(actionMoveRight)
            }
        }
        else {
            if basket.position.x > self.frame.minX {
                basket.runAction(actionMoveLeft)
            }
        }
    }
}

override func update(currentTime: CFTimeInterval) {


}

}

Bodi Osman
  • 117
  • 1
  • 7
  • http://stackoverflow.com/questions/24169882/how-to-properly-remove-node-when-out-of-screen-bounds/24195006#24195006 While it may not seem related at first, it is an example of how to trigger an event when two objects come into contact. This method is the way collision detection is meant to be handled in sprite kit. RayWenderlich.com has some easy to follow tutorials that will get you off the ground in less than an hour. My answer will allow you to both detect a collision and have access to both objects that collide. This lets you remove the balls easily as well. – meisenman Apr 01 '15 at 14:18
  • Bear in mind you don't detect collisions - you detect contacts (between 2 SKNodes). Collisions are handled automatically by the game engine if you set things up that way.collision detection and contact detection are separate, real things and it helps to get the terminology right. Unfortunately what everyone else refers to and means by 'collision detection' is called contact detection in SK. – Steve Ives Jul 20 '16 at 09:44

1 Answers1

1

For now you have a code that typically used in situations where user is taping something. You need to use BodyA & BodyB and assign a bitmasks to your nodes.

self.basket.physicsBody?.categoryBitMask = ColliderType.basket.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball1.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball1.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball2.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball2.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball3.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball3.rawValue

And do that for every ball too. And then in func didBeginContact you should say to Xcode what to do, if you have an animation or something:

if (contact.bodyA.categoryBitMask == ColliderType.ball1.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball1.rawValue) {
        yourGameOverFunc()
    }
    if (contact.bodyA.categoryBitMask == ColliderType.ball2.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball2.rawValue) {
        yourGameOverFunc()
    }
    if (contact.bodyA.categoryBitMask == ColliderType.ball3.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball3.rawValue) {
        yourGameOverFunc()
    }
Burundanga
  • 668
  • 5
  • 15