Hello!
I am trying to figure out how to detect a collision, but no matter what I do, it seems to not want to work(Using didBegin). I have used multiple resources to no avail and im so close to banging my head against the wall lol. Im hoping someone can Identify what im doing wrong with it. I'll attach my class for you :)
Thank you for any help!
//
// GameView.swift
// DogeBall
//
// Created by James Biser on 2/21/21.
//
import UIKit
import SpriteKit
import SceneKit
import QuartzCore
import CoreMotion
class GameView: UIViewController, SKSceneDelegate, SKPhysicsContactDelegate {
@IBOutlet var gameView: UIView!
@IBOutlet weak var pointsLabel: UILabel!
@IBOutlet weak var livesLeftLabel: UILabel!
let player = SKSpriteNode(imageNamed: "gameElement.png")
let goal = SKSpriteNode(imageNamed: "goal.png")
var motionManager = CMMotionManager()
var destX:CGFloat = 0.0
var destY : CGFloat = 0.0
@IBOutlet weak var SKGameView: SKView!
var scene = SKScene()
var toggle = true
override func viewDidLoad() {
// self.navigationController?.isNavigationBarHidden = true
scene.size = gameView.bounds.size
scene.scaleMode = .aspectFill
SKGameView.presentScene(scene)
SKGameView.showsFPS = true
SKGameView.showsNodeCount = true
SKGameView.showsPhysics = true
player.position = CGPoint(x: SKGameView.frame.size.width/2, y: SKGameView.frame.size.height/2)
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width / 2.0)
player.physicsBody?.affectedByGravity = false
player.physicsBody!.contactTestBitMask = 0
player.physicsBody?.collisionBitMask = 0
// goal.position = spawnGoalAtRandom()
goal.physicsBody = SKPhysicsBody(circleOfRadius: goal.size.width / 2.0)
goal.position = CGPoint(x: SKGameView.frame.size.width/2+9, y: SKGameView.frame.size.height/2+9)
goal.physicsBody?.affectedByGravity = false
goal.physicsBody!.contactTestBitMask = 0
goal.physicsBody!.collisionBitMask = 0
goal.physicsBody?.usesPreciseCollisionDetection = true
player.physicsBody?.usesPreciseCollisionDetection = true
scene.addChild(goal)
scene.addChild(player)
motionManager.startAccelerometerUpdates()
startAccelerometers()
}
func didBeginContact(contact : SKPhysicsContact){
print("cool")
}
func update(_ currentTime: TimeInterval, for scene: SKScene) {
print("update frame")
}
func spawnGoalAtRandom() - CGPoint{
let height = self.scene.frame.height
let width = self.scene.frame.width
let randomPosition = CGPoint(x: CGFloat(arc4random()).truncatingRemainder(dividingBy: height) - 10 , y: CGFloat(arc4random()).truncatingRemainder(dividingBy: width) - 10)
return randomPosition
}
var isTouched = false
override func touchesBegan(_ touches: SetUITouch, with event: UIEvent?) {
if let touch = touches.first {
if player.contains(touch.location(in: scene)) {
isTouched = true
}
}
}
override func touchesMoved(_ touches: SetUITouch, with event: UIEvent?) {
if (isTouched == true) {
player.position = (touches.first?.location(in: scene))!
}
}
override func touchesEnded(_ touches: SetUITouch, with event: UIEvent?) {
if isTouched {
isTouched = false
}
}
var timer = Timer()
func startAccelerometers() {
// Make sure the accelerometer hardware is available.
if self.motionManager.isAccelerometerAvailable {
self.motionManager.accelerometerUpdateInterval = 1.0 / 60.0 // 60 Hz
self.motionManager.startAccelerometerUpdates()
// Configure a timer to fetch the data.
self.timer = Timer(fire: Date(), interval: (1.0/60.0),
repeats: true, block: { (timer) in
// Get the accelerometer data.
if let data = self.motionManager.accelerometerData {
let x = data.acceleration.x
let y = data.acceleration.y
let z = data.acceleration.z
self.didMove()
self.update()
}
})
// Add the timer to the current run loop.
RunLoop.current.add(self.timer, forMode: .default)
}
}
func didMove() {
if motionManager.isAccelerometerAvailable {
// 2
motionManager.accelerometerUpdateInterval = 0.01
motionManager.startAccelerometerUpdates(to: .main) {
(data, error) in
guard let data = data, error == nil else {
return
}
// 3
let currentX = self.player.position.x
let currentY = self.player.position.y
self.destX = currentX + CGFloat(data.acceleration.x * 500)
self.destY = currentY + CGFloat(data.acceleration.y*500)
}
}
}
func update() {
if toggle {
let action = SKAction.moveTo(x: destX, duration: 1)
let action2 = SKAction.moveTo(y: destY, duration: 1)
player.run(action)
player.run(action2)
if !scene.frame.contains(player.position) {
self.player.position = CGPoint(x: SKGameView.frame.size.width/2, y: SKGameView.frame.size.height/2)
// self.navigationController?.present(ac, animated: true, completion: nil)
}
if player.contains(goal) {
print("work")
}
}
}
//Never gets called
func didBegin(_ contact: SKPhysicsContact) {
print("asdas")
}
}