1

I have these four circles that are moving from the bottom of the screen to the top. I have another circle in the middle of the screen. The circle in the middle can move to the left and right by touching either the left or right side of the screen. I want the score to increment by one every time the circle in the middle of the screen dodges one of the other 4 circles that are coming up. How would I accomplish this? Thanks. This is the code I have that is not working for what I want it to do.

// This function is called on contact between physics objects

func didBeginContact(contact:SKPhysicsContact){
    let node1:SKNode = contact.bodyB.node!
    let node2:SKNode = contact.bodyB.node!
    if primaryBall == contact.bodyA.node! {
        primaryBall.physicsBody?.affectedByGravity = true
        view?.scene?.paused = false
    } else {
        primaryBall.physicsBody?.affectedByGravity = false
    }
    if ballToDodge.size.height > -50 + primaryBall.size.height {
        self.score++
        highscoreLabel.text = String(score)
    }
}

override func update(currentTime: CFTimeInterval) {     

if ballToDodge.position.y < enemy.position.y {
        self.score++
        highscoreLabel.text = String(score)

    }

 }
swifter22
  • 65
  • 9
  • 1
    Is the method getting called? Test to see what happens when it is supposed to score. – Kendel Feb 22 '15 at 04:38
  • 1
    If you want to increase the score when circle in the middle dodges any of four circles coming from the bottom you don't need to check for contact detection in didBeginContact(dodging != contact:). You have to move your logic into update method and at every frame to check the y position of certain circle coming from the bottom and apply your logic based on the heights of your nodes (as well as anchor points setup)... – Whirlwind Feb 22 '15 at 10:20
  • 1
    Also you can do this without update and with didBeginContact but in the way that when certain circle from the bottom make contact with upper edge of the scene's physics body you do score updating. But I can't say for sure what will best apply to your game logic because I don't know what should happen when circle from the bottom make contact with circle from the middle... – Whirlwind Feb 22 '15 at 10:31
  • I already added the physicsbody when the circle in the middle makes contact with the circles coming from the bottom. Could you give me an example of how to do this in the update method because I cant seem to get it working? Thank you! – swifter22 Feb 22 '15 at 13:52
  • I added this code in the update method and the score is going really fast and when the circle in the middle makes contact with one of the circles coming from the bottom the score stops for half a second and then continues going fast. I want the score to go slower and also when the circle in the middle hits one of the circles coming from the bottom I want the scene to be paused. The code is in the op. – swifter22 Feb 22 '15 at 14:00
  • 1
    You can subclass SKSpriteNode and make your custom class with BOOL property set by default to YES and used to track whether or not score should be increased. So, after your sprite reaches desired point, you increase the score, and then set BOOL value to NO. This will enforce that score is increased only once per sprite. Something similar to this: http://stackoverflow.com/questions/28526694/physics-detecting-collision-multiple-times/28527280#28527280 – Whirlwind Feb 22 '15 at 19:16
  • Could you give me some code to do this because I still cant seem to figure this out? Thank you. – swifter22 Feb 23 '15 at 01:41

0 Answers0