根据用户点击屏幕的位置调用不同的功能

时间:2018-07-16 01:22:54

标签: ios swift xcode sprite-kit

我正在构建一个简单的应用,该应用的球从屏幕顶部落下,并且必须击中下面色轮上的相应颜色。此刻,色轮将旋转1次,并在每次用户点击屏幕时对其原始值进行1积分。

我希望色轮在用户点击屏幕右侧时向右旋转,而在用户点击屏幕左侧时向左旋转。我在弄清楚如何“拆分屏幕”时遇到了麻烦,因此当用户点击特定的一侧时会调用特定的旋转功能。

这是我转动车轮并将其与球色匹配的代码...

//转轮功能

func turnWheel() {
    colorSwitch.run(SKAction.rotate(byAngle: .pi/2, duration: 0.25))
    if let newState = SwitchState(rawValue: switchState.rawValue + 1){
        switchState = newState
    } else {
        switchState = .red
    }
}

//产生球功能

func spawnBall(){
    currentColorIndex = Int(arc4random_uniform(UInt32(4)))

    let ball = SKSpriteNode(texture: SKTexture(imageNamed: "ball"), color: PlayColors.colors[currentColorIndex!], size: CGSize(width: 20.0, height: 20.0))
    ball.colorBlendFactor = 1.0
    ball.name = "Ball"
    ball.position = CGPoint(x: frame.midX, y: frame.maxY)
    ball.zPosition = ZPositions.ball
    ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
    ball.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory
    ball.physicsBody?.contactTestBitMask = PhysicsCategories.switchCategory
    ball.physicsBody?.collisionBitMask = PhysicsCategories.none

    addChild(ball)
}

//物理联系人代表

extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
    let contactMask = contact.bodyA.categoryBitMask | 
 contact.bodyB.categoryBitMask  
    if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.switchCategory {
        if let ball = contact.bodyA.node?.name == "Ball" ?
            contact.bodyA.node as? SKSpriteNode : contact.bodyB.node as?
            SKSpriteNode {
            if currentColorIndex == switchState.rawValue {
                score += 1
                updateScoreLabel()
                let generator = UIImpactFeedbackGenerator(style: .light)
                generator.impactOccurred()
                ball.removeFromParent()
                self.spawnBall()
            } else {
                gameOver()
                explosion(ball: ball)
                let generator2 = UIImpactFeedbackGenerator(style: .heavy)
                generator2.impactOccurred()
            }
         }
      }    
   }
}

除了使轮子向相反方向旋转外,我还需要它仍然能够匹配颜色。现在,颜色存储在枚举中,并且当转轮的rawValue与球的数组值匹配时,就会发生“正确匹配”。

//球色

enum PlayColors {
static let colors = [
    UIColor(red: 231/255, green: 76/255, blue: 60/255, alpha: 1.0),
    UIColor(red: 241/255, green: 196/255, blue: 15/255, alpha: 1.0),
    UIColor(red: 46/255, green: 204/255, blue: 113/255, alpha: 1.0),
    UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0)
    ]
}
enum SwitchState: Int {
    case red, yellow, green, blue
}

1 个答案:

答案 0 :(得分:0)

基本上,您希望将屏幕分成两部分。为此,您要确定用户单击屏幕的哪一侧。您可以按照以下代码进行操作。

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

    let touch:UITouch = (touches.first as UITouch?)!
    let touchLocation = touch.location(in: self)

     if touchLocation.x < self.frame.size.width / 2 {
            // Left side of the screen
            // Call function to rotate wheel left
     }else{
            // Right side of the screen
            // Call function to rotate wheel right
        }
    }
}

关于您的didBegin(_ contact: SKPhysicsContact),这是种凌乱且难以理解的事情。

我相信您应该创建一个名为Ball的类。这样的事情(我已经从您的spawnBallFunction转移了您的代码)。

class Ball: SKSpriteNode{

        var currentColorIndex = Int(arc4random_uniform(UInt32(4)))
        var currIndex: Int {
            get {
                return currentColorIndex
            }
            set {
                currentColorIndex = newValue
            }
        }

    init(){

        let ballTexture = SKTexture(imageNamed: "ball")
        super.init(texture: ballTexture, color: PlayColors.colors[currentColorIndex!], size: CGSize(width: 20.0, height: 20.0))

        self.colorBlendFactor = 1.0
        self.name = "Ball"
        self.position = CGPoint(x: frame.midX, y: frame.maxY)
        self.zPosition = ZPositions.ball
        self.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)
        self.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory
        self.physicsBody?.contactTestBitMask = PhysicsCategories.switchCategory
        self.physicsBody?.collisionBitMask = PhysicsCategories.none
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

现在在您的spawnBall函数中,您可以执行以下操作:

func spawnBall(){
    let ball = Ball() 
    self.addChild(ball)
}

这是我为清理您的联系功能所做的工作:

extension GameScene{

    func didBegin(_ contact: SKPhysicsContact) {

      var ball: Ball
      var otherNode: SKSpriteNode //not sure what this other node is by looking at your code

      if(contact.bodyA.name == "ball"){
          ball = contact.bodyA.node! as! Ball
          otherNode = contact.bodyB.node! as! SKSpriteNode
      }
      else{
          otherNode = contact.bodyA.node! as! SKSpriteNode
          ball = contact.bodyB.node! as! Ball
      }

      if ball.currentColorIndex == switchState.rawValue {

          score += 1
          updateScoreLabel()
          let generator = UIImpactFeedbackGenerator(style: .light)
          generator.impactOccurred()
          ball.removeFromParent()
          self.spawnBall()
       } else {
          gameOver()
          explosion(ball: ball)
          let generator2 = UIImpactFeedbackGenerator(style: .heavy)
          generator2.impactOccurred()
       }
    }
}