'CGFloat'不能转换为'Float'和更多

时间:2014-09-29 07:44:56

标签: ios swift xcode6 cgfloat

我的应用内有更多问题。我的错误就像标题所说:

  1. Swift编译器错误。
  2. ERROR1: ' CGFloat的'不能转换为' Float'

    第1和第2错误的代码:

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

    这是整个功能:

    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编译器错误。
    2. ERROR1: '浮动'与' UInt8'

      不同

      第一个错误的代码:

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

      误差2: ' SKPhysicsBody&#39?;没有名为' velocity'

      的成员

      第二个错误的代码:

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

      这是整个功能:

      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
              }
          }
      

      1. Swift编译器错误。
      2. ERROR1: &#39; CGFloat的&#39;与&#39; UInt8&#39;

        不同

        第1和第2错误的代码(两行都相同):

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

        误差2: 无法调用&#39; - =&#39;使用类型&#39;(@ lvalue CGFloat,$ T9)的参数列表&#39;

        第3和第4个错误的代码(两行都相同):

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

        误差3: &#39; CGFloat的&#39;与&#39; UInt8&#39;

        不同
        pipe.position.x -= movingSpeed
        

        这是整个功能:

        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)"
                    }
                }
            }
        

        我希望你们中的一些人可以为我解决这个问题,因为我不能:)

1 个答案:

答案 0 :(得分:5)

错误1:使用CGFloat代替Float。例如:

var vel: CGFloat = 200

错误2:physicsBody是可选的(可以是nil),因此在引用?.之前使用velocity运算符有条件地解包它:

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

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

要了解CGFloat是什么,按住Command键点击它 - 这会转到CoreGraphics API,您可以在其中读取CGFloat是“本机类型...”浮动在32位体系结构上,在64位体系结构上使用Double。“

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

相关问题