TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

didBegin is not working in swift

Forums > Swift

currently I'm in project 17 where i have complete full project but the in last video of it there is use the didbegin but it is not working in me xcode and in ipad simulater

code:

//
//  GameScene.swift
//  project17
//
//  Created by Divyang Parmar on 02/09/23.
//

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    var starFeild: SKEmitterNode!
    var player: SKSpriteNode!
    var scoreLabel: SKLabelNode!
    var score = 0 {
        didSet {
            scoreLabel.text = "Score \(score)"
        }
    }

    var possibleEnemies = ["ball", "hammer", "tv"]
    var gameTimer: Timer?
    var isGameOver = false

    override func didMove(to view: SKView) {
        backgroundColor = .black

        starFeild = SKEmitterNode(fileNamed: "starfield")!
        starFeild.position = CGPoint(x: 1024, y: 384)
        starFeild.advanceSimulationTime(10)
        starFeild.zPosition = -1
        addChild(starFeild)

        player = SKSpriteNode(imageNamed: "player")
        player.position = CGPoint(x: 100, y: 384)
        player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
        player.physicsBody?.collisionBitMask = 1
        addChild(player)

        scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
        scoreLabel.text = "Score \(score)"
        scoreLabel.horizontalAlignmentMode = .left
        scoreLabel.position = CGPoint(x: 15, y: 15)
        addChild(scoreLabel)

        physicsWorld.gravity = .zero
        physicsWorld.contactDelegate = self

        gameTimer = Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)
    }

    override func update(_ currentTime: TimeInterval) {
        for node in children {
            if node.position.x < -300 {
                node.removeFromParent()
            }
        }

        if !isGameOver {
            score += 1
        }
    }

    @objc func createEnemy() {
        guard let enemy = possibleEnemies.randomElement() else { return }

        let sprit = SKSpriteNode(imageNamed: enemy)
        sprit.position = CGPoint(x: 1200, y: Int.random(in: 50...735))
        addChild(sprit)

        sprit.physicsBody = SKPhysicsBody(texture: sprit.texture!, size: sprit.size)
        sprit.physicsBody?.categoryBitMask = sprit.physicsBody?.collisionBitMask ?? 0
        sprit.physicsBody?.velocity = CGVector(dx: -500, dy: 0)
        sprit.physicsBody?.angularVelocity = 5
        sprit.physicsBody?.linearDamping = 0
        sprit.physicsBody?.angularDamping = 0
    }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        guard let touch = touches.first else { return }

        var location = touch.location(in: self)

        if location.y < 100 {
            location.y = 100
        } else if location.y > 668 {
            location.y = 668
        }

        player.position = location
    }

     func didBegin(_ contact: SKPhysicsContact) {
        let explosion = SKEmitterNode(fileNamed: "explosion")!
        explosion.position = player.position
        addChild(explosion)

        player.removeFromParent()
        isGameOver = true
    }

}

3      

Hi! Here is the working code, so you can check what is missing or play around with it to find out why yours is not working.

class GameScene: SKScene, SKPhysicsContactDelegate {

  var starfield: SKEmitterNode!
  var player: SKSpriteNode!

  var scoreLabel: SKLabelNode!
  var score = 0 {
    didSet {
      scoreLabel.text = "Score: \(score)"
    }
  }

  let possibleEnemies = ["ball", "hammer", "tv"]
  var isGameOver = false
  var gameTimer: Timer?

  override func didMove(to view: SKView) {
    backgroundColor = .black

    starfield = SKEmitterNode(fileNamed: "starfield")
    starfield.position = CGPoint(x: 1024, y: 384)
    starfield.advanceSimulationTime(10)
    addChild(starfield)
    starfield.zPosition = -1

    player = SKSpriteNode(imageNamed: "player")
    player.position = CGPoint(x: 100, y: 384)
    player.physicsBody = SKPhysicsBody(texture: player.texture!, size: player.size)
    player.physicsBody?.contactTestBitMask = 1
    addChild(player)

    scoreLabel = SKLabelNode(fontNamed: "Chalkduster")
    scoreLabel.position = CGPoint(x: 16, y: 16)
    scoreLabel.horizontalAlignmentMode = .left
    addChild(scoreLabel)

    score = 0

    physicsWorld.gravity = .zero
    physicsWorld.contactDelegate = self

    gameTimer = Timer.scheduledTimer(timeInterval: 0.35, target: self, selector: #selector(createEnemy), userInfo: nil, repeats: true)

  }

  override func update(_ currentTime: TimeInterval) {
    for node in children {
      if node.position.x < -300 {
        node.removeFromParent()
      }
    }
    if !isGameOver {
      score += 1
    }
  }

  @objc func createEnemy() {
    guard let enemy = possibleEnemies.randomElement() else { return }

    let sprite = SKSpriteNode(imageNamed: enemy)
    sprite.position = CGPoint(x: 1200, y: Int.random(in: 50...736))
    addChild(sprite)

    sprite.physicsBody = SKPhysicsBody(texture: sprite.texture!, size: sprite.size)
    sprite.physicsBody?.categoryBitMask = 1
    sprite.physicsBody?.velocity = CGVector(dx: -500, dy: 0)
    sprite.physicsBody?.angularVelocity = 5
    sprite.physicsBody?.linearDamping = 0
    sprite.physicsBody?.angularDamping = 0
  }

  override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    guard let touch = touches.first else { return }
    var location = touch.location(in: self)

    if location.y < 100 {
      location.y = 100
    } else if location.y > 668 {
      location.y = 668
    }

    player.position = location
  }

  func didBegin(_ contact: SKPhysicsContact) {
    let explosion = SKEmitterNode(fileNamed: "explosion")!
    explosion.position = player.position
    addChild(explosion)

    player.removeFromParent()
    isGameOver = true
  }
}

3      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.