I have a 40x40 rectangle node, and I want to detect when the bottom touches a platform.
I tried this
let feet = SKPhysicsBody(rectangleOfSize: CGSize(width: hero.frame.size.width, height: 1), center: CGPoint(x: 0, y: -(hero.frame.size.height/2 - 0.5)))
then set the categoryBitMask, collisionBitMask, contactTestBitMaskand added it to the hero
hero.physicsBody = SKPhysicsBody(bodies: [feet])
But in didBeginContact the println() doesn't print.
I need a body for the bottom of the rectangle and one for the top, because if hero hits a platform from below the collision should push him down.
Update
Here is how I set the bit masks
let heroFeetCategory: UInt32 = 1 << 0
let edgeCategory: UInt32 = 1 << 1
let groundCategory: UInt32 = 1 << 2
let feet = SKPhysicsBody(rectangleOfSize: CGSize(width: hero.frame.size.width, height: 10), center: CGPoint(x: 0, y: -(hero.frame.size.height/2 - 5)))
feet.collisionBitMask = edgeCategory | groundCategory
feet.contactTestBitMask = groundCategory
feet.categoryBitMask = heroFeetCategory
hero.physicsBody = SKPhysicsBody(bodies: [feet])
hero.physicsBody?.usesPreciseCollisionDetection = true
hero.physicsBody?.velocity = CGVectorMake(0, 0)
hero.physicsBody?.restitution = 0.0
hero.physicsBody?.friction = 0.0
hero.physicsBody?.angularDamping = 0.0
hero.physicsBody?.linearDamping = 1.0
hero.physicsBody?.allowsRotation = false
hero.physicsBody?.mass = 0.0641777738928795
world.addChild(hero)
and for the ground
let ground = SKSpriteNode(color: UIColor.whiteColor(), size: CGSizeMake(38, 38))
ground.name = "groundName"
ground.position = CGPoint(x: 0, y: -(self.frame.size.height/2 - ground.frame.size.height/2))
ground.physicsBody = SKPhysicsBody(rectangleOfSize: ground.size)
ground.physicsBody?.collisionBitMask = edgeCategory | heroFeetCategory
ground.physicsBody?.contactTestBitMask = heroFeetCategory
ground.physicsBody?.categoryBitMask = groundCategory
world.addChild(ground)
And how I detect if they touch
func didBeginContact(contact: SKPhysicsContact) {
var notTheHero: SKPhysicsBody!;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
notTheHero = contact.bodyB;
} else {
notTheHero = contact.bodyA;
}
println(notTheHero.node) // print "heroName"
if (notTheHero.categoryBitMask == groundCategory) {
println("touch began?"); // is never called
}
}
collision is fine, but when you print the node's name through the physics body with notTheHero.node you only access the SKNode, and not the NSString property of its name. Instead, try something like println(notTheHero.node.name);
Related
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
let balls = [
SKSpriteNode(imageNamed: "blueball.png"),
SKSpriteNode(imageNamed: "greenball.png"),
SKSpriteNode(imageNamed: "realredball.png"),
]
let redRectangle = SKSpriteNode(imageNamed: "redrectangle.png")
let blueRectangle = SKSpriteNode(imageNamed: "bluerectangle.png")
let greenRectangle = SKSpriteNode(imageNamed: "greenrectangle.png")
let wall1 = SKSpriteNode(imageNamed: "drop_wall.png")
let wall2 = SKSpriteNode(imageNamed: "drop_wall.png")
let bottom = SKSpriteNode(imageNamed:"drop_bottom.png")
let top = SKSpriteNode(imageNamed:"drop_bottom.png")
let blueBallCategory :UInt32 = 0x1 << 0
let greenBallCategory :UInt32 = 0x1 << 1
let realRedBallCategory :UInt32 = 0x1 << 2
let redRectangleCategory : UInt32 = 0x1 << 3
let blueRectangleCategory : UInt32 = 0x1 << 4
let greenRectangleCategory : UInt32 = 0x1 << 5
override func didMove(to view: SKView) {
spawnBalls()
spawnRectangles()
physicsWorld.contactDelegate = self
moveRectangles()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for ball in balls{
ball.isUserInteractionEnabled = false
}
physics()
}
func didBegin(_ contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case blueBallCategory | blueRectangleCategory:
for ball in balls{
ball.removeFromParent()
}
print("Alive! Blue ball has hit blue rectangle.")
case greenBallCategory | greenRectangleCategory:
print("Alive! Green ball has hit green rectangle.")
case realRedBallCategory | redRectangleCategory:
print("Alive! Red ball has hit red rectangle.")
case blueBallCategory | redRectangleCategory:
print("dead")
case blueBallCategory | greenRectangleCategory:
print("dead")
case realRedBallCategory | blueRectangleCategory:
print("dead")
case realRedBallCategory | greenRectangleCategory:
print("dead")
case greenBallCategory | redRectangleCategory:
print("dead")
case greenBallCategory | blueRectangleCategory:
print("dead")
default:
print("missed")
}
}
func spawnRectangles() {
redRectangle.position = CGPoint(x: 0, y: -400)
redRectangle.size = CGSize(width: 200, height: 20)
blueRectangle.position = CGPoint(x: -300, y: -200)
blueRectangle.size = CGSize(width: 200, height: 20)
greenRectangle.position = CGPoint(x: 100, y: -550)
greenRectangle.size = CGSize(width: 200, height: 20)
self.addChild(redRectangle)
self.addChild(blueRectangle)
self.addChild(greenRectangle)
wall1.position = CGPoint(x: -367.04, y: 0)
wall1.size = CGSize(width: 20, height: 1350)
wall1.physicsBody = SKPhysicsBody(rectangleOf: wall1.size)
wall1.physicsBody?.isDynamic = false
wall1.physicsBody?.affectedByGravity = false
self.addChild(wall1)
wall2.position = CGPoint(x: 367.04, y: 0)
wall2.size = CGSize(width: 20, height: 1350)
wall2.physicsBody = SKPhysicsBody(rectangleOf: wall2.size)
wall2.physicsBody?.isDynamic = false
wall2.physicsBody?.affectedByGravity = false
self.addChild(wall2)
top.position = CGPoint(x: 0, y: 657)
top.size = CGSize(width: 765, height: 20)
top.physicsBody = SKPhysicsBody(rectangleOf: top.size)
top.physicsBody?.isDynamic = false
top.physicsBody?.affectedByGravity = false
self.addChild(top)
bottom.size = CGSize(width: 765, height: 20)
bottom.position = CGPoint(x: 0, y: -657)
bottom.physicsBody = SKPhysicsBody(rectangleOf: bottom.size)
bottom.physicsBody?.isDynamic = false
bottom.physicsBody?.affectedByGravity = false
self.addChild(bottom)
}
func physics(){
for ball in balls{
ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.height/2)
ball.physicsBody?.contactTestBitMask = blueRectangleCategory | greenRectangleCategory | redRectangleCategory
}
redRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
redRectangle.physicsBody?.affectedByGravity = false
redRectangle.physicsBody?.isDynamic = false
blueRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
blueRectangle.physicsBody?.affectedByGravity = false
blueRectangle.physicsBody?.isDynamic = false
greenRectangle.physicsBody = SKPhysicsBody(rectangleOf: redRectangle.size)
greenRectangle.physicsBody?.isDynamic = false
greenRectangle.physicsBody?.affectedByGravity = false
balls[0].physicsBody?.categoryBitMask = blueBallCategory
balls[1].physicsBody?.categoryBitMask = greenBallCategory
balls[2].physicsBody?.categoryBitMask = realRedBallCategory
redRectangle.physicsBody?.categoryBitMask = redRectangleCategory
blueRectangle.physicsBody?.categoryBitMask = blueRectangleCategory
greenRectangle.physicsBody?.categoryBitMask = greenRectangleCategory
}
func moveRectangles(){
let redMoveRight = SKAction.moveTo(x: 300, duration: 2)
let redMoveLeft = SKAction.moveTo(x: -280, duration: 2)
let redWholeMovement = SKAction.repeatForever(SKAction.sequence([redMoveRight,redMoveLeft]))
redRectangle.run(redWholeMovement)
let blueMoveRight = SKAction.moveTo(x: 300, duration: 2)
let blueMoveLeft = SKAction.moveTo(x: -280, duration: 1.5)
let blueWholeMovement = SKAction.repeatForever(SKAction.sequence([blueMoveRight,blueMoveLeft]))
blueRectangle.run(blueWholeMovement)
let greenMoveRight = SKAction.moveTo(x: 300, duration: 2)
let greenMoveLeft = SKAction.moveTo(x: -280, duration: 1.5)
let greenWholeMovement = SKAction.repeatForever(SKAction.sequence([greenMoveLeft,greenMoveRight]))
greenRectangle.run(greenWholeMovement)
}
func spawnBalls(){
let ball = balls[Int(arc4random_uniform(UInt32(balls.count)))]
ball.position = CGPoint(x: 0, y: 250)
ball.size = CGSize(width: 70, height: 70)
self.addChild(ball)
}
}
I want a new ball to spawn at the top of the screen if a ball hits a same colored rectangle. Right now when I run the app a randomly colored ball is spawned at the top and when the user clicks the ball drops. If the ball makes contact with a moving rectangle of the same color of the ball the game is supposed to keep going. But it just ends after. If anyone could help that would be great.Thanks!
You only spawn one ball in spawnBalls(). So when you touch only one ball appears. You should try to move spawnBalls() to touchesBegan().
bellow is my code which for some reason won't recognise contact between two sprites, in this case, ninja and left or right wall. So my question is what am I missing? I add collision categories and assigned them accordingly
func didBegin(_ contact: SKPhysicsContact) {
let nodeB = contact.bodyB.node!
if nodeB.name == "RIGHT_WALL" {
print("right wall touched")
RightWallTouched = true
self.CurrentScore = self.CurrentScore + 1
}
if nodeB.name == "LEFT_WALL" {
print("left wall touched")
RightWallTouched = false
self.CurrentScore = self.CurrentScore + 1
}
self.CurrentScoreLabel.text = "\(self.CurrentScore)"
if nodeB.name == "SPIKE" {
self.GameStarted = false
if(CurrentScore > HighScore){
defaults.set(CurrentScore, forKey: self.HighScoreStorageKey)
defaults.synchronize()
self.HighScoreLabel.text = "\(self.defaults.integer(forKey: self.HighScoreStorageKey))"
}
endGame()
}
}
func CreateNinja(){
let ninjaTexture = SKTexture(imageNamed: "ninja-right.png")
ninjaTexture.filteringMode = .nearest
self.ninja = SKSpriteNode(texture: ninjaTexture)
self.ninja!.physicsBody = SKPhysicsBody(texture: ninjaTexture, size: ninja!.size)
self.ninja?.name = "NINJA"
self.ninja!.physicsBody?.isDynamic = false
self.ninja!.physicsBody?.allowsRotation = false
self.ninja!.physicsBody?.restitution = 0.6
self.ninja!.physicsBody?.mass = 0.430 // m = 430g
self.ninja!.position = CGPoint(x: self.frame.size.width / 2 , y: self.frame.size.height/5)
self.ninja!.physicsBody?.categoryBitMask = ninjaCollisionCategory
self.ninja!.physicsBody?.contactTestBitMask = 0
self.ninja!.physicsBody?.collisionBitMask = wallCollisionCategory
self.addChild(self.ninja!)
}
func CreateWall(){
let topwall = SKSpriteNode()
topwall.name = "TOP_WALL"
topwall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.size.width, height: 5))
topwall.physicsBody?.categoryBitMask = wallCollisionCategory
topwall.physicsBody?.isDynamic = false
topwall.physicsBody?.restitution = 0.6
topwall.position = CGPoint(x: self.frame.size.width / 2.0 , y: self.frame.size.height)
topwall.physicsBody?.friction = 0
topwall.zPosition = 4
self.addChild(topwall)
let leftwall = SKSpriteNode(color: UIColor.black, size: CGSize(width: 20, height: self.frame.size.height))
leftwall.name = "LEFT_WALL"
leftwall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: self.frame.size.height))
leftwall.physicsBody?.categoryBitMask = wallCollisionCategory
leftwall.physicsBody?.contactTestBitMask = 0
leftwall.physicsBody?.isDynamic = false
leftwall.position = CGPoint(x: 0 , y: self.frame.size.height / 2.0)
leftwall.zPosition = 4
leftwall.physicsBody?.restitution = 1.0
leftwall.physicsBody?.friction = 0
self.addChild(leftwall)
let rightwall = SKSpriteNode(color: UIColor.black, size: CGSize(width: 20, height: self.frame.size.height))
rightwall.name = "RIGHT_WALL"
rightwall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: self.frame.size.height))
rightwall.physicsBody?.categoryBitMask = wallCollisionCategory
rightwall.physicsBody?.contactTestBitMask = 0
rightwall.physicsBody?.isDynamic = false
rightwall.physicsBody?.restitution = 1.0
rightwall.physicsBody?.friction = 0
rightwall.position = CGPoint(x: self.frame.size.width , y: self.frame.size.height / 2.0)
rightwall.zPosition = 4
self.addChild(rightwall)
}
A more common way to do the collision function would be something like this
func didBegin(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// Ninja hit wall or wall hit ninja
if (firstBody.categoryBitMask == ninjaCollisionCategory) && (secondBody.categoryBitMask == wallCollisionCategory) {
// Do something for all walls
...
// you can also check the name for some specific wall e.g.
if secondBody.node?.name == "RIGHT_WALL" {
// hit right wall
}
}
}
You also need to set the delegate in didMoveToView
physicsWorld.contactDelegate = self
and should probably set your ninja sprite physics body to dynamic
ninja?.physicsBody?.isDynamic = true // You can also remove this line as its set to true by default
Finally, as good practice, try to follow the Swift guidelines, your functions and properties should start with small letters not capital letters e.g
var currentScore = 0
func createNinja() { ... }
Hope this helps
I am trying to make a ball bounce within a circle so it cant get out of the circumference of the circle. Now the ball bounce in self.frame. How can I make the ball bounce within the circle?
let sceneBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
sceneBody.friction = 0
self.physicsBody = sceneBody
var ball = SKShapeNode(circleOfRadius: 9)
ball.fillColor = SKColor.whiteColor()
ball.position = view.center
ball.physicsBody = SKPhysicsBody(circleOfRadius: 9)
ball.physicsBody?.affectedByGravity = true
ball.physicsBody?.restitution = 1
ball.physicsBody?.linearDamping = 0
ball.zPosition = 3
self.addChild(ball)
circulo = SKSpriteNode(imageNamed: "circuloFondo2")
//Circle that I want the ball bounce within
circulo.size = CGSize(width:view.frame.size.width - padding , height: view.frame.size.width - padding)
circulo.color = UIColor(red: 0.15, green: 0.15, blue: 0.15, alpha: 1)
circulo.colorBlendFactor = 1
circulo.alpha = 0.35
circulo.position = view.center
self.addChild(circulo)
circulo.zPosition = 1
I've prepared a simple test project to explain how to do it:
import UIKit
import SpriteKit
class GameScene: SKScene, SKPhysicsContactDelegate {
enum CollisionTypes: UInt32 {
case Circle = 1
case Ball = 2
//case Enemy1 = 4
//case Enemy2 = 8
//case Enemy3 = 16
//case EnemyBoss = 32
}
var circulo: SKSpriteNode!
var padding: CGFloat = 40.0
var center: CGPoint!
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, -6)
self.physicsWorld.contactDelegate = self
self.center = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame))
// Prepare circle
circulo = SKSpriteNode(imageNamed: "circuloFondo2.png")
//Circle that I want the ball bounce within
circulo.size = CGSize(width:view.frame.size.width - padding , height: view.frame.size.width - padding)
let circlePath = UIBezierPath(arcCenter: self.center, radius: (view.frame.size.width - padding*2)/2, startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true)
circulo.color = UIColor(red: 0.15, green: 0.15, blue: 0.15, alpha: 1)
circulo.colorBlendFactor = 1
circulo.alpha = 1
circulo.position = self.center
circulo.zPosition = 1
// Set physics
let circuloBody = SKPhysicsBody.init(edgeLoopFromPath: circlePath.CGPath)
self.physicsBody = circuloBody
self.physicsBody!.affectedByGravity = false
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.dynamic = true
self.physicsBody!.mass = 0
self.physicsBody!.friction = 0
self.physicsBody!.linearDamping = 0
self.physicsBody!.angularDamping = 0
self.physicsBody!.restitution = 1
self.physicsBody!.categoryBitMask = CollisionTypes.Circle.rawValue
self.physicsBody!.contactTestBitMask = CollisionTypes.Ball.rawValue
//self.circulo.physicsBody!.collisionBitMask = 0
self.addChild(circulo)
//Prepare ball
let ball = SKShapeNode(circleOfRadius: 9)
ball.fillColor = SKColor.whiteColor()
ball.position = self.center
ball.zPosition = 1
// Set physics
ball.physicsBody = SKPhysicsBody(circleOfRadius: 9)
ball.physicsBody!.affectedByGravity = true
ball.physicsBody!.restitution = 0.8
ball.physicsBody!.linearDamping = 0
ball.physicsBody!.friction = 0.3
ball.physicsBody?.dynamic = true
ball.physicsBody!.mass = 0.5
ball.physicsBody!.allowsRotation = true
ball.physicsBody!.categoryBitMask = CollisionTypes.Ball.rawValue
ball.physicsBody!.contactTestBitMask = CollisionTypes.Circle.rawValue
//ball.physicsBody!.collisionBitMask = 0
self.addChild(ball)
}
func didBeginContact(contact: SKPhysicsContact) {
// elements
if (contact.bodyA.categoryBitMask == CollisionTypes.Circle.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Ball.rawValue) {
print("contact between circle and ball")
}
}
The circuloFondo2 image:
A demo about the project:
In this answer you can see all the code to do it, but if you want you can download the project here
I have a moving platform, but when the node is above the platform it doesnt move with the platform horizontally
In this article, the problem is explained: Moving Platform Hell
http://www.learn-cocos2d.com/2013/08/physics-engine-platformer-terrible-idea/
And in comment there is solutions for Box2D: Kinematic body
But what about SpriteKit ?
Update
I'm moving the platform using
let moveHPart1 = SKAction.moveByX(origW, y: 0, duration: moveDuration);
let moveHPart2 = SKAction.moveByX(-origW, y: 0, duration: moveDuration);
platform(SKAction.repeatActionForever(SKAction.sequence([moveHPart1, moveHPart2])));
Personally I am against of using physics for moving platforms because moving platform physics body has to be dynamic.
Static platforms
For static platforms setting physics body dynamic property to false is perfect solution. And this is how it is meant to be. Static bodies are not affected by forces but still give you a collision response. So, the problem is solved.
But you can't change position of static physics bodies by using forces. You can do this by using actions or manually setting its position. But, then you are removing a platform out of physics simulation.
In order to do all with physics, you have to keep the platform dynamic. But this can lead in other problems. For example when player lands on platform, he will push the platform down, because player has a mass.
Even if platform has big mass it will go down as time passing. Remember, we cant just update platforms x position manually, because this can make a mess with physics simulation.
"Moving platform hell" as stated in that nice article of LearnCocos2d is probably the best description what can happen when using physics for this task :-)
Moving platform example
To show you some possibilities I made an simple example on how you can move a platform with applying a force to it, and make a character to stay on it.There are few things I've done in order to make this to work:
Changed a mass of platform. This will prevent platform from moving when player bumps in it from below.
Made an edge based physics body to prevent platform falling when player lands on it.
Played with properties like allows rotation and friction to get desired effect.
Here is the code :
import SpriteKit
class GameScene: SKScene,SKPhysicsContactDelegate
{
let BodyCategory : UInt32 = 0x1 << 1
let PlatformCategory : UInt32 = 0x1 << 2
let WallCategory : UInt32 = 0x1 << 3
let EdgeCategory : UInt32 = 0x1 << 4 // This will prevent a platforom from falling down
let PlayerCategory : UInt32 = 0x1 << 5
let platformSpeed: CGFloat = 40.0
let body = SKShapeNode(circleOfRadius: 20.0)
let player = SKShapeNode(circleOfRadius: 20.0)
let platform = SKSpriteNode(color: SKColor.greenColor(), size: CGSize(width:100, height:20))
let notDynamicPlatform = SKSpriteNode(color: SKColor.greenColor(), size: CGSize(width:100, height:20))
override func didMoveToView(view: SKView)
{
//Setup contact delegate so we can use didBeginContact and didEndContact methods
physicsWorld.contactDelegate = self
//Setup borders
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody?.categoryBitMask = WallCategory
self.physicsBody?.collisionBitMask = BodyCategory | PlayerCategory
//Setup some physics body object
body.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
body.fillColor = SKColor.greenColor()
body.physicsBody = SKPhysicsBody(circleOfRadius: 20)
body.physicsBody?.categoryBitMask = BodyCategory
body.physicsBody?.contactTestBitMask = PlatformCategory
body.physicsBody?.collisionBitMask = PlatformCategory | WallCategory
body.physicsBody?.allowsRotation = false
body.physicsBody?.dynamic = true
self.addChild(body)
//Setup player
player.position = CGPoint(x: CGRectGetMidX(self.frame), y:30)
player.fillColor = SKColor.greenColor()
player.physicsBody = SKPhysicsBody(circleOfRadius: 20)
player.physicsBody?.categoryBitMask = PlayerCategory
player.physicsBody?.contactTestBitMask = PlatformCategory
player.physicsBody?.collisionBitMask = PlatformCategory | WallCategory | BodyCategory
player.physicsBody?.allowsRotation = false
player.physicsBody?.friction = 1
player.physicsBody?.dynamic = true
self.addChild(player)
//Setup platform
platform.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) - 100)
platform.physicsBody = SKPhysicsBody(rectangleOfSize: platform.size)
platform.physicsBody?.categoryBitMask = PlatformCategory
platform.physicsBody?.contactTestBitMask = BodyCategory
platform.physicsBody?.collisionBitMask = BodyCategory | EdgeCategory | PlayerCategory
platform.physicsBody?.allowsRotation = false
platform.physicsBody?.affectedByGravity = false
platform.physicsBody?.dynamic = true
platform.physicsBody?.friction = 1.0
platform.physicsBody?.restitution = 0.0
platform.physicsBody?.mass = 20
//Setup edge
let edge = SKNode()
edge.physicsBody = SKPhysicsBody(edgeFromPoint: CGPoint(x: 0, y:-platform.size.height/2), toPoint: CGPoint(x: self.frame.size.width, y:-platform.size.height/2))
edge.position = CGPoint(x:0, y: CGRectGetMidY(self.frame) - 100)
edge.physicsBody?.categoryBitMask = EdgeCategory
edge.physicsBody?.collisionBitMask = PlatformCategory
self.addChild(edge)
self.addChild(platform)
}
override func update(currentTime: NSTimeInterval) {
if(platform.position.x <= platform.size.width/2.0 + 20.0 && platform.physicsBody?.velocity.dx < 0.0 ){
platform.physicsBody?.velocity = CGVectorMake(platformSpeed, 0.0)
}else if((platform.position.x >= self.frame.size.width - platform.size.width/2.0 - 20.0) && platform.physicsBody?.velocity.dx >= 0.0){
platform.physicsBody?.velocity = CGVectorMake(-platformSpeed, 0.0)
}else if(platform.physicsBody?.velocity.dx > 0.0){
platform.physicsBody?.velocity = CGVectorMake(platformSpeed, 0.0)
}else{
platform.physicsBody?.velocity = CGVectorMake(-platformSpeed, 0.0)
}
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
let touch: AnyObject? = touches.anyObject()
let location = touch?.locationInNode(self)
if(location?.x > 187.5){
player.physicsBody?.applyImpulse(CGVector(dx: 3, dy: 50))
}else{
player.physicsBody?.applyImpulse(CGVector(dx: -3, dy: 50))
}
}
}
Here is the result :
import SpriteKit
// fix spawning so close to the middle
// get ball col working
// set width apart/ height they must be apart (if statement)
let BallCategoryName = "ball"
let BarCategoryName = "block"
let BarNodeCategoryName = "blockNode"
let GreenBallCategory : UInt32 = 0x1 << 0
let RedBallCategory: UInt32 = 0x1 << 1
let GreenBarCategory : UInt32 = 0x1 << 2
let RedBarCategory : UInt32 = 0x1 << 3
class GameScene: SKScene, SKPhysicsContactDelegate {
//VARIABLES IMPORTANTE//
var greenBall = SKSpriteNode(imageNamed: "greenball")
var redBall = SKSpriteNode(imageNamed: "redball")
var bar0 = SKSpriteNode(imageNamed: "bar0")
var bar1 = SKSpriteNode(imageNamed: "bar1")
var bar2 = SKSpriteNode(imageNamed: "bar2")
var bar3 = SKSpriteNode(imageNamed: "bar3")
var bar4 = SKSpriteNode(imageNamed: "bar4")
var bar5 = SKSpriteNode(imageNamed: "bar5")
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
// BALL CHARACTERISTICS//
//Green//
greenBall.position = CGPoint(x: frame.size.width*0.25, y: (frame.size.height * 0.95)/2)
greenBall.anchorPoint = CGPoint(x: 0.5, y: 0.5)
greenBall.physicsBody = SKPhysicsBody(circleOfRadius: greenBall.size.width/2)
greenBall.physicsBody?.friction = 0
greenBall.physicsBody?.restitution = 1
greenBall.physicsBody?.linearDamping = 0
greenBall.physicsBody?.angularDamping = 0
greenBall.physicsBody?.affectedByGravity = false
greenBall.name = BallCategoryName
addChild(greenBall)
//Red//
redBall.anchorPoint = CGPoint(x: 0.5 , y: 0.5)
redBall.position = CGPoint(x: frame.size.width*0.75 , y: (frame.size.height * 0.95)/2)
redBall.physicsBody = SKPhysicsBody(circleOfRadius: greenBall.size.width/2)
redBall.physicsBody?.friction = 0
redBall.physicsBody?.restitution = 1
redBall.physicsBody?.linearDamping = 0
redBall.physicsBody?.angularDamping = 0
redBall.physicsBody?.affectedByGravity = false
redBall.physicsBody?.dynamic = false
addChild(redBall)
//////////////////////////////////////////////////////
// SETTING UP SCENE//
GameScene(size: self.view!.frame.size) //WHy not work
let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = borderBody
self.physicsBody?.friction = 0
// BACKGROUND COLOR
// PHYSICS //
physicsWorld.gravity = CGVectorMake(0, 0)
//////////////////////////////////////////////////////
// EXTRA BARS
var midBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))
var menuBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))
var invisMenuBar = SKShapeNode(rectOfSize: CGSize(width: frame.size.width, height: 0.00001))
var botBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))
midBar.fillColor = SKColor.whiteColor()
menuBar.fillColor = SKColor.whiteColor()
invisMenuBar.fillColor = SKColor.purpleColor()
botBar.fillColor = SKColor.whiteColor()
////MID BAR////
var minus = frame.size.height * 0.049
midBar.position = CGPoint(x: frame.size.width/2, y: (frame.size.height * 0.95)/2)
addChild(midBar)
////BOT BAR////
botBar.position = CGPoint(x: frame.size.width/2, y: 0 + (botBar.frame.size.height/2))
addChild(botBar)
////MENU BAR////
menuBar.position = CGPoint(x: frame.size.width/2, y: frame.size.height - (frame.size.height * 0.05))
addChild(menuBar)
//// INVIS MENU BAR ////
invisMenuBar.position = CGPoint(x: frame.size.width/2, y: frame.size.height - (frame.size.height * 0.047))
invisMenuBar.physicsBody = SKPhysicsBody(rectangleOfSize: invisMenuBar.frame.size)
invisMenuBar.physicsBody?.friction = 0
invisMenuBar.physicsBody?.restitution = 1
invisMenuBar.physicsBody?.linearDamping = 0
invisMenuBar.physicsBody?.angularDamping = 0
invisMenuBar.physicsBody?.affectedByGravity = false
invisMenuBar.physicsBody?.dynamic = false
invisMenuBar.zPosition = 100
menuBar.zPosition = 5
addChild(invisMenuBar)
// TEST STUFF WITH BARS //
let chosenOne = SKSpriteNode(imageNamed: "bar4")
chosenOne.position = CGPoint(x: frame.midX - chosenOne.frame.width/2 - 50 , y: self.frame.height * 0.75)
addChild(chosenOne)
//////////////////////////////////////////////////////
//// SETTING UP MY CONTACT ///
greenBall.physicsBody?.categoryBitMask = GreenBallCategory
chosenOne.physicsBody?.categoryBitMask = RedBarCategory
greenBall.physicsBody?.contactTestBitMask = RedBarCategory
chosenOne.physicsBody?.contactTestBitMask = GreenBallCategory
chosenOne.physicsBody = SKPhysicsBody(rectangleOfSize: chosenOne.frame.size)
func didBeginContact(contact: SKPhysicsContact) {
// 1. Create local variables for two physics bodies
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody
// 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
// 3. react to the contact between ball and bottom
if firstBody.categoryBitMask == GreenBallCategory && secondBody.categoryBitMask == RedBarCategory {
//TODO: Replace the log statement with display of Game Over Scene
println("Hit bottom. First contact has been made.")
}
}
I'm following http://www.raywenderlich.com/84341/create-breakout-game-sprite-kit-swift to the letter. I've got the self contact delegate all set up, the two do hit each other but it doesn't print to the logs to prove the collision stuff is actually working.
Any ideas?
Your didBeginContact method is inside the didMoveToView method. Since both methods in SKPhysicsContactDelegate are optional:
protocol SKPhysicsContactDelegate : NSObjectProtocol {
optional func didBeginContact(contact: SKPhysicsContact)
optional func didEndContact(contact: SKPhysicsContact)
}
you're not being warned that GameScene doesn't implement the methods.
Move didBeginContact out of didMoveToView and you should be fine.
(On a side note: I would advise that you split up didMoveToView into several, smaller methods. It'll make it easier to understand what's going on because, at the moment, it looks pretty bloated.)