S

Samuel

Mitglied

Zuletzt aktiv vor 2 Monaten

  1. vor 2 Monaten
    j M Y, g:ia \G\M\TO
    S Samuel beginnt die Unterhaltung Lösung gesucht!.

    Hallo ich habe mich an dem Spiel "FlappyBird" versucht.
    Das funktioniert schon einmal sehr gut, allerdings würde ich gerne programmieren, dass das Spiel schneller abläuft und somit schwieriger wird.
    Dazu muss ich im folgenden Code die Zeilen: var delay = SKAction.wait(forDuration: 1.5) und var movePipes = SKAction.moveBy(x: -distance - 50, y: 0, duration: TimeInterval(0.008 * distance)) ändern.
    Die Änderung soll ausgelöst werden, wenn der Spieler eine gewisse Punktzahl erreicht hat. Also eine if-Abfrage mit der var score.
    Mein Problem ist jetzt, dass die Punkte erst weit unten im Code eingetragen werden und anschließend kann ich nicht mehr auf die Funktion override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) mit den obrigen zwei Zeilen zugreifen. Wie löse ich das Problem am besten??
    Vielen Dank im Voraus! Hier noch der vollständige Code:

    //
    // GameScene.swift

    import SpriteKit

    class GameScene: SKScene, SKPhysicsContactDelegate {

    var gameStarted = Bool(false)
    var died = Bool(false)
    let coinSound = SKAction.playSoundFileNamed("CoinSound.mp3", waitForCompletion: false)

    var score = Int(0)
    var scoreLbl = SKLabelNode()
    var highscoreLbl = SKLabelNode()
    var taptoplayLbl = SKLabelNode()
    var restartBtn = SKSpriteNode()
    var pauseBtn = SKSpriteNode()
    var logoImg = SKSpriteNode()
    var wallPair = SKNode()
    var moveAndRemove = SKAction()

    //CREATE THE BIRD ATLAS FOR ANIMATION
    let birdAtlas = SKTextureAtlas(named:"player")
    var birdSprites = Array<SKTexture>()
    var bird = SKSpriteNode()
    var repeatActionbird = SKAction()

    override func didMove(to view: SKView) {
    createScene()

    }





    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if gameStarted == false{
    gameStarted = true
    bird.physicsBody?.affectedByGravity = true
    createPauseBtn()
    logoImg.run(SKAction.scale(to: 0.5, duration: 0.3), completion: {
    self.logoImg.removeFromParent()
    })
    taptoplayLbl.removeFromParent()
    self.bird.run(repeatActionbird)

    let spawn = SKAction.run({
    () in
    self.wallPair = self.createWalls()
    self.addChild(self.wallPair)
    })


    var delay = SKAction.wait(forDuration: 1.5)
    let SpawnDelay = SKAction.sequence([spawn, delay])
    let spawnDelayForever = SKAction.repeatForever(SpawnDelay)
    self.run(spawnDelayForever)

    let distance = CGFloat(self.frame.width + wallPair.frame.width)
    var movePipes = SKAction.moveBy(x: -distance - 50, y: 0, duration: TimeInterval(0.008 * distance))
    let removePipes = SKAction.removeFromParent()
    moveAndRemove = SKAction.sequence([movePipes, removePipes])

    bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
    bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))

    } else {
    if died == false {
    bird.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
    bird.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 40))
    }
    }

    for touch in touches{
    let location = touch.location(in: self)
    if died == true{
    if restartBtn.contains(location){
    if UserDefaults.standard.object(forKey: "highestScore") != nil {
    let hscore = UserDefaults.standard.integer(forKey: "highestScore")
    if hscore < Int(scoreLbl.text!)!{
    UserDefaults.standard.set(scoreLbl.text, forKey: "highestScore")
    }
    } else {
    UserDefaults.standard.set(0, forKey: "highestScore")
    }
    restartScene()
    }
    } else {
    if pauseBtn.contains(location){
    if self.isPaused == false{
    self.isPaused = true
    pauseBtn.texture = SKTexture(imageNamed: "play")
    } else {
    self.isPaused = false
    pauseBtn.texture = SKTexture(imageNamed: "pause")
    }
    }
    }
    }
    }



    func restartScene(){
    self.removeAllChildren()
    self.removeAllActions()
    died = false
    gameStarted = false
    score = 0
    createScene()
    }

    func createScene(){


    self.physicsBody = SKPhysicsBody(edgeLoopFrom: self.frame)
    self.physicsBody?.categoryBitMask = CollisionBitMask.groundCategory
    self.physicsBody?.collisionBitMask = CollisionBitMask.birdCategory
    self.physicsBody?.contactTestBitMask = CollisionBitMask.birdCategory
    self.physicsBody?.isDynamic = false
    self.physicsBody?.affectedByGravity = false

    self.physicsWorld.contactDelegate = self
    self.backgroundColor = SKColor(red: 80.0/255.0, green: 192.0/255.0, blue: 203.0/255.0, alpha: 1.0)





    for i in 0..<2 {
    let background = SKSpriteNode(imageNamed: "bg")
    background.anchorPoint = CGPoint.init(x: 0, y: 0)
    background.position = CGPoint(x:CGFloat(i) * self.frame.width, y: 0)
    background.name = "background"
    background.size = (self.view?.bounds.size)!
    self.addChild(background)
    }

    //SET UP THE BIRD SPRITES FOR ANIMATION
    birdSprites.append(birdAtlas.textureNamed("bird3"))
    birdSprites.append(birdAtlas.textureNamed("bird4"))
    birdSprites.append(birdAtlas.textureNamed("bird3"))
    birdSprites.append(birdAtlas.textureNamed("bird4"))

    self.bird = createBird()
    self.addChild(bird)

    //ANIMATE THE BIRD AND REPEAT THE ANIMATION FOREVER
    let animatebird = SKAction.animate(with: self.birdSprites, timePerFrame: 10)
    self.repeatActionbird = SKAction.repeatForever(animatebird)

    scoreLbl = createScoreLabel()
    self.addChild(scoreLbl)

    highscoreLbl = createHighscoreLabel()
    self.addChild(highscoreLbl)

    createLogo()

    taptoplayLbl = createTaptoplayLabel()
    self.addChild(taptoplayLbl)
    }

    func didBegin(_ contact: SKPhysicsContact) {


    let firstBody = contact.bodyA
    let secondBody = contact.bodyB

    if firstBody.categoryBitMask == CollisionBitMask.birdCategory && secondBody.categoryBitMask == CollisionBitMask.pillarCategory || firstBody.categoryBitMask == CollisionBitMask.pillarCategory && secondBody.categoryBitMask == CollisionBitMask.birdCategory || firstBody.categoryBitMask == CollisionBitMask.birdCategory && secondBody.categoryBitMask == CollisionBitMask.groundCategory || firstBody.categoryBitMask == CollisionBitMask.groundCategory && secondBody.categoryBitMask == CollisionBitMask.birdCategory{
    enumerateChildNodes(withName: "wallPair", using: ({
    (node, error) in
    node.speed = 0
    self.removeAllActions()
    }))
    if died == false{
    died = true
    createRestartBtn()
    pauseBtn.removeFromParent()
    self.bird.removeAllActions()
    }
    } else if firstBody.categoryBitMask == CollisionBitMask.birdCategory && secondBody.categoryBitMask == CollisionBitMask.flowerCategory {
    run(coinSound)
    score += 1
    scoreLbl.text = "\(score)"
    secondBody.node?.removeFromParent()



    } else if firstBody.categoryBitMask == CollisionBitMask.flowerCategory && secondBody.categoryBitMask == CollisionBitMask.birdCategory {
    run(coinSound)
    score += 1
    scoreLbl.text = "\(score)"
    firstBody.node?.removeFromParent()

    }


    }

    override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
    if gameStarted == true{
    if died == false{
    enumerateChildNodes(withName: "background", using: ({
    (node, error) in
    let bg = node as! SKSpriteNode
    bg.position = CGPoint(x: bg.position.x - 2, y: bg.position.y)
    if bg.position.x <= -bg.size.width {
    bg.position = CGPoint(x:bg.position.x + bg.size.width * 2, y:bg.position.y)
    }
    }))
    }
    }

    }

    }

  2. j M Y, g:ia \G\M\TO
    S Samuel ist dem Forum beigetreten.