0

I can’t find any help or solution for my problem. I have 4 SKSpriteNodes named: bottomGoalGreen, topGoalGreen, bottomGoalBlue, and topGoalBlue. I also have a ball that is a SKSpriteNode named ball. My first question/problem is when I have my ball collide with, for example, topGoalGreen or bottomGoalGreen, I want the topGoalGreen to be removed as well as bottomGoalGreen and then topGoalBlue and bottomGoalBlue to appear and vice versa. My other problem is with my ball and the collision. I have two SKAction.moveToY so the ball can move up and down the screen. I was wondering if the SKActions could be the culprit to why the collision will not happen. I hope I improved my question. If not, I will try again to clarify.

import Foundation
import SpriteKit
import UIKit

struct PhysicsCatagory {

static let bottomGoalGreen : UInt32 = 1
static let topGoalGreen : UInt32 = 2
static let bottomGoalBlue : UInt32 = 4
static let topGoalBlue : UInt32 = 8
static let ball : UInt32 = 16

}

class GamePlayScene: SKScene, SKPhysicsContactDelegate {

var topGoalGreen = SKSpriteNode(imageNamed: "green goal (top).png")
var bottomGoalGreen = SKSpriteNode(imageNamed: "green goal (bottom).png")
var topGoalBlue = SKSpriteNode(imageNamed: "blue goal (top).png")
var bottomGoalBlue = SKSpriteNode(imageNamed: "blue goal (bottom).png")
var ball = SKSpriteNode(imageNamed: "green ball.png")




override func didMoveToView(view: SKView) {
    //setup scene

    physicsWorld.gravity = CGVector.zeroVector
    physicsWorld.gravity = CGVectorMake(0, 0)
    physicsWorld.contactDelegate = self


    self.scene?.backgroundColor = UIColor.blackColor()
    self.scene?.size = CGSize(width: 640, height: 1136)



    //Top goal green code
    topGoalGreen.position = CGPoint (x: self.size.width * 0.5, y: self.size.width * 1.52)
    topGoalGreen.physicsBody = SKPhysicsBody(rectangleOfSize: topGoalGreen.size)
    topGoalGreen.size = CGSize (width: 300, height: 309)

    topGoalGreen.physicsBody?.dynamic = false
    topGoalGreen.physicsBody?.categoryBitMask = PhysicsCatagory.topGoalGreen
    topGoalGreen.physicsBody?.collisionBitMask = 0
    topGoalGreen.physicsBody?.contactTestBitMask = PhysicsCatagory.ball
    self.addChild(topGoalGreen)


    //Bottom goal code
    bottomGoalGreen.position = CGPoint (x: self.size.width * 0.5, y: self.size.width * 0.252)
    bottomGoalGreen.size = CGSize (width: 300, height: 309)

    bottomGoalGreen.physicsBody?.dynamic = false
    bottomGoalGreen.physicsBody?.categoryBitMask = PhysicsCatagory.bottomGoalGreen
    bottomGoalGreen.physicsBody?.contactTestBitMask = PhysicsCatagory.ball

    self.addChild(bottomGoalGreen)


    //Ball code
    ball.position = CGPoint (x: self.size.width * 0.5, y: self.size.width * 0.9)
    ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width / 2)
    ball.size = CGSize (width: 80, height: 82)

    ball.physicsBody?.dynamic = true
    ball.physicsBody?.categoryBitMask = PhysicsCatagory.ball
    ball.physicsBody?.collisionBitMask = 0
    ball.physicsBody?.contactTestBitMask = PhysicsCatagory.topGoalGreen

    ball.physicsBody?.categoryBitMask = PhysicsCatagory.bottomGoalGreen
    ball.physicsBody?.collisionBitMask = PhysicsCatagory.bottomGoalGreen
    ball.physicsBody?.contactTestBitMask = PhysicsCatagory.bottomGoalGreen

    let moveBallUp = SKAction.moveToY(1040, duration: 2)
    let moveBallDown = SKAction.moveToY(90, duration: 2)
    let moveUpAndDown = SKAction.sequence([moveBallUp, moveBallDown])
    let moveUpAndDownForever = SKAction.repeatActionForever(moveUpAndDown)
    ball.runAction(moveUpAndDownForever)


    self.addChild(ball)


}


func didBeginContact(contact: SKPhysicsContact) {

    var firstBody : SKPhysicsBody = contact.bodyA
    var secondBody : SKPhysicsBody = contact.bodyB

    if (((firstBody.categoryBitMask == PhysicsCatagory.topGoalGreen) && (secondBody.categoryBitMask == PhysicsCatagory.ball)) ||
        ((firstBody.categoryBitMask == PhysicsCatagory.ball) && (secondBody.categoryBitMask == PhysicsCatagory.topGoalGreen))){

            CollisionWithBall(firstBody.node as! SKSpriteNode, ball: secondBody.node as! SKSpriteNode)

            NSLog("Collision!")
    }

}

func CollisionWithBall(topGoalGreen : SKSpriteNode, ball : SKSpriteNode) {


}

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

}

}
behind the aura
  • 296
  • 2
  • 14
  • I just realized that I have a mistake in the question. What I mean is when the ball collides with a certain Y point, I want the SKSpriteNodes (topGoal and bottomGoal) to change to the SKTextures. – behind the aura Jul 17 '15 at 21:31

2 Answers2

1

I figured out what I was missing. I was missing a SKPhysicsBody for both goals and ball. Thanks to the other commenters for trying to help me.

//Top goal green code
    topGoalGreen.position = CGPoint (x: self.size.width * 0.5, y: self.size.width * 1.67)
    topGoalGreen.size = CGSize (width: 400, height: 80)
    topGoalGreen.physicsBody = SKPhysicsBody(rectangleOfSize: CGSize (width: 10, height: 10))
    topGoalGreen.physicsBody?.dynamic = false
    topGoalGreen.physicsBody?.categoryBitMask = PhysicsCatagory.topGoalGreen
    topGoalGreen.physicsBody?.contactTestBitMask = PhysicsCatagory.ball
    self.addChild(topGoalGreen)
behind the aura
  • 296
  • 2
  • 14
0

In order to register contact you should set contact delegate.

physicsWorld.contactDelegate = self

Otherwise , you can't use methods like didBeginContact or didEndContact. Also you have to set category, contact and collision bitmasks properly in order to make everything to work. So, next thing would be to set category and contact bit masks properly.

ball.physicsBody?.categoryBitMask = PhysicsCategory.ball

//Contact will be registered when ball makes a contact with top or bottom goal.
ball.physicsBody?.contactTestBitMask = PhysicsCategory.topGoal |  PhysicsCategory.bottomGoal 

You have to follow this principle as well for top and bottom goal nodes.

Check out this great answer by rickster in order to understand how things works when using physics engine in SpriteKit.

Community
  • 1
  • 1
Whirlwind
  • 14,286
  • 11
  • 68
  • 157