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

时间:2021-11-16 21:43:25

I have more problems within my app. My errors is as the title says:

我的应用程序中存在更多问题。我的错误如标题所示:

  1. Swift Compiler Error.
  2. Swift编译器错误。

Error1: 'CGFloat' is not Convertible to 'Float'

错误1:'CGFloat'无法转换为'Float'

Code for the 1st and 2nd error:

第1和第2错误的代码:

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

Here is the whole 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 Compiler Error.
  2. Swift编译器错误。

Error1: 'Float' is not identical to 'UInt8'

错误1:'浮动'与'UInt8'不同

Code for the 1st error:

第一个错误的代码:

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

Error2: 'SKPhysicsBody?' does not have a member named 'velocity'

错误2:'SKPhysicsBody?'没有名为'velocity'的成员

Code for the 2nd error:

第二个错误的代码:

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

Here is the whole 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
        }
    }

  1. Swift Compiler Error.
  2. Swift编译器错误。

Error1: 'CGFloat' is not identical to 'UInt8'

错误1:'CGFloat'与'UInt8'不同

Code for the 1st and 2nd error (Same error both lines):

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

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

Error2: Cannot invoke '-=' with an argument list of type '(@lvalue CGFloat, $T9)'

错误2:无法使用类型为'(@lvalue CGFloat,$ T9)的参数列表调用' - ='

Code for the 3rd and 4th error (Same error both lines):

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

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

Error3: 'CGFloat' is not identical to 'UInt8'

错误3:'CGFloat'与'UInt8'不同

pipe.position.x -= movingSpeed

Here is the whole 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)"
            }
        }
    }

I hope some of you guys can figure this out for me because I can't :)

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

1 个解决方案

#1


5  

Error 1: Use CGFloat instead of Float. For example:

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

var vel: CGFloat = 200

Error 2: physicsBody is an optional (can be nil), so use ?. operator to conditionally unwrap it before referencing its velocity:

错误2:physicsBody是可选的(可以是nil),所以使用?。运算符在引用其速度之前有条件地展开它:

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

All of your errors seem to be caused by one of these two issues. For example, spawnPipeRow's argument is again offs: Float instead of CGFloat. And I suspect the same is true of the values that you did not declare in the snippets you provided, such as space or movingSpeed.

您的所有错误似乎都是由这两个问题之一引起的。例如,spawnPipeRow的参数是offs:Float而不是CGFloat。我怀疑你在你提供的片段中没有声明的值也是如此,例如space或movingSpeed。

To understand what CGFloat is, Command-click on it – this will take you to CoreGraphics API where you can read that CGFloat is a "native type ... which is Float on 32-bit architectures and Double on 64-bit architectures."

要了解CGFloat是什么,请按住Command键点击它 - 这将带您进入CoreGraphics API,您可以在其中读取CGFloat是“本机类型......在32位体系结构上是Float,在64位体系结构上是Double”。

Also, I would tend to use ? rather than ! when modifying physicsBody as the latter will crash if the physicsBody is nil.

另外,我倾向于使用?而不是 !修改physicsBody时,如果physicsBody为nil,后者将崩溃。

#1


5  

Error 1: Use CGFloat instead of Float. For example:

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

var vel: CGFloat = 200

Error 2: physicsBody is an optional (can be nil), so use ?. operator to conditionally unwrap it before referencing its velocity:

错误2:physicsBody是可选的(可以是nil),所以使用?。运算符在引用其速度之前有条件地展开它:

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

All of your errors seem to be caused by one of these two issues. For example, spawnPipeRow's argument is again offs: Float instead of CGFloat. And I suspect the same is true of the values that you did not declare in the snippets you provided, such as space or movingSpeed.

您的所有错误似乎都是由这两个问题之一引起的。例如,spawnPipeRow的参数是offs:Float而不是CGFloat。我怀疑你在你提供的片段中没有声明的值也是如此,例如space或movingSpeed。

To understand what CGFloat is, Command-click on it – this will take you to CoreGraphics API where you can read that CGFloat is a "native type ... which is Float on 32-bit architectures and Double on 64-bit architectures."

要了解CGFloat是什么,请按住Command键点击它 - 这将带您进入CoreGraphics API,您可以在其中读取CGFloat是“本机类型......在32位体系结构上是Float,在64位体系结构上是Double”。

Also, I would tend to use ? rather than ! when modifying physicsBody as the latter will crash if the physicsBody is nil.

另外,我倾向于使用?而不是 !修改physicsBody时,如果physicsBody为nil,后者将崩溃。