分享菜单会在按两次按钮时打开

时间:2016-11-20 13:51:15

标签: ios swift sprite-kit

我已经为我的游戏添加了一个共享菜单,当游戏结束时,弹出的游戏会出现在共享菜单中。当我按下屏幕上的任何地方时,共享菜单弹出,游戏返回到开头,提示玩家再次点击播放。然后当玩家点击屏幕时,共享菜单再次弹出,这使得游戏在第一次播放后无法播放。我想我需要设置共享菜单才能在按下共享按钮的SKSpriteNode时弹出,如果玩家点击屏幕上的其他任何地方游戏应该重置。如果你能看一下代码,看看我哪里出错,将不胜感激。

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var shareButton = SKSpriteNode()

var santa = SKSpriteNode()

var bg = SKSpriteNode()

var scoreLabel = SKLabelNode()

var tapToPlayLabel = SKLabelNode()

var score = 0

var gameOverScreen = SKSpriteNode()

var timer = Timer()

enum ColliderType: UInt32 {

    case santa = 1
    case Object = 2
    case Gap = 4

}

enum ButtonName: String {

    case play
    case share

}

var gameOver = false

func makeBlocks() {

    let moveBlocks = SKAction.move(by: CGVector(dx: -2 * self.frame.width, dy: 0), duration: TimeInterval(self.frame.width / 100))

    let gapHeight = santa.size.height * 4

    let movementAmount = arc4random() % UInt32(self.frame.height / 2)

    let blockOffset = CGFloat(movementAmount) - self.frame.height / 4

    let blockTexture = SKTexture(imageNamed: "block1.png")

    let block1 = SKSpriteNode(texture: blockTexture)

    block1.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY + blockTexture.size().height / 2 + gapHeight / 2 + blockOffset)

    block1.run(moveBlocks)

    block1.physicsBody = SKPhysicsBody(rectangleOf: blockTexture.size())
    block1.physicsBody!.isDynamic = false

    block1.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    block1.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    block1.physicsBody!.collisionBitMask = ColliderType.Object.rawValue

    block1.zPosition = -2

    self.addChild(block1)

    let block2Texture = SKTexture(imageNamed: "block2.png")

    let block2 = SKSpriteNode(texture: block2Texture)

    block2.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY - block2Texture.size().height / 2 - gapHeight / 2 + blockOffset)

    block2.run(moveBlocks)

    block2.physicsBody = SKPhysicsBody(rectangleOf: blockTexture.size())
    block2.physicsBody!.isDynamic = false

    block2.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    block2.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    block2.physicsBody!.collisionBitMask = ColliderType.Object.rawValue

    block2.zPosition = -2

    self.addChild(block2)

    let gap = SKNode()

    gap.position = CGPoint(x: self.frame.midX + self.frame.width, y: self.frame.midY + blockOffset)

    gap.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: blockTexture.size().width, height: gapHeight))

    gap.physicsBody!.isDynamic = false

    gap.run(moveBlocks)

    gap.physicsBody!.contactTestBitMask = ColliderType.santa.rawValue
    gap.physicsBody!.categoryBitMask = ColliderType.Gap.rawValue
    gap.physicsBody!.collisionBitMask = ColliderType.Gap.rawValue

    self.addChild(gap)

}

func didBegin(_ contact: SKPhysicsContact) {

    if gameOver == false {

    if contact.bodyA.categoryBitMask == ColliderType.Gap.rawValue || contact.bodyB.categoryBitMask == ColliderType.Gap.rawValue {

        score += 1

        scoreLabel.text = String(score)


    } else {

    self.speed = 0

    gameOver = true

    timer.invalidate()

    let gameOverScreenTexture = SKTexture(imageNamed: "GameOverPopup.jpg")

    var j: CGFloat = 0

    gameOverScreen = SKSpriteNode(texture: gameOverScreenTexture, size: CGSize(width: 600, height: 600))

    gameOverScreen.position = CGPoint(x: self.frame.midX, y: self.frame.midY)

    gameOverScreen.size.height = self.frame.height / 3


    gameOverScreen.zPosition = -1

    self.addChild(gameOverScreen)


     //share button

    let shareButtonTexture = SKTexture(imageNamed: "shareButton.png")

    var k: CGFloat = 0

    shareButton = SKSpriteNode(texture: shareButtonTexture, size: CGSize(width: 500, height: 100))

    shareButton.position = CGPoint(x: self.frame.midX, y: self.frame.midY)

    shareButton.size.height = self.frame.height / 10

    shareButton.name = "shareButton"

    shareButton.zPosition = 0

    self.addChild(shareButton)




        }
    }

}

func openShareMenu(value: String, image: UIImage?) {
    guard let view = view else { return }

    // Activity items
    var activityItems = [AnyObject]()

    // Text
    let text = "Can you beat my score "
    activityItems.append(text as AnyObject)

    // Add image if valid
    if let image = image {
        activityItems.append(image)
    }

    // Activity controller
    let activityController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)


    // Excluded activity types
    activityController.excludedActivityTypes = [
        UIActivityType.airDrop,
        UIActivityType.print,
        UIActivityType.assignToContact,
        UIActivityType.addToReadingList,
    ]

    // Present
    view.window?.rootViewController?.present(activityController, animated: true)
}



override func didMove(to view: SKView) {

    addChild(shareButton)

    self.physicsWorld.contactDelegate = self

    setupGame()

}

func setupGame() {

    timer = Timer.scheduledTimer(timeInterval: 3, target: self, selector: #selector(self.makeBlocks), userInfo: nil, repeats: true)

    let bgTexture = SKTexture(imageNamed: "bg.png")

    let moveBGAnimation = SKAction.move(by: CGVector(dx: -bgTexture.size().width, dy: 0), duration: 7)
    let shiftBGAnimation = SKAction.move(by: CGVector(dx: bgTexture.size().width, dy: 0), duration: 0)
    let moveBGForever = SKAction.repeatForever(SKAction.sequence([moveBGAnimation, shiftBGAnimation]))

    var i: CGFloat = 0

    while i < 3 {

        bg = SKSpriteNode(texture: bgTexture)

        bg.position = CGPoint(x: bgTexture.size().width * i, y: self.frame.midY)

        bg.size.height = self.frame.height

        bg.run(moveBGForever)

        bg.zPosition = -3

        self.addChild(bg)

        i += 1

    }


    let santaTexture = SKTexture(imageNamed: "santa1.png")
    let santaTexture2 = SKTexture(imageNamed: "santa2.png")

    let animation = SKAction.animate(with: [santaTexture, santaTexture2], timePerFrame: 0.1)
    let makeSantaMove = SKAction.repeatForever(animation)

    santa = SKSpriteNode(texture: santaTexture)

    santa.position = CGPoint(x: self.frame.midX, y: self.frame.midY)

    santa.run(makeSantaMove)

    santa.physicsBody = SKPhysicsBody(circleOfRadius: santaTexture.size().height / 2)

    santa.physicsBody!.isDynamic = false

    santa.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    santa.physicsBody!.categoryBitMask = ColliderType.santa.rawValue
    santa.physicsBody!.collisionBitMask = ColliderType.santa.rawValue

    self.addChild(santa)

    let ground = SKNode()

    ground.position = CGPoint(x: self.frame.midX, y: -self.frame.height / 2)

    ground.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.width, height: 1))

    ground.physicsBody!.isDynamic = false

    ground.physicsBody!.contactTestBitMask = ColliderType.Object.rawValue
    ground.physicsBody!.categoryBitMask = ColliderType.Object.rawValue
    ground.physicsBody!.collisionBitMask = ColliderType.Object.rawValue


    self.addChild(ground)

    scoreLabel.fontName = "Helvetica"

    scoreLabel.fontSize = 60

    scoreLabel.text = "0"

    scoreLabel.position = CGPoint(x: self.frame.midX, y: self.frame.height / 2 - 220)

    self.addChild(scoreLabel)

    tapToPlayLabel.fontName = "Helvetica"

    tapToPlayLabel.fontSize = 70

    tapToPlayLabel.text = "Tap to Play!"

    tapToPlayLabel.position = CGPoint(x: self.frame.midX, y: self.frame.height / 2 - 500)

    self.addChild(tapToPlayLabel)


}

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

    if gameOver == false {

     tapToPlayLabel.isHidden = true

     santa.physicsBody!.isDynamic = true

     santa.physicsBody!.velocity = CGVector(dx: 0, dy: 0)

     santa.physicsBody!.applyImpulse(CGVector(dx: 0, dy: 320))

    } else {

        tapToPlayLabel.isHidden = false

        gameOver = false

        score = 0

        self.speed = 1

        self.removeAllChildren()

        setupGame()

    }


    if let name = shareButton.name {

        if name == "shareButton" {

            openShareMenu(value: "\(self.score)", image: nil)


        }

    }

 }

override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered
  }

}

1 个答案:

答案 0 :(得分:0)

您正在调用touches方法错误。您没有使用触摸节点的名称来查看它是否是共享按钮。您只需分配一个属性(如果让name = ...)并查看它是否为共享按钮。您需要与触摸的实际节点的名称进行比较。你也在运行你的gameOver代码随时触摸开火,你需要区分。

试试这个

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

         for touch in touches {
             let location = touch.location(in: self) // get location of touch
             let nodeTouched = atPoint(location) // get touched node

             // Pressed button
             if let nodeName = nodeTouched.name {
                  switch nodeName {
                  case ButtonName.share.rawValue:
                     openShareMenu(value: "\(self.score)", image: nil)
                     return // return early if share button is pressed so your gameOver code below etc doesnt fire.

                  default: 
                      break
                }
           }

           // Did not press button, usual code
            if gameOver == false {..... }
      }
 }

希望这有帮助