Ok so I posted a question about this topic before and I was told to go more in-depth about my problem. I recently ran into an issue in Spritekit where a node would fly through another node in less than a frame so the system wouldn't catch it and it wouldn't bounce like I wanted it to. So I was told to use "usesPreciseCollisionDetection". I tried using the method you will see in my code snippet.
This is what my game is:
I have a line bouncing up a down the screen and there is a zone (I refer to it as "area") that you are supposed to get the line inside of. As the game progresses, the line gets faster and the zone gets smaller. Also at every multiple of 5, the line restarts from the bottom of the screen.
import SpriteKit
import GameplayKit
var impulseLevel = 30
var score = 0
var scoreString = String(score)
class GameScene: SKScene {
var line = SKSpriteNode()
var area = SKSpriteNode()
var label = SKLabelNode(text: scoreString)
override func didMove(to view: SKView) {
line = self.childNode(withName: "line") as! SKSpriteNode
area = self.childNode(withName: "area") as! SKSpriteNode
score = 139
scoreString = String(score)
//label
addChild(label)
label.position = CGPoint(x: 0 , y: 400)
label.fontColor = UIColor.black
label.text = scoreString
label.fontSize = 200
label.fontName = "HelveticaNeue-Bold"
//area position variables
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30 ))
line.physicsBody?.usesPreciseCollisionDetection = true
//border
let border = SKPhysicsBody(edgeLoopFrom: self.frame)
border.friction = 0
border.restitution = 0
self.physicsBody = border
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
//variables
var HalfHeight = area.size.height / 2
var upperhalf = area.position.y + HalfHeight
var lowerhalf = area.position.y - HalfHeight
for touch in touches{
if line.position.y >= lowerhalf && line.position.y <= upperhalf{
print("Works")
score = score + 1
scoreString = String(score)
label.text = scoreString
var random = -550 + Int(arc4random_uniform(UInt32(550+550+1)))
print(random)
area.position = CGPoint(x: 0, y: random)
if score == 5{
area.size.height = 285
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
}
if score == 10{
area.size.height = 270
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
}
if score == 15 {
area.size.height = 250
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 30))
}
if score == 20 {
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))
}
if score == 25 {
area.size.height = 225
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))
}
if score == 30 {
area.size.height = 205
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 35))
}
if score == 35 {
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))
}
var adding = impulseLevel/20
var added = impulseLevel + adding
if score > 35 && score % 5 == 0 {
adding = impulseLevel/20
added = impulseLevel + adding
line.position = CGPoint(x:0, y: -600)
line.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
impulseLevel = added
line.physicsBody?.applyImpulse(CGVector(dx: 0, dy:impulseLevel))
}
}else{
print("You lose")
}
}
}
override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}
}
Sometimes this works but not all the time. When the impulse gets higher than 120, the line will go through the border anyway.