2014-09-29 46 views
6

我的应用程序中存在更多问题。我的错误是正如标题所示:'CGFloat'不可转换为'浮动'等更多

  1. Swift编译器错误。

ERROR1: 'CGFloat的' 是无法转换为 '浮动'

代码为第1和第2个错误:

self.setPositionRelativeBot(pipeBot, x: xx, y: offset) 
self.setPositionRelativeTop(pipeTop, x: xx, y: offset + space) 

这里是整个FUNC:

func spawnPipeRow(offs: Float) 
    { 
     let offset = offs - space/2 

     let pipeBot = mainPipe.copy() as Pipe 
     let pipeTop = mainPipe.copy() as Pipe 

     pipeBot.texture = SKTexture(imageNamed: "BotPipe") 
     pipeTop.texture = SKTexture(imageNamed: "TopPipe") 

     pipeBot.texture!.filteringMode = SKTextureFilteringMode.Nearest 
     pipeTop.texture!.filteringMode = SKTextureFilteringMode.Nearest 

     pipeBot.isBottom = true 

     let xx = self.view!.bounds.size.width 

     self.setPositionRelativeBot(pipeBot, x: xx, y: offset) 
     self.setPositionRelativeTop(pipeTop, x: xx, y: offset + space) 

     pipeBot.physicsBody = SKPhysicsBody(rectangleOfSize: pipeBot.size) 
     pipeTop.physicsBody = SKPhysicsBody(rectangleOfSize: pipeTop.size) 

     pipeBot.physicsBody!.dynamic = false 
     pipeTop.physicsBody!.dynamic = false 

     pipeBot.physicsBody!.contactTestBitMask = birdCategory 
     pipeTop.physicsBody!.contactTestBitMask = birdCategory 

     pipeBot.physicsBody!.collisionBitMask = birdCategory 
     pipeTop.physicsBody!.collisionBitMask = birdCategory 

     pipes.append(pipeBot) 
     pipes.append(pipeTop) 

     self.addChild(pipeBot) 
     self.addChild(pipeTop) 
    } 

  1. Swift编译器错误。

ERROR1: '浮动' 不等同于 'UINT8'

代码为第1错误:

vel -= 85 - (self.view!.bounds.size.height - bird.position.y) 

误差2: '?SKPhysicsBody' 不具有名为构件 '速度'

代码的第二错误:

bird.physicsBody.velocity = CGVectorMake(0, vel) 

这里是整个FUNC:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) 
    { 
     if (!bird.physicsBody!.dynamic) 
     { 
      //First touch 

      self.spawnPipeRow(0) 

      bird.physicsBody!.dynamic = true 

      bird.physicsBody!.velocity = CGVectorMake(0, 175) 

      scoreLabel.hidden = false 

      isMoving = true 
     } else if (isMoving) 
     { 
      var vel: Float = 200 

      if (self.view!.bounds.size.height - bird.position.y < 85) 
      { 
       vel -= 85 - (self.view!.bounds.size.height - bird.position.y) 
      } 

      bird.physicsBody.velocity = CGVectorMake(0, vel) 
     } else 
     { 
      overlay.removeFromParent() 

      for pi in pipes 
      { 
       pi.removeFromParent() 
      } 

      pipes.removeAll(keepCapacity: false) 

      score = 0 

      bird.physicsBody!.dynamic = false 
      bird.position = CGPoint(x: 150, y: view!.bounds.size.height/2 - 10) 

      scoreLabel.hidden = true 

      isGroundMoving = true 
     } 
    } 

  • Swift编译器错误。
  • ERROR1: 'CGFloat的' 不等于 'UINT8'

    代码的第一和第二误差(同样的错误都行):

    ground1.position.x -= movingSpeed 
    ground2.position.x -= movingSpeed 
    

    误差2: 不能调用' - ='带有类型参数列表'(@lvalue CGFloat,$ T9)'

    代码为第三和第四个错误(相同错误两行):

    background1.position.x -= movingSpeed/3 
    background2.position.x -= movingSpeed/3 
    

    误差3: 'CGFloat的' 不等同于 'UINT8'

    pipe.position.x -= movingSpeed 
    

    这里是整个FUNC:

    override func update(currentTime: CFTimeInterval) 
        { 
         if (isGroundMoving) 
         { 
          ground1.position.x -= movingSpeed 
          ground2.position.x -= movingSpeed 
    
          if (ground1.position.x <= -self.view!.bounds.size.width/2) 
          { 
           ground1.position.x = self.view!.bounds.size.width * 1.5 - 2 
          } 
    
          if (ground2.position.x <= -self.view!.bounds.size.width/2) 
          { 
           ground2.position.x = self.view!.bounds.size.width * 1.5 - 2 
          } 
    
          background1.position.x -= movingSpeed/3 
          background2.position.x -= movingSpeed/3 
    
          if (background1.position.x <= -self.view!.bounds.size.width/2) 
          { 
           background1.position.x = self.view!.bounds.size.width * 1.5 - 2 
          } 
    
          if (background2.position.x <= -self.view!.bounds.size.width/2) 
          { 
           background2.position.x = self.view!.bounds.size.width * 1.5 - 2 
          } 
    
          if (isMoving) 
          { 
           for (var i = 0; i < pipes.count; i++) 
           { 
            let pipe = pipes[i] 
    
            if (pipe.position.x + (pipe.size.width/2) < 0) 
            { 
             pipe.removeFromParent() 
    
             continue 
            } 
    
            if (pipe.position.x + (pipe.size.width/2) < self.view!.bounds.size.width/2 && pipe.isBottom && !pipe.pointAdded) 
            { 
             score++ 
    
             pipe.pointAdded = true 
            } 
    
            pipe.position.x -= movingSpeed 
    
            if (i == pipes.count - 1) 
            { 
             if (pipe.position.x < self.view!.bounds.width - pipe.size.width * 2.0) 
             { 
              self.spawnPipeRow(self.randomOffset()) 
             } 
            } 
           } 
    
           scoreLabel.text = "Score: \(score)" 
          } 
         } 
        } 
    

    我希望有你们的可以算因为我不能:)

    +0

    您需要明确地进行大量转换,或者正确输入变量。例如'让抵消:CGFloat = ...'或'CGFloat(抵消)' – sapi 2014-09-29 07:47:06

    +0

    @sapi我是一个需要简单解释的人,我不明白你的意思,你想让我做什么? :/ – StigPing 2014-09-29 07:55:43

    +0

    而不是投票,人们应该解释为什么。 – StigPing 2014-09-29 08:55:56

    回答

    5

    错误1:使用CGFloat而不是的Float。例如:

    var vel: CGFloat = 200 
    

    错误2:physicsBody是一个可选的(可以为空),所以使用?.操作引用之前有条件地解开它的velocity

    bird.physicsBody?.velocity = CGVectorMake(0, vel) 
    

    所有你的错误似乎是由这两个问题之一引起的。例如,spawnPipeRow的论点再次是offs: Float而不是CGFloat。我怀疑您提供的代码段中未声明的值也是如此,例如spacemovingSpeed

    要了解什么是CGFloat,它命令点击 - 这将带你到CoreGraphics API,你可以阅读CGFloat是“原生型......这是对32位架构,双浮在64-位体系结构“。

    此外,当修改physicsBody时,我倾向于使用?而不是!,因为如果physicsBody为零,后者将会崩溃。

    +0

    我不确定为什么人们不会投你的问题。我的猜测是,如果您尝试使用最少量的代码来隔离所获得的错误并重现它们,它会更有帮助。这会帮助你意识到至少有两个问题是你应该单独问问他们。而且,如果你确实将它们隔离开来,你很容易就会发现它们已经在这个网站上被询问和回答了。 – milos 2014-09-29 09:20:26

    +0

    今天人们已经投了我几个问题的投票。 – StigPing 2014-09-29 09:39:54

    +0

    谢谢你解决我的错误,现在我只有1个错误,但没有以上;) – StigPing 2014-09-29 09:50:08