balloon burst game in swift spriteKit

-1

i am new to swift spriteKit and i am developing a game in which some balloons appear on screen at random position and user has to tap on them to burst them.

so far i have achieved to display balloons at random position with tag on them but there is a problem that balloons are overlapping each other and i want that there will be only maximum 5 balloons on the screen at a time and do not overlap each other and each and every balloon must disappear after 5 seconds in the starting and as game become difficult the time changes to 2 seconds.

and there is also a problem that i have faced is i want to change the balloon image after user tap on ballon but in the touchesBegan() i am not able to access ballon node.

this is what i am doing

class GameScene: SKScene {
var balloonArray = ["Blue","Green","Yellow","Purpule","Red"]
var balloon = SKSpriteNode(imageNamed: "Blue")
var count = 1
let viewSize = UIScreen.main.bounds.size
var gameScore: SKLabelNode!
var score = 0 {
    didSet {
        gameScore.text = "Score: \(score)"
    }
}

override func didMove(to view: SKView) {

    let background = SKSpriteNode(imageNamed: "whackBackground")
    background.position = CGPoint(x: 512, y: 384)
    background.blendMode = .replace
    background.zPosition = -1
    addChild(background)

    gameScore = SKLabelNode(fontNamed: "Helvetica Neue")
    gameScore.text = "Score: 0"
    gameScore.position = CGPoint(x: viewSize.width/2, y: 30)
    gameScore.horizontalAlignmentMode = .center
    gameScore.fontSize = 30
    addChild(gameScore)

    addRandomBalloon()

}

func addRandomBalloon() {
    // This function randomly places balloon around the game scene
    //
    let wait = SKAction.wait(forDuration: 2)
    let block = SKAction.run({
        let randomX = self.randomFloat(from: 50, to: self.frame.size.width - 50)
        let randomY = self.randomFloat(from: 200 , to: self.frame.size.height - 100)
        let balloon = SKSpriteNode(imageNamed: "Blue")

       balloon.position = CGPoint(x: randomX, y: randomY)
        balloon.size = CGSize(width: 100, height: 100)
        balloon.name = "blue_balloon"
        self.addChild(balloon)

        let myLabel = SKLabelNode(fontNamed:"Helvetica Neue")
        myLabel.text = "\(self.count)"
        myLabel.fontSize = 30
        myLabel.fontColor = UIColor.black
        myLabel.horizontalAlignmentMode = .center
        myLabel.verticalAlignmentMode = .center
        balloon.addChild(myLabel)



        self.count = self.count+1

    })
    let sequence = SKAction.sequence([wait, block])
   run(SKAction.repeatForever(sequence), withKey: "addBlock")

}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

     if let touch = touches.first {

         let location = touch.location(in: self)
         let tappedNodes = nodes(at: location)

         for node in tappedNodes {
            if node.name == "blue_balloon"  {
                    score = score+1
            }


        }
    }     
}

// Marker: Random number generation
func randomFloat(from: CGFloat, to: CGFloat) -> CGFloat {
    // Used to randomize the starting direction of the ball
    //
    let random: CGFloat = CGFloat(Float(arc4random()) / 0xFFFFFFFF)
    return random * (to - from) + from
  }
}
ios
swift
asked on Stack Overflow Dec 30, 2018 by harinder rana

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0